├── .gitignore ├── .travis.yml ├── DailyCodingProblem.Solutions ├── 01-99 │ ├── 01-19 │ │ ├── Problem01 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem02 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem03 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem04 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem05 │ │ │ ├── README.md │ │ │ └── solution.js │ │ ├── Problem06 │ │ │ ├── InMemoryLinkedList.cs │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem07 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem08 │ │ │ ├── BinaryTree.cs │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem09 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem10 │ │ │ ├── README.md │ │ │ └── solution.js │ │ ├── Problem11 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Trie.cs │ │ ├── Problem12 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ └── Problem16 │ │ │ ├── OrderLogService.cs │ │ │ ├── README.md │ │ │ └── Solution.cs │ ├── 20-39 │ │ ├── Problem22 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem26 │ │ │ ├── ExtendedLinkedList.cs │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem29 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ └── Problem33 │ │ │ ├── README.md │ │ │ └── Solution.cs │ ├── 40-59 │ │ ├── 42 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem40 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ └── Problem50 │ │ │ ├── Node.cs │ │ │ ├── README.md │ │ │ └── Solution.cs │ ├── 60-79 │ │ ├── Problem61 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem62 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem63 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem69 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem70 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem73 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem74 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem75 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem77 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ └── Problem78 │ │ │ ├── README.md │ │ │ └── Solution.cs │ └── 80-99 │ │ ├── Problem80 │ │ ├── README.md │ │ └── Solution.cs │ │ ├── Problem81 │ │ ├── README.md │ │ └── Solution.cs │ │ ├── Problem82 │ │ ├── README.md │ │ └── Solution.cs │ │ ├── Problem83 │ │ ├── README.md │ │ └── Solution.cs │ │ ├── Problem85 │ │ ├── README.md │ │ └── Solution.cs │ │ ├── Problem88 │ │ ├── README.md │ │ └── Solution.cs │ │ └── Problem89 │ │ ├── README.md │ │ └── Solution.cs ├── 100-199 │ ├── 100-119 │ │ ├── Problem101 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem102 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem103 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem104 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem106 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem107 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem110 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem113 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem118 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ └── Problem119 │ │ │ ├── README.md │ │ │ └── Solution.cs │ ├── 120-139 │ │ ├── Problem120 │ │ │ ├── README.md │ │ │ └── Singleton.cs │ │ ├── Problem122 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem123 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem124 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem125 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem126 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem127 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem129 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem133 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem134 │ │ │ ├── README.md │ │ │ └── SparseArray.cs │ │ ├── Problem135 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── Problem138 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ ├── 140-159 │ │ ├── Problem140 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem143 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem145 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem147 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem149 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem150 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem152 │ │ │ ├── NumberGenerator.cs │ │ │ └── README.md │ │ ├── Problem153 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem155 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem158 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── Problem159 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ ├── 160-179 │ │ ├── Problem161 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── Problem163 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem164 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem166 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── Problem167 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ └── 180-199 │ │ ├── Problem181 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs │ │ ├── Problem184 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs │ │ └── Problem195 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs ├── 200-299 │ ├── 200-219 │ │ ├── 202 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── 208 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ ├── 220-239 │ │ ├── 226 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 237 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem232 │ │ │ ├── PrefixMapSum.cs │ │ │ ├── README.md │ │ │ └── Tests.cs │ │ └── Problem233 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ ├── 240-259 │ │ ├── 241 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 245 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 246 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── Problem258 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── Problem259 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ ├── 260-279 │ │ ├── 260 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 263 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 267 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 268 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 270 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 273 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 274 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── 275 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ └── 280-299 │ │ ├── 282 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs │ │ ├── 292 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs │ │ └── 295 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs ├── 300-399 │ ├── 300-319 │ │ ├── 313 │ │ │ ├── README.md │ │ │ └── Solution.cs │ │ ├── 314 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 315 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── 317 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ ├── 320-339 │ │ ├── 321 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 326 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 329 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 332 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── 339 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ ├── 340-359 │ │ ├── 342 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 346 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 349 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ ├── 355 │ │ │ ├── README.md │ │ │ ├── Solution.cs │ │ │ └── Tests.cs │ │ └── 356 │ │ │ ├── FixedLengthArraysQueue.cs │ │ │ ├── README.md │ │ │ └── Tests.cs │ └── 360-379 │ │ ├── 360 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs │ │ ├── 363 │ │ ├── AddSubtractBuilder.cs │ │ ├── README.md │ │ └── Tests.cs │ │ ├── 367 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs │ │ ├── 368 │ │ ├── KeyValueStore.cs │ │ ├── README.md │ │ └── Tests.cs │ │ ├── 369 │ │ ├── README.md │ │ ├── StockTracker.cs │ │ └── Tests.cs │ │ ├── 371 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs │ │ └── 372 │ │ ├── README.md │ │ ├── Solution.cs │ │ └── Tests.cs ├── Common │ ├── LinkedList.cs │ └── Node.cs ├── DailyCodingProblem.Solutions.csproj └── SolutionTemplate │ ├── README.md │ ├── Solution.cs │ └── Tests.cs ├── DailyCodingProblem.Tests ├── 01-99 │ ├── 01-19 │ │ ├── Problem01Tests │ │ │ └── TwoNumbersEqualTests.cs │ │ └── Problem02Tests │ │ │ └── GetArrayOfProductsTests.cs │ ├── 20-39 │ │ └── Problem26Tests │ │ │ ├── AddTests.cs │ │ │ ├── Mock │ │ │ └── ExtendedLinkedList.cs │ │ │ └── RemoveKthLastElementTests.cs │ ├── 40-59 │ │ ├── Problem40Tests │ │ │ └── GetNonDuplicatedIntegerTests.cs │ │ └── Problem50Tests │ │ │ └── SolutionTests.cs │ ├── 60-79 │ │ ├── Problem61Tests │ │ │ └── PowTests.cs │ │ ├── Problem62Tests │ │ │ └── SolutionTests.cs │ │ ├── Problem63Tests │ │ │ └── SolutionTests.cs │ │ ├── Problem69Tests │ │ │ └── GetLargestProductTests.cs │ │ ├── Problem70Tests │ │ │ └── GetNthPerfectNumberTests.cs │ │ ├── Problem73Tests │ │ │ └── ReverseTests.cs │ │ ├── Problem74Tests │ │ │ └── GetXOccurencesCountTests.cs │ │ ├── Problem75Tests │ │ │ └── GetLongestIncreasingSubsequenceTests.cs │ │ └── Problem77Tests │ │ │ └── MergeOverlappingIntervalsTests.cs │ └── 80-99 │ │ ├── Problem80Tests │ │ └── GetDeepestNodeTests.cs │ │ ├── Problem81Tests │ │ └── GetPossibleLettersTests.cs │ │ ├── Problem85Tests │ │ └── GetNumberTests.cs │ │ ├── Problem88Tests │ │ └── DivideTests.cs │ │ └── Problem89Tests │ │ └── IsBinaryTreeTests.cs ├── 100-199 │ ├── 100-119 │ │ ├── Problem101Tests │ │ │ └── GetPrimeNumbersWithCurrentSumTests.cs │ │ ├── Problem102Tests │ │ │ └── GetContiguousElementsThatSumToKTests.cs │ │ ├── Problem103Tests │ │ │ └── GetShortestSubstringContainingAllCharactersTests.cs │ │ ├── Problem104Tests │ │ │ ├── IsDoublyLinkedListPalindromeTests.cs │ │ │ └── IsSinglyLinkedListPalindromeTests.cs │ │ ├── Problem106Tests │ │ │ └── CanReachLastIndexTests.cs │ │ ├── Problem107Tests │ │ │ └── PrintTreeLevelWiseTests.cs │ │ ├── Problem110Tests │ │ │ └── GetPathsToLeavesTests.cs │ │ ├── Problem113Tests │ │ │ └── ReverseWordsTests.cs │ │ ├── Problem118Tests │ │ │ └── GetSquaredSortedElementsTests.cs │ │ └── Problem119Tests │ │ │ └── GetIntervalThatCoversOthersTests.cs │ ├── 120-139 │ │ ├── Problem120Tests │ │ │ └── SingletonTests.cs │ │ ├── Problem122Tests │ │ │ └── GetMaxNumberOfCoinsTests.cs │ │ ├── Problem123Tests │ │ │ └── IsNumberTests.cs │ │ ├── Problem125Tests │ │ │ └── GetNodesWithSumKTests.cs │ │ ├── Problem126Tests │ │ │ └── RotateListTests.cs │ │ ├── Problem127Tests │ │ │ └── SumNumbersTests.cs │ │ └── Problem129Tests │ │ │ └── GetSquareRootTests.cs │ └── 160-179 │ │ └── Problem161Tests │ │ └── GetReversedBitRepresentationTests.cs └── DailyCodingProblem.Tests.csproj ├── DailyCodingProblem.sln ├── LICENSE ├── README.md └── Tips ├── An Introduction To Backtracking.md ├── How to Formulaically Solve Tree Interview Questions.md └── How to Pick a Random Element from an Infinite Stream.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: csharp 2 | solution: DailyCodingProblem.sln 3 | mono: none 4 | dotnet: 2.2 5 | 6 | notifications: 7 | email: false 8 | 9 | script: 10 | - dotnet restore 11 | - dotnet build 12 | - dotnet test DailyCodingProblem.Tests/ 13 | - dotnet test DailyCodingProblem.Solutions/ -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem01/README.md: -------------------------------------------------------------------------------- 1 | This problem was recently asked by Google. 2 | 3 | Given a list of numbers and a number k, return whether any two numbers from the list add up to k. 4 | 5 | For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. 6 | 7 | Bonus: Can you do this in one pass? -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem01/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem01 6 | { 7 | public class Solution 8 | { 9 | public static void Test() 10 | { 11 | var numbers = Console.ReadLine() 12 | .Split(' ') 13 | .Select(int.Parse) 14 | .ToList(); 15 | 16 | var k = int.Parse(Console.ReadLine()); 17 | 18 | var result = TwoNumbersEqual(numbers, k); 19 | Console.WriteLine(result); 20 | } 21 | 22 | public static bool TwoNumbersEqual(List numbers, int k) 23 | { 24 | var set = new HashSet(); 25 | 26 | foreach (var number in numbers) 27 | { 28 | var remaining = k - number; 29 | 30 | if (set.Contains(remaining)) 31 | { 32 | return true; 33 | } 34 | 35 | set.Add(number); 36 | } 37 | 38 | return false; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem02/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Uber. 2 | 3 | Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. 4 | 5 | For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6]. 6 | 7 | Follow-up: what if you can't use division? -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem02/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem02 5 | { 6 | public class Solution 7 | { 8 | public static void Test() 9 | { 10 | var input = Console.ReadLine() 11 | .Split(' ') 12 | .Select(int.Parse) 13 | .ToArray(); 14 | 15 | var result = GetArrayOfProducts(input); 16 | 17 | Console.WriteLine(string.Join(' ', result)); 18 | } 19 | 20 | public static int[] GetArrayOfProducts(int[] input) 21 | { 22 | var result = new int[input.Length]; 23 | 24 | for (var i = 0; i < result.Length; i++) 25 | { 26 | result[i] = 1; 27 | 28 | for (int j = 0; j < result.Length; j++) 29 | { 30 | if (j != i) 31 | { 32 | result[i] *= input[j]; 33 | } 34 | } 35 | } 36 | 37 | return result; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem03/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. 4 | 5 | For example, given the following Node class 6 | 7 | class Node: 8 | def __init__(self, val, left=None, right=None): 9 | self.val = val 10 | self.left = left 11 | self.right = right 12 | The following test should pass: 13 | 14 | node = Node('root', Node('left', Node('left.left')), Node('right')) 15 | assert deserialize(serialize(node)).left.left.val == 'left.left' 16 | 17 | --------------------------------------------- 18 | Solved with the help from [here](https://www.geeksforgeeks.org/serialize-deserialize-binary-tree/) -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem04/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Stripe. 2 | 3 | Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well. 4 | 5 | For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3. 6 | 7 | You can modify the input array in-place. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem04/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem04 6 | { 7 | public class Solution 8 | { 9 | public static void Test() 10 | { 11 | var input = Console.ReadLine() 12 | .Split(' ') 13 | .Select(int.Parse) 14 | .ToHashSet(); 15 | 16 | var minPositiveNumber = GetMinimalPositiveNumber(input); 17 | 18 | Console.WriteLine(minPositiveNumber); 19 | } 20 | 21 | public static int GetMinimalPositiveNumber(HashSet input) 22 | { 23 | var minPositiveNumber = 1; 24 | while (true) 25 | { 26 | if (!input.Contains(minPositiveNumber)) 27 | { 28 | break; 29 | } 30 | 31 | minPositiveNumber++; 32 | } 33 | 34 | return minPositiveNumber; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem05/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Jane Street. 2 | 3 | cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. 4 | 5 | Given this implementation of cons: 6 | 7 | def cons(a, b): 8 | def pair(f): 9 | return f(a, b) 10 | return pair 11 | Implement car and cdr. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem05/solution.js: -------------------------------------------------------------------------------- 1 | const cons = (a, b) => { 2 | const pair = (f) => { 3 | return f(a, b); 4 | } 5 | 6 | return pair; 7 | } 8 | 9 | const car = (pair) => { 10 | const f = (a, b) => a; 11 | 12 | return pair(f); 13 | } 14 | 15 | const cdr = (pair) => { 16 | const f = (a, b) => b; 17 | 18 | return pair(f); 19 | 20 | } 21 | 22 | console.log(car(cons(3, 4)) === 3) 23 | 24 | console.log(cdr(cons(3, 4)) === 4) -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem06/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding next and prev fields, it holds a field named both, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an add(element) which adds the element to the end, and a get(index) which returns the node at index. 4 | 5 | If using a language that has no pointers (such as Python), you can assume you have access to get_pointer and dereference_pointer functions that converts between nodes and memory addresses. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem06/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem06 4 | { 5 | public class Solution 6 | { 7 | public static void Test(string[] args) 8 | { 9 | var list = new InMemoryLinkedList(); 10 | 11 | for (var i = 0; i < 10; i++) 12 | { 13 | list.Add(i); 14 | } 15 | 16 | Console.WriteLine(list.Get(5)); 17 | } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem07/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded. 4 | 5 | For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'. 6 | 7 | You can assume that the messages are decodable. For example, '001' is not allowed. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem07/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem07 4 | { 5 | public class Solution 6 | { 7 | public static void Test() 8 | { 9 | var input = Console.ReadLine(); 10 | 11 | var result = NumberOfWaysToDecode(input, 0); 12 | Console.WriteLine(result); 13 | } 14 | 15 | public static int NumberOfWaysToDecode(string message, int index) 16 | { 17 | if (index == message.Length) 18 | { 19 | return 1; 20 | } 21 | 22 | var waysToDecode = 0; 23 | 24 | var currentNumber = int.Parse(message[index].ToString()); 25 | 26 | if (currentNumber > 0) 27 | { 28 | waysToDecode += NumberOfWaysToDecode(message, index + 1); 29 | } 30 | 31 | if (index < message.Length - 1) 32 | { 33 | currentNumber = int.Parse(message.Substring(index, 2)); 34 | 35 | if (currentNumber < 27) 36 | { 37 | waysToDecode += NumberOfWaysToDecode(message, index + 2); 38 | } 39 | } 40 | 41 | return waysToDecode; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem08/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value. 4 | 5 | Given the root to a binary tree, count the number of unival subtrees. 6 | 7 | For example, the following tree has 5 unival subtrees: 8 | 9 | 0 10 | / \ 11 | 1 0 12 | / \ 13 | 1 0 14 | / \ 15 | 1 1 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem08/Solution.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem08 4 | { 5 | public class Solution 6 | { 7 | public static void Test() 8 | { 9 | var root = new Node(0, new Node(1), new Node(0, new Node(1, new Node(1), new Node(1)), new Node(0))); 10 | 11 | var tree = new BinaryTree(root); 12 | 13 | var univalCount = tree.GetUnivalTreesCount(); 14 | 15 | System.Console.WriteLine(univalCount); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem09/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Airbnb. 2 | 3 | Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative. 4 | 5 | For example, [2, 4, 6, 2, 5] should return 13, since we pick 2, 6, and 5. [5, 1, 1, 5] should return 10, since we pick 5 and 5. 6 | 7 | Follow-up: Can you do this in O(N) time and constant space? -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem10/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Apple. 2 | 3 | Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem10/solution.js: -------------------------------------------------------------------------------- 1 | const scheduler = (f, n) => { 2 | setTimeout(f, n); 3 | } 4 | 5 | scheduler(() => console.log('done'), 2500); -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem11/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Twitter. 2 | 3 | Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix. 4 | 5 | For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal]. 6 | 7 | Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem11/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem11 4 | { 5 | public class Solution 6 | { 7 | public static void Test() 8 | { 9 | var trie = new Trie(); 10 | 11 | trie.Add("dog"); 12 | trie.Add("deer"); 13 | trie.Add("deal"); 14 | 15 | var results = trie.Search("de"); 16 | 17 | foreach (var item in results) 18 | { 19 | Console.WriteLine(item); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem12/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Amazon. 2 | 3 | There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters. 4 | 5 | For example, if N is 4, then there are 5 unique ways: 6 | 7 | 1, 1, 1, 1 8 | 2, 1, 1 9 | 1, 2, 1 10 | 1, 1, 2 11 | 2, 2 12 | What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X? For example, if X = {1, 3, 5}, you could climb 1, 3, or 5 steps at a time. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem12/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem12 5 | { 6 | public class Solution 7 | { 8 | public static void Test() 9 | { 10 | var n = int.Parse(Console.ReadLine()); 11 | 12 | var x = Console.ReadLine() 13 | .Split(' ') 14 | .Select(int.Parse) 15 | .ToArray(); 16 | 17 | var waysToClimb = Climb(0, n, x); 18 | 19 | Console.WriteLine(waysToClimb); 20 | } 21 | 22 | public static int Climb(int climbedStairs, int n, int[] x) 23 | { 24 | if (climbedStairs > n) 25 | { 26 | return 0; 27 | } 28 | 29 | if (climbedStairs == n) 30 | { 31 | return 1; 32 | } 33 | 34 | var result = 0; 35 | 36 | for (int i = 0; i < x.Length; i++) 37 | { 38 | var nextClimb = climbedStairs + x[i]; 39 | 40 | result += Climb(nextClimb, n, x); 41 | } 42 | 43 | return result; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem16/OrderLogService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem16 4 | { 5 | public class OrderLogService 6 | { 7 | private readonly LinkedList log = new LinkedList(); 8 | 9 | public OrderLogService(int maxItems) 10 | { 11 | MaxItems = maxItems; 12 | } 13 | 14 | public int MaxItems { get; set; } 15 | 16 | public void Record(int orderId) 17 | { 18 | if (log.Count == this.MaxItems) 19 | { 20 | log.RemoveFirst(); 21 | } 22 | 23 | log.AddLast(orderId); 24 | } 25 | 26 | public int GetLast(int i) 27 | { 28 | var lastNode = log.Last; 29 | 30 | while (i > 0) 31 | { 32 | lastNode = lastNode.Previous; 33 | i--; 34 | } 35 | 36 | return lastNode.Value; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem16/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Twitter. 2 | 3 | You run an e-commerce website and want to record the last N order ids in a log. Implement a data structure to accomplish this, with the following API: 4 | 5 | * record(order_id): adds the order_id to the log 6 | * get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N. 7 | 8 | You should be as efficient with time and space as possible. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/01-19/Problem16/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem16 4 | { 5 | public class Solution 6 | { 7 | public static void Test() 8 | { 9 | var n = 10; 10 | 11 | var service = new OrderLogService(n); 12 | 13 | var totalItems = 15; 14 | 15 | for (var j = 0; j < totalItems; j++) 16 | { 17 | service.Record(j); 18 | } 19 | 20 | var i = 5; 21 | 22 | var ithOrderId = service.GetLast(i); 23 | 24 | Console.WriteLine(ithOrderId); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/20-39/Problem22/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list. If there is more than one possible reconstruction, return any of them. If there is no possible reconstruction, then return null. 4 | 5 | For example, given the set of words 'quick', 'brown', 'the', 'fox', and the string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox']. 6 | 7 | Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string "bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 'and', 'beyond']. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/20-39/Problem22/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem22 7 | { 8 | public class Solution 9 | { 10 | public static void Test() 11 | { 12 | var dictionary = Console.ReadLine() 13 | .Split(' ') 14 | .ToHashSet(); 15 | 16 | var input = Console.ReadLine(); 17 | 18 | var words = GetWordsFromDictionary(input, dictionary); 19 | 20 | Console.WriteLine(string.Join(' ', words)); 21 | } 22 | 23 | public static IEnumerable GetWordsFromDictionary(string input, HashSet dictionary) 24 | { 25 | var builder = new StringBuilder(); 26 | 27 | var result = new List(); 28 | 29 | for (var i = 0; i < input.Length; i++) 30 | { 31 | var currentWord = builder.Append(input[i]); 32 | var word = builder.ToString(); 33 | 34 | if (dictionary.Contains(word)) 35 | { 36 | result.Add(word); 37 | 38 | builder = builder.Clear(); 39 | } 40 | } 41 | 42 | return result; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/20-39/Problem26/ExtendedLinkedList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem26 4 | { 5 | public class ExtendedLinkedList : Common.LinkedList 6 | where T : IComparable, IEquatable 7 | { 8 | public T RemoveKthLastElement(int k) 9 | { 10 | var iterations = this.count - k; 11 | 12 | var currentNode = this.Root; 13 | var previousNode = (LinkedListNode)null; 14 | 15 | while (iterations > 0) 16 | { 17 | previousNode = currentNode; 18 | currentNode = currentNode.Next; 19 | iterations--; 20 | } 21 | 22 | previousNode.Next = currentNode.Next; 23 | this.count--; 24 | 25 | return currentNode.Value; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/20-39/Problem26/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given a singly linked list and an integer k, remove the kth last element from the list. k is guaranteed to be smaller than the length of the list. 4 | 5 | The list is very long, so making more than one pass is prohibitively expensive. 6 | 7 | Do this in constant space and in one pass. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/20-39/Problem26/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem26 4 | { 5 | public static class Solution 6 | { 7 | public static void Test() 8 | { 9 | var list = new ExtendedLinkedList(); 10 | 11 | for (int i = 1; i < 11; i++) 12 | { 13 | list.Add(i); 14 | } 15 | 16 | var k = int.Parse(Console.ReadLine()); 17 | 18 | var number = list.RemoveKthLastElement(k); 19 | 20 | Console.WriteLine(number); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/20-39/Problem29/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Amazon. 2 | 3 | Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string "AAAABBBCCDAA" would be encoded as "4A3B2C1D2A". 4 | 5 | Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/20-39/Problem33/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Compute the running median of a sequence of numbers. That is, given a stream of numbers, print out the median of the list so far on each new element. 4 | 5 | Recall that the median of an even-numbered list is the average of the two middle numbers. 6 | 7 | For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should print out: 8 | 9 | 2 10 | 1.5 11 | 2 12 | 3.5 13 | 2 14 | 2 15 | 2 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/40-59/42/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given a list of integers S and a target number k, write a function that returns a subset of S that adds up to k. If such a subset cannot be made, then return null. 4 | 5 | Integers can appear more than once in the list. You may assume all numbers in the list are positive. 6 | 7 | For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] since it sums up to 24 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/40-59/42/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem42 5 | { 6 | public class Solution 7 | { 8 | public static IEnumerable GetSubsetThatAddsToK(IList numbers, int k) 9 | { 10 | return GetSubsetThatAddsToK(numbers, k, 0, 0, new List()); 11 | } 12 | 13 | private static IEnumerable GetSubsetThatAddsToK(IList numbers, int k, int currentIndex, int currentSum, IEnumerable currentNumbers) 14 | { 15 | if (currentSum == k) 16 | { 17 | return currentNumbers; 18 | } 19 | 20 | if (currentSum > k || currentIndex == numbers.Count()) 21 | { 22 | return null; 23 | } 24 | 25 | var withNumbers = new List(currentNumbers) {numbers[currentIndex]}; 26 | var with = GetSubsetThatAddsToK(numbers, k, currentIndex + 1, currentSum + numbers[currentIndex], 27 | withNumbers); 28 | 29 | var without = GetSubsetThatAddsToK(numbers, k, currentIndex + 1, currentSum, currentNumbers); 30 | 31 | return with ?? without; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/40-59/42/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem42 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetSubsetsThatAddsToK_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var s = new List { 12, 1, 61, 5, 9, 2 }; 15 | var k = 24; 16 | 17 | var expected = new List { 12, 9, 2, 1 }; 18 | 19 | // Act 20 | var actual = Solution.GetSubsetThatAddsToK(s, k); 21 | 22 | // Assert 23 | actual.Should().BeEquivalentTo(expected); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/40-59/Problem40/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer. 4 | 5 | For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19. 6 | 7 | Do this in O(N) time and O(1) space. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/40-59/Problem40/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem40 5 | { 6 | public class Solution 7 | { 8 | public static void Test() 9 | { 10 | } 11 | 12 | public static int GetNonDuplicatedInteger(IEnumerable input) 13 | { 14 | var map = new HashSet(); 15 | 16 | var results = new HashSet(); 17 | 18 | foreach (var element in input) 19 | { 20 | if (!map.Contains(element)) 21 | { 22 | results.Add(element); 23 | map.Add(element); 24 | } 25 | else 26 | { 27 | if (results.Contains(element)) 28 | { 29 | results.Remove(element); 30 | } 31 | } 32 | } 33 | 34 | return results.First(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/40-59/Problem50/Node.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem50 2 | { 3 | public class Node 4 | { 5 | public Node(int? value) 6 | { 7 | Value = value; 8 | } 9 | 10 | public Node(char sign) 11 | { 12 | Sign = sign; 13 | } 14 | 15 | public Node(char sign, Node left, Node right) 16 | : this(sign) 17 | { 18 | Left = left; 19 | Right = right; 20 | } 21 | 22 | public int? Value { get; set; } 23 | 24 | public char Sign { get; set; } 25 | 26 | public Node Left { get; set; } 27 | 28 | public Node Right { get; set; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/40-59/Problem50/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of '+', '−', '∗', or '/'. 4 | 5 | Given the root to such a tree, write a function to evaluate it. 6 | 7 | For example, given the following tree: 8 | 9 | * 10 | / \ 11 | + + 12 | / \ / \ 13 | 3 2 4 5 14 | You should return 45, as it is (3 + 2) * (4 + 5). -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem61/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Implement integer exponentiation. That is, implement the pow(x, y) function, where x and y are integers and returns x^y. 4 | 5 | Do this faster than the naive method of repeated multiplication. 6 | 7 | For example, pow(2, 10) should return 1024. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem61/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem61 2 | { 3 | public class Solution 4 | { 5 | public static void Test() 6 | { 7 | } 8 | 9 | public static long Pow(int number, int power) 10 | { 11 | var pows = new int[power]; 12 | pows[0] = 1; 13 | pows[1] = number; 14 | 15 | var currentPow = 1; 16 | var currentNumber = number; 17 | 18 | while (true) 19 | { 20 | if (currentPow == power) 21 | { 22 | return currentNumber; 23 | } 24 | 25 | 26 | if (currentPow * 2 < power) 27 | { 28 | currentNumber *= currentNumber; 29 | currentPow *= 2; 30 | } 31 | else 32 | { 33 | currentNumber *= number; 34 | currentPow++; 35 | } 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem62/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down. 4 | 5 | For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right: 6 | 7 | Right, then down 8 | Down, then right 9 | Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem62/Solution.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace DailyCodingProblem.Solutions.Problem62 3 | { 4 | public class Solution 5 | { 6 | public static void Test() 7 | { 8 | } 9 | 10 | public static int GetNumberOfWaysToCorner(int n, int m) 11 | { 12 | return GetNumberOfWaysToPosition(n, m, 1, 1); 13 | } 14 | 15 | private static int GetNumberOfWaysToPosition(int row, int col, int currentRow, int currentCol) 16 | { 17 | if (currentRow == row && currentCol == col) 18 | { 19 | return 1; 20 | } 21 | 22 | var result = 0; 23 | 24 | if (currentRow < row) 25 | { 26 | result += GetNumberOfWaysToPosition(row, col, currentRow + 1, currentCol); 27 | } 28 | 29 | if (currentCol < col) 30 | { 31 | result += GetNumberOfWaysToPosition(row, col, currentRow, currentCol + 1); 32 | } 33 | 34 | return result; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem63/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Given a 2D matrix of characters and a target word, write a function that returns whether the word can be found in the matrix by going left-to-right, or up-to-down. 4 | 5 | For example, given the following matrix: 6 | 7 | [['F', 'A', 'C', 'I'], 8 | ['O', 'B', 'Q', 'P'], 9 | ['A', 'N', 'O', 'B'], 10 | ['M', 'A', 'S', 'S']] 11 | and the target word 'FOAM', you should return true, since it's the leftmost column. Similarly, given the target word 'MASS', you should return true, since it's the last row. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem69/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Given a list of integers, return the largest product that can be made by multiplying any three integers. 4 | 5 | For example, if the list is [-10, -10, 5, 2], we should return 500, since that's -10 * -10 * 5. 6 | 7 | You can assume the list has at least three integers. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem70/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | A number is considered perfect if its digits sum up to exactly 10. 4 | 5 | Given a positive integer n, return the n-th perfect number. 6 | 7 | For example, given 1, you should return 19. Given 2, you should return 28. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem70/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem70 4 | { 5 | public class Solution 6 | { 7 | public static void Test() 8 | { 9 | Console.WriteLine("Hello World!"); 10 | } 11 | 12 | public static int GetNthPerfectNumber(int n) 13 | { 14 | var counter = 19; 15 | while (true) 16 | { 17 | if (IsPerfectNumber(counter)) 18 | { 19 | n--; 20 | } 21 | 22 | if (n == 0) 23 | { 24 | return counter; 25 | } 26 | 27 | counter++; 28 | } 29 | } 30 | 31 | private static bool IsPerfectNumber(int number) 32 | { 33 | var sum = 0; 34 | 35 | while (number > 0) 36 | { 37 | sum += (number % 10); 38 | number /= 10; 39 | 40 | if (sum > 10) 41 | { 42 | return false; 43 | } 44 | } 45 | 46 | if (sum == 10) 47 | { 48 | return true; 49 | } 50 | 51 | return false; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem73/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given the head of a singly linked list, reverse it in-place. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem74/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Apple. 2 | 3 | Suppose you have a multiplication table that is N by N. That is, a 2D array where the value at the i-th row and j-th column is (i + 1) * (j + 1) (if 0-indexed) or i * j (if 1-indexed). 4 | 5 | Given integers N and X, write a function that returns the number of times X appears as a value in an N by N multiplication table. 6 | 7 | For example, given N = 6 and X = 12, you should return 4, since the multiplication table looks like this: 8 | 9 | | 1 | 2 | 3 | 4 | 5 | 6 | 10 | 11 | | 2 | 4 | 6 | 8 | 10 | 12 | 12 | 13 | | 3 | 6 | 9 | 12 | 15 | 18 | 14 | 15 | | 4 | 8 | 12 | 16 | 20 | 24 | 16 | 17 | | 5 | 10 | 15 | 20 | 25 | 30 | 18 | 19 | | 6 | 12 | 18 | 24 | 30 | 36 | 20 | 21 | And there are 4 12's in the table. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem74/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem74 2 | { 3 | public class Solution 4 | { 5 | public static void Test() 6 | { 7 | } 8 | 9 | public static int GetXOccurencesCount(int n, int x) 10 | { 11 | var count = 0; 12 | 13 | for (int i = 1; i <= n; i++) 14 | { 15 | if (x % i == 0 && x / i <= n) 16 | { 17 | count++; 18 | } 19 | } 20 | 21 | return count; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem75/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Given an array of numbers, find the length of the longest increasing subsequence in the array. The subsequence does not necessarily have to be contiguous. 4 | 5 | For example, given the array [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], the longest increasing subsequence has length 6: it is 0, 2, 6, 9, 11, 15. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem77/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Snapchat. 2 | 3 | Given a list of possibly overlapping intervals, return a new list of intervals where all overlapping intervals have been merged. 4 | 5 | The input list is not necessarily ordered in any way. 6 | 7 | For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1, 3), (4, 10), (20, 25)]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem77/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem77 6 | { 7 | public class Solution 8 | { 9 | public static void Test() 10 | { 11 | } 12 | 13 | public static IEnumerable<(int start, int end)> MergeOverlappingIntervals(IEnumerable<(int start, int end)> input) 14 | { 15 | var result = new List<(int start, int end)>(); 16 | 17 | foreach (var (start, end) in input) 18 | { 19 | if (!input.Any(el => el.start < start && el.end > end)) 20 | { 21 | result.Add((start, end)); 22 | } 23 | } 24 | 25 | return result; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/60-79/Problem78/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given k sorted singly linked lists, write a function to merge all the lists into one sorted singly linked list. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem80/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given the root of a binary tree, return a deepest node. For example, in the following tree, return d. 4 | 5 | a 6 | / \ 7 | b c 8 | / 9 | d -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem81/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Yelp. 2 | 3 | Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit. 4 | 5 | For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"].1 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem82/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked Microsoft. 2 | 3 | Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters. 4 | 5 | For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem83/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Invert a binary tree. 4 | 5 | For example, given the following tree: 6 | 7 | a 8 | / \ 9 | b c 10 | / \ / 11 | d e f 12 | should become: 13 | 14 | a 15 | / \ 16 | c b 17 | \ / \ 18 | f e d -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem85/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. You can assume b can only be 1 or 0. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem85/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem85 2 | { 3 | public class Solution 4 | { 5 | public static void Test() 6 | { 7 | } 8 | 9 | public static int GetNumber(int b, int x, int y) 10 | { 11 | return b / 1 == 1 ? x : y; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem88/README.md: -------------------------------------------------------------------------------- 1 | This question was asked by ContextLogic. 2 | 3 | Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem88/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem88 2 | { 3 | public class Solution 4 | { 5 | public static int Divide(int divident, int divisor) 6 | { 7 | var quotient = 0; 8 | 9 | while (divident >= divisor) 10 | { 11 | quotient++; 12 | divident -= divisor; 13 | } 14 | 15 | return quotient; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem89/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by LinkedIn. 2 | 3 | Determine whether a tree is a valid binary search tree. 4 | 5 | A binary search tree is a tree with two children, left and right, and satisfies the constraint that the key in the left child must be less than or equal to the root and the key in the right child must be greater than or equal to the root. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/01-99/80-99/Problem89/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using DailyCodingProblem.Solutions.Common; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem89 5 | { 6 | public class Solution 7 | { 8 | public static bool IsBinaryTree(Node root) 9 | where T : IComparable 10 | { 11 | if (root.Left == null && root.Right == null) 12 | { 13 | return true; 14 | } 15 | 16 | if (root.Left != null) 17 | { 18 | if (root.Left.Value.CompareTo(root.Value) >= 0) 19 | { 20 | return false; 21 | } 22 | 23 | if (!IsBinaryTree(root.Left)) 24 | { 25 | return false; 26 | } 27 | } 28 | 29 | if (root.Right != null) 30 | { 31 | if (root.Right.Value.CompareTo(root.Value) <= 0) 32 | { 33 | return false; 34 | } 35 | 36 | if (!IsBinaryTree(root.Right)) 37 | { 38 | return false; 39 | } 40 | } 41 | 42 | return true; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem101/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwetanow/DailyCodingProblem/af11ffee4986e50e11a047bff35467d8c88f72b4/DailyCodingProblem.Solutions/100-199/100-119/Problem101/README.md -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem102/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Lyft. 2 | 3 | Given a list of integers and a number K, return which contiguous elements of the list sum to K. 4 | 5 | For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return [2, 3, 4], since 2 + 3 + 4 = 9. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem102/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem102 5 | { 6 | public class Solution 7 | { 8 | public static int[] GetContiguousElementsThatSumToK(int[] list, int k) 9 | { 10 | var result = new int[] { }; 11 | 12 | for (var i = 0; i < list.Length; i++) 13 | { 14 | var currentSum = 0; 15 | 16 | for (var j = i; j < list.Length; j++) 17 | { 18 | currentSum += list[j]; 19 | 20 | if (currentSum > k) 21 | { 22 | break; 23 | } 24 | 25 | if (currentSum == k) 26 | { 27 | result = list 28 | .Skip(i) 29 | .Take(j - i + 1) 30 | .ToArray(); 31 | 32 | return result; 33 | } 34 | } 35 | } 36 | 37 | return result; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem103/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Square. 2 | 3 | Given a string and a set of characters, return the shortest substring containing all the characters in the set. 4 | 5 | For example, given the string "figehaeci" and the set of characters {a, e, i}, you should return "aeci". 6 | 7 | If there is no substring containing all the characters in the set, return null. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem103/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem103 4 | { 5 | public class Solution 6 | { 7 | public static string GetShortestSubstringContainingAllCharacters(string input, char[] characters) 8 | { 9 | if (!characters.All(input.Contains)) 10 | { 11 | return null; 12 | } 13 | 14 | var withoutLeft = GetShortestSubstringContainingAllCharacters(input.Substring(1, input.Length - 1), characters); 15 | 16 | var withoutRight = GetShortestSubstringContainingAllCharacters(input.Substring(0, input.Length - 1), characters); 17 | 18 | if (withoutLeft == null && withoutRight == null) 19 | { 20 | return input; 21 | } 22 | 23 | if (withoutLeft == null) 24 | { 25 | return withoutRight; 26 | } 27 | 28 | if (withoutRight == null) 29 | { 30 | return withoutLeft; 31 | } 32 | 33 | return withoutRight.Length < withoutLeft.Length ? withoutRight : withoutLeft; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem104/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwetanow/DailyCodingProblem/af11ffee4986e50e11a047bff35467d8c88f72b4/DailyCodingProblem.Solutions/100-199/100-119/Problem104/README.md -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem104/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem104 4 | { 5 | public class Solution 6 | { 7 | public static bool IsDoublyLinkedListPalindrome(LinkedList list) 8 | { 9 | var half = list.Count / 2; 10 | 11 | var left = list.First; 12 | var right = list.Last; 13 | 14 | for (int i = 0; i < half; i++) 15 | { 16 | if (left.Value != right.Value) 17 | { 18 | return false; 19 | } 20 | 21 | left = left.Next; 22 | right = right.Previous; 23 | } 24 | 25 | return true; 26 | } 27 | 28 | public static bool IsSinglyLinkedListPalindrome(Common.LinkedList list) 29 | { 30 | var doublyLinkedList = new LinkedList(); 31 | 32 | var current = list.Root; 33 | 34 | while (current != null) 35 | { 36 | doublyLinkedList.AddLast(current.Value); 37 | 38 | current = current.Next; 39 | } 40 | 41 | return IsDoublyLinkedListPalindrome(doublyLinkedList); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem106/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Pinterest. 2 | 3 | Given an integer list where each number represents the number of hops you can make, determine whether you can reach to the last index starting at index 0. 4 | 5 | For example, [2, 0, 1, 0] returns True while [1, 1, 0, 1] returns False. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem106/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem106 2 | { 3 | public class Solution 4 | { 5 | public static bool CanReachLastIndex(int[] list) 6 | { 7 | var index = 0; 8 | 9 | while (index < list.Length) 10 | { 11 | var current = list[index]; 12 | 13 | index += current; 14 | 15 | if (index == list.Length - 1) 16 | { 17 | return true; 18 | } 19 | 20 | if (current == 0 && index < list.Length - 1) 21 | { 22 | return false; 23 | } 24 | } 25 | 26 | return false; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem107/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Print the nodes in a binary tree level-wise. For example, the following should print 1, 2, 3, 4, 5. 4 | 5 | 1 6 | / \ 7 | 2 3 8 | / \ 9 | 4 5 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem107/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Text; 4 | using DailyCodingProblem.Solutions.Common; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem107 7 | { 8 | public class Solution 9 | { 10 | public static string PrintTreeLevelWise(Common.Node root) 11 | { 12 | var queue = new Queue>(); 13 | 14 | queue.Enqueue(root); 15 | 16 | var builder = new StringBuilder(); 17 | 18 | while (queue.Any()) 19 | { 20 | var current = queue.Dequeue(); 21 | 22 | builder.Append($"{current.Value} "); 23 | 24 | if (current.Left != null) 25 | { 26 | queue.Enqueue(current.Left); 27 | } 28 | 29 | if (current.Right != null) 30 | { 31 | queue.Enqueue(current.Right); 32 | } 33 | } 34 | 35 | return builder.ToString().Trim(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem110/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Given a binary tree, return all paths from the root to leaves. 4 | 5 | For example, given the tree: 6 | 7 | 1 8 | / \ 9 | 2 3 10 | / \ 11 | 4 5 12 | Return [[1, 2], [1, 3, 4], [1, 3, 5]]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem110/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using DailyCodingProblem.Solutions.Common; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem110 7 | { 8 | public class Solution 9 | { 10 | private static readonly List> paths = new List>(); 11 | 12 | public static List> GetPathsToLeaves(Node root) 13 | { 14 | GetPathsToLeaves(root, new List()); 15 | 16 | return paths; 17 | } 18 | 19 | private static void GetPathsToLeaves(Node node, List currentPath) 20 | { 21 | currentPath.Add(node.Value); 22 | 23 | if (node.Left == null && node.Right == null) 24 | { 25 | paths.Add(currentPath); 26 | } 27 | 28 | if (node.Left != null) 29 | { 30 | var copy = currentPath 31 | .Select(x => x) 32 | .ToList(); 33 | 34 | GetPathsToLeaves(node.Left, copy); 35 | } 36 | 37 | if (node.Right != null) 38 | { 39 | var copy = currentPath 40 | .Select(x => x) 41 | .ToList(); 42 | 43 | GetPathsToLeaves(node.Right, copy); 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem113/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given a string of words delimited by spaces, reverse the words in string. For example, given "hello world here", return "here world hello" 4 | 5 | Follow-up: given a mutable string representation, can you perform this operation in-place? -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem113/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem113 5 | { 6 | public class Solution 7 | { 8 | public static string ReverseWords(string input) 9 | { 10 | return string.Join(' ', input 11 | .Split(' ', StringSplitOptions.RemoveEmptyEntries) 12 | .Reverse() 13 | .ToArray()); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem118/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given a sorted list of integers, square the elements and give the output in sorted order. 4 | 5 | For example, given [-9, -2, 0, 2, 3], return [0, 4, 4, 9, 81]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem118/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem118 5 | { 6 | public class Solution 7 | { 8 | public static int[] GetSquaredSortedElements(int[] input) 9 | { 10 | return input 11 | .Select(el => (int)Math.Pow(el, 2)) 12 | .OrderBy(el => el) 13 | .ToArray(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/100-119/Problem119/README.md: -------------------------------------------------------------------------------- 1 | Given a set of closed intervals, find the smallest set of numbers that covers all the intervals. If there are multiple smallest sets, return any of them. 2 | 3 | For example, given the intervals [0, 3], [2, 6], [3, 4], [6, 9], one set of numbers that covers all these intervals is {3, 6}. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem120/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Implement the singleton pattern with a twist. First, instead of storing one instance, store two instances. And in every even call of getInstance(), return the first instance and in every odd call of getInstance(), return the second instance. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem120/Singleton.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem120 2 | { 3 | public class Singleton 4 | { 5 | private static Singleton FirstInstance; 6 | private static Singleton SecondInstance; 7 | 8 | private static int Counter = 1; 9 | 10 | protected Singleton(int id) 11 | { 12 | Id = id; 13 | } 14 | 15 | public static void Reset() 16 | { 17 | FirstInstance = null; 18 | SecondInstance = null; 19 | Counter = 1; 20 | } 21 | 22 | public static void InitializeSingletons(int evenId, int oddId) 23 | { 24 | FirstInstance = new Singleton(evenId); 25 | SecondInstance = new Singleton(oddId); 26 | } 27 | 28 | public static Singleton GetInstance() 29 | { 30 | return Counter++ % 2 == 0 ? FirstInstance : SecondInstance; 31 | } 32 | 33 | public int Id { get; set; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem122/README.md: -------------------------------------------------------------------------------- 1 | You are given a 2-d matrix where each cell represents number of coins in that cell. Assuming we start at matrix[0][0], and can only move right or down, find the maximum number of coins you can collect by the bottom right corner. 2 | 3 | For example, in this matrix 4 | 5 | 0 3 1 1 6 | 2 0 0 4 7 | 1 5 3 1 8 | The most we can collect is 0 + 2 + 1 + 5 + 3 + 1 = 12 coins. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem122/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem122 2 | { 3 | public class Solution 4 | { 5 | private static int MaxNumberOfCoins = 0; 6 | 7 | private static int Rows; 8 | private static int Cols; 9 | 10 | public static int GetMaxNumberOfCoins(int[][] matrix) 11 | { 12 | Rows = matrix.Length; 13 | Cols = matrix[0].Length; 14 | 15 | GetMaxNumberOfCoins(matrix, 0, 0, 0); 16 | 17 | return MaxNumberOfCoins; 18 | } 19 | 20 | private static void GetMaxNumberOfCoins(int[][] matrix, int row, int col, int current) 21 | { 22 | current = current + matrix[row][col]; 23 | 24 | if (row == Rows - 1 && col == Cols - 1) 25 | { 26 | if (MaxNumberOfCoins < current) 27 | { 28 | MaxNumberOfCoins = current; 29 | } 30 | 31 | return; 32 | } 33 | 34 | if (row < Rows - 1) 35 | { 36 | GetMaxNumberOfCoins(matrix, row + 1, col, current); 37 | } 38 | 39 | if (col < Cols - 1) 40 | { 41 | GetMaxNumberOfCoins(matrix, row, col + 1, current); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem123/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by LinkedIn. 2 | 3 | Given a string, return whether it represents a number. Here are the different kinds of numbers: 4 | 5 | "10", a positive integer 6 | "-10", a negative integer 7 | "10.1", a positive real number 8 | "-10.1", a negative real number 9 | "1e5", a number in scientific notation 10 | And here are examples of non-numbers: 11 | 12 | "a" 13 | "x 1" 14 | "a -2" 15 | "-" -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem123/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Globalization; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem123 4 | { 5 | public class Solution 6 | { 7 | public static bool IsNumber(string input) 8 | { 9 | return double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out double result); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem124/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | You have n fair coins and you flip them all at the same time. Any that come up tails you set aside. The ones that come up heads you flip again. How many rounds do you expect to play before only one coin remains? 4 | 5 | Write a function that, given n, returns the number of rounds you'd expect to play until one coin remains. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem124/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem124 4 | { 5 | public class Solution 6 | { 7 | // Because the coins are fair, we assume 1/2 probability of tails. So after first round, 1/2 coins are left, after second is 1/2*1/2, etc... 8 | public static int GetNumberOfRoundsUntillOneCoinLeft(int n) => (int)Math.Floor(Math.Log(n, 2)) + 1; // 2/2=1, log returns to 2 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem124/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem124 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase(50, 6)] // 50->25->13->7->4->2->1 10 | [TestCase(51, 6)] // 51->26 (left)->13->7->4->2->1 11 | public void TestGetNumberOfRoundsUntillOneCoinLeft_ShouldReturnCorrectly(int n, int expectedResult) 12 | { 13 | // Arrange, Act 14 | var actualResult = Solution.GetNumberOfRoundsUntillOneCoinLeft(n); 15 | 16 | // Assert 17 | actualResult.Should().Be(expectedResult); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem125/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given the root of a binary search tree, and a target K, return two nodes in the tree whose sum equals K. 4 | 5 | For example, given the following tree and K of 20 6 | 7 | 10 8 | / \ 9 | 5 15 10 | / \ 11 | 11 15 12 | 13 | Return the nodes 5 and 15. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem125/Solution.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using System.Collections.Generic; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem125 5 | { 6 | public class Solution 7 | { 8 | private static readonly Dictionary> nodes = new Dictionary>(); 9 | 10 | public static int[] GetNodesWithSumK(Node node, int k) 11 | { 12 | if (node == null) 13 | { 14 | return null; 15 | } 16 | 17 | var remaining = k - node.Value; 18 | 19 | if (nodes.ContainsKey(remaining)) 20 | { 21 | return new int[] { nodes[remaining].Value, node.Value }; 22 | } 23 | 24 | nodes.Add(node.Value, node); 25 | 26 | return GetNodesWithSumK(node.Left, k) ?? GetNodesWithSumK(node.Right, k); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem126/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Write a function that rotates a list by k elements. For example, [1, 2, 3, 4, 5, 6] rotated by two becomes [3, 4, 5, 6, 1, 2]. Try solving this without creating a copy of the list. How many swap or move operations do you need? -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem126/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem126 5 | { 6 | public class Solution 7 | { 8 | public static IList RotateList(IList list, int k) 9 | { 10 | for (int i = 0; i < k; i++) 11 | { 12 | var element = list.First(); 13 | 14 | list.RemoveAt(0); 15 | 16 | list.Add(element); 17 | } 18 | 19 | return list; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem127/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Let's represent an integer in a linked list format by having each node represent a digit in the number. The nodes make up the number in reversed order. 4 | 5 | For example, the following linked list: 6 | 7 | 1 -> 2 -> 3 -> 4 -> 5 8 | is the number 54321. 9 | 10 | Given two linked lists in this format, return their sum in the same linked list format. 11 | 12 | For example, given 13 | 14 | 9 -> 9 15 | 5 -> 2 16 | return 124 (99 + 25) as: 17 | 18 | 4 -> 2 -> 1 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem127/Solution.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem127 4 | { 5 | public class Solution 6 | { 7 | public static LinkedList SumNumbers(LinkedList left, LinkedList right) 8 | { 9 | var leftNumber = GetNumber(left); 10 | var rightNumber = GetNumber(right); 11 | 12 | var resultNumber = leftNumber + rightNumber; 13 | 14 | return GetList(resultNumber); 15 | } 16 | 17 | private static LinkedList GetList(int number) 18 | { 19 | var result = new LinkedList(); 20 | 21 | while (number > 0) 22 | { 23 | result.Add(number % 10); 24 | 25 | number /= 10; 26 | } 27 | 28 | return result; 29 | } 30 | 31 | private static int GetNumber(LinkedList linkedList) 32 | { 33 | var multiplyer = 1; 34 | var result = 0; 35 | 36 | var current = linkedList.Root; 37 | while (current != null) 38 | { 39 | result += current.Value * multiplyer; 40 | 41 | multiplyer *= 10; 42 | 43 | current = current.Next; 44 | } 45 | 46 | return result; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem129/README.md: -------------------------------------------------------------------------------- 1 | Given a real number n, find the square root of n. For example, given n = 9, return 3. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem129/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem129 4 | { 5 | public class Solution 6 | { 7 | public static double GetSquareRoot(int n) => Math.Sqrt(n); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem133/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Amazon. 2 | 3 | Given a node in a binary search tree, return the next bigger element, also known as the inorder successor. 4 | 5 | For example, the inorder successor of 22 is 30. 6 | 7 | 10 8 | / \ 9 | 5 30 10 | / \ 11 | 22 35 12 | 13 | You can assume each node has a parent pointer. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem133/Solution.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem133 4 | { 5 | public class Solution 6 | { 7 | public static Node GetInorderSuccessor(Node node) => node.Right ?? node.Parent; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem133/Tests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem133 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetInorderSuccessor_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var root = new Node(10); 15 | root.Left = new Node(5, parent: root); 16 | root.Right = new Node(30, parent: root); 17 | 18 | root.Right.Left = new Node(22, parent: root.Right); 19 | root.Right.Right = new Node(35, parent: root.Right); 20 | 21 | var inputNode = root.Right.Left; 22 | 23 | var expectedResult = root.Right; 24 | 25 | // Act 26 | var actualResult = Solution.GetInorderSuccessor(inputNode); 27 | 28 | // Assert 29 | actualResult.Should().Be(expectedResult); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem134/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | You have a large array with most of the elements as zero. 4 | 5 | Use a more space-efficient data structure, SparseArray, that implements the same interface: 6 | 7 | init(arr, size): initialize with the original large array and size. 8 | set(i, val): updates index at i with val. 9 | get(i): gets the value at index i. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem135/README.md: -------------------------------------------------------------------------------- 1 | This question was asked by Apple. 2 | 3 | Given a binary tree, find a minimum path sum from root to a leaf. 4 | 5 | For example, the minimum path in this tree is [10, 5, 1, -1], which has sum 15. 6 | 7 | 10 8 | / \ 9 | 5 5 10 | \ \ 11 | 2 1 12 | / 13 | -1 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem135/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using DailyCodingProblem.Solutions.Common; 3 | using Moq; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem135 6 | { 7 | public class Solution 8 | { 9 | public static int GetMinimumPathSumToLeaf(Node root) 10 | { 11 | return GetMinimumPathSumToLeaf(root, 0); 12 | } 13 | 14 | private static int GetMinimumPathSumToLeaf(Node node, int currentSum) 15 | { 16 | // Node is leaf 17 | if (node == null) 18 | { 19 | return currentSum; 20 | } 21 | 22 | var leftMinSum = GetMinimumPathSumToLeaf(node.Left, currentSum + node.Value); 23 | var rightMinSum = GetMinimumPathSumToLeaf(node.Right, currentSum + node.Value); 24 | 25 | return Math.Min(leftMinSum, rightMinSum); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem135/Tests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem135 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetMinimumPathSumToLeaf_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var root = new Node(10, 15 | new Node(5, 16 | right: new Node(2) 17 | ), 18 | new Node(5, 19 | right: new Node(1, 20 | left: new Node(-1) 21 | ))); 22 | 23 | var expectedResult = 15; 24 | 25 | // Act 26 | var actualResult = Solution.GetMinimumPathSumToLeaf(root); 27 | 28 | // Assert 29 | actualResult.Should().Be(expectedResult); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem138/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Find the minimum number of coins required to make n cents. 4 | 5 | You can use standard American denominations, that is, 1, 5, 10, and 25. 6 | 7 | For example, given n = 16, return 3 since we can make it with a 10, a 5, and a 1. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem138/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem138 5 | { 6 | public class Solution 7 | { 8 | private static IEnumerable denominations = new List { 25, 10, 5, 1 }; 9 | 10 | public static IEnumerable FindMinimumNumberOfCoinsToSumN(int n) 11 | { 12 | var result = new List(); 13 | 14 | var remaining = n; 15 | 16 | while (remaining > 0) 17 | { 18 | var next = denominations 19 | .Where(d => d <= remaining) 20 | .Max(); 21 | 22 | result.Add(next); 23 | remaining -= next; 24 | } 25 | 26 | return result; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/120-139/Problem138/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem138 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestFindMinimumNumberOfCoinsToSumN_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var n = 16; 15 | var expectedResult = new List {10, 5, 1}; 16 | 17 | // Act 18 | var actualResult = Solution.FindMinimumNumberOfCoinsToSumN(n); 19 | 20 | // Assert 21 | actualResult.Should().BeEquivalentTo(expectedResult); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem140/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Given an array of integers in which two elements appear exactly once and all other elements appear exactly twice, find the two elements that appear only once. 4 | 5 | For example, given the array [2, 4, 6, 8, 10, 2, 6, 10], return 4 and 8. The order does not matter. 6 | 7 | Follow-up: Can you do this in linear time and constant space? -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem140/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem140 4 | { 5 | public class Solution 6 | { 7 | public static IEnumerable FindElementsThatAppearOnce(List list) 8 | { 9 | var result = new HashSet(); 10 | 11 | foreach (var element in list) 12 | { 13 | if (result.Contains(element)) 14 | { 15 | result.Remove(element); 16 | } 17 | else 18 | { 19 | result.Add(element); 20 | } 21 | } 22 | 23 | return result; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem140/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem140 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestFindElementsThatAppearOnce_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var input = new List { 2, 4, 6, 8, 10, 2, 6, 10 }; 15 | var expectedResult = new List { 4, 8 }; 16 | 17 | // Act 18 | var actualResult = Solution.FindElementsThatAppearOnce(input); 19 | 20 | // Assert 21 | actualResult.Should().BeEquivalentTo(expectedResult); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem143/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Amazon. 2 | 3 | Given a pivot x, and a list lst, partition the list into three parts. 4 | 5 | The first part contains all elements in lst that are less than x 6 | The second part contains all elements in lst that are equal to x 7 | The third part contains all elements in lst that are larger than x 8 | Ordering within a part can be arbitrary. 9 | 10 | For example, given x = 10 and lst = [9, 12, 3, 5, 14, 10, 10], one partition may be `[9, 3, 5, 10, 10, 12, 14]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem143/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem143 5 | { 6 | public class Solution 7 | { 8 | public static IEnumerable PartitionList(List input, int x) 9 | { 10 | var smaller = new LinkedList(); 11 | var bigger = new LinkedList(); 12 | var same = new LinkedList(); 13 | 14 | foreach (var element in input) 15 | { 16 | if (element < x) 17 | { 18 | smaller.AddLast(element); 19 | } 20 | else if (element > x) 21 | { 22 | bigger.AddLast(element); 23 | } 24 | else 25 | { 26 | same.AddLast(element); 27 | } 28 | } 29 | 30 | foreach (var i in bigger) 31 | { 32 | same.AddLast(i); 33 | } 34 | 35 | foreach (var i in same) 36 | { 37 | smaller.AddLast(i); 38 | } 39 | 40 | return smaller.ToList(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem143/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem143 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestPartitionList_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var input = new List { 9, 12, 3, 5, 14, 10, 10 }; 15 | var x = 10; 16 | 17 | var expectedResult = new List { 9, 3, 5, 10, 10, 12, 14 }; 18 | 19 | // Act 20 | var actualResult = Solution.PartitionList(input, x); 21 | 22 | // Assert 23 | actualResult.Should().ContainInOrder(expectedResult); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem145/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given the head of a singly linked list, swap every two nodes and return its head. 4 | 5 | For example, given 1 -> 2 -> 3 -> 4, return 2 -> 1 -> 4 -> 3. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem145/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem145 4 | { 5 | public class Solution 6 | { 7 | public static Common.LinkedList SwapEveryTwoNodes(Common.LinkedList list) 8 | { 9 | var current = list.Root; 10 | 11 | while (current != null) 12 | { 13 | SwapValues(current, current.Next); 14 | 15 | current = current.Next.Next; 16 | } 17 | 18 | return list; 19 | } 20 | 21 | private static void SwapValues(Common.LinkedList.LinkedListNode node, Common.LinkedList.LinkedListNode otherNode) 22 | { 23 | var swap = node.Value; 24 | node.Value = otherNode.Value; 25 | otherNode.Value = swap; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem145/Tests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem145 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestSwapEveryTwoNodes_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var list = new LinkedList(); 14 | list.Add(1); 15 | list.Add(2); 16 | list.Add(3); 17 | list.Add(4); 18 | 19 | var expectedList = new LinkedList(); 20 | expectedList.Add(2); 21 | expectedList.Add(1); 22 | expectedList.Add(4); 23 | expectedList.Add(3); 24 | 25 | // Act 26 | var result = Solution.SwapEveryTwoNodes(list); 27 | 28 | // Assert 29 | Assert.AreEqual(expectedList, result); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem147/README.md: -------------------------------------------------------------------------------- 1 | 2 | Given a list, sort it using this method: reverse(lst, i, j), which reverses lst from i to j`. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem147/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem147 5 | { 6 | public static class Solution 7 | { 8 | public static void SortListWithReverse(this List list) 9 | { 10 | var sorted = false; 11 | 12 | while (!sorted) 13 | { 14 | sorted = true; 15 | 16 | for (var i = 0; i < list.Count - 1; i++) 17 | { 18 | if (list[i] > list[i + 1]) 19 | { 20 | list.ReversePartial(i, i + 1); 21 | sorted = false; 22 | } 23 | } 24 | } 25 | } 26 | 27 | public static void ReversePartial(this List list, int i, int j) 28 | { 29 | var copy = list.GetRange(i, j - i + 1); 30 | copy.Reverse(); 31 | 32 | for (int k = i; k <= j; k++) 33 | { 34 | list[k] = copy[k - i]; 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem147/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using FluentAssertions; 4 | using NUnit.Framework; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem147 7 | { 8 | [TestFixture] 9 | public class Tests 10 | { 11 | [Test] 12 | public void TestSortListWithReverse_ShouldReturnCorrectly() 13 | { 14 | // Arrange 15 | var list = Enumerable.Range(0, 10).ToList(); 16 | 17 | var expectedResult = new List(list); 18 | list.Reverse(); 19 | 20 | // Act 21 | list.SortListWithReverse(); 22 | 23 | // Assert 24 | list.Should().BeEquivalentTo(expectedResult).And.BeInAscendingOrder(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem149/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Goldman Sachs. 2 | 3 | Given a list of numbers L, implement a method sum(i, j) which returns the sum from the sublist L[i:j] (including i, excluding j). 4 | 5 | For example, given L = [1, 2, 3, 4, 5], sum(1, 3) should return sum([2, 3]), which is 5. 6 | 7 | You can assume that you can do some pre-processing. sum() should be optimized over the pre-processing step. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem149/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem149 5 | { 6 | public class Solution 7 | { 8 | private readonly List list; 9 | 10 | public Solution(List list) 11 | { 12 | this.list = list; 13 | } 14 | 15 | public int GetSublistSum(int start, int end) => list 16 | .Where((_, index) => index >= start && index < end) 17 | .Sum(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem149/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem149 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetSublistSum_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var list = new List { 1, 2, 3, 4, 5 }; 15 | var start = 1; 16 | var end = 3; 17 | 18 | var expectedResult = 5; 19 | 20 | var sut = new Solution(list); 21 | 22 | // Act 23 | var actualResult = sut.GetSublistSum(start, end); 24 | 25 | // Assert 26 | actualResult.Should().Be(expectedResult); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem150/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by LinkedIn. 2 | 3 | Given a list of points, a central point, and an integer k, find the nearest k points from the central point. 4 | 5 | For example, given the list of points [(0, 0), (5, 4), (3, 1)], the central point (1, 2), and k = 2, return [(0, 0), (3, 1)]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem150/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem150 6 | { 7 | public class Solution 8 | { 9 | public static IEnumerable<(int x, int y)> FindNearestKPoints(IEnumerable<(int x, int y)> points, 10 | (int x, int y) center, int k) 11 | { 12 | return points 13 | .OrderBy(p => GetDistance(p, center)) 14 | .Take(k) 15 | .ToList(); 16 | } 17 | 18 | private static double GetDistance((int x, int y) point, (int x, int y) otherPoint) 19 | { 20 | var distance = Math.Pow(point.x - otherPoint.x, 2) + Math.Pow(point.y - otherPoint.y, 2); 21 | 22 | distance = Math.Sqrt(distance); 23 | 24 | return distance; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem150/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem150 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestFindNearestKPoints_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var points = new List<(int x, int y)> 14 | { 15 | (0, 0), (5, 4), (3, 1) 16 | }; 17 | 18 | var center = (1, 2); 19 | var k = 2; 20 | 21 | var expectedResult = new List<(int x, int y)> 22 | { 23 | (0,0),(3,1) 24 | }; 25 | 26 | // Act 27 | var result = Solution.FindNearestKPoints(points, center, k); 28 | 29 | // Assert 30 | CollectionAssert.AreEqual(expectedResult, result); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem152/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Triplebyte. 2 | 3 | You are given n numbers as well as n probabilities that sum up to 1. Write a function to generate one of the numbers with its corresponding probability. 4 | 5 | For example, given the numbers [1, 2, 3, 4] and probabilities [0.1, 0.5, 0.2, 0.2], your function should return 1 10% of the time, 2 50% of the time, and 3 and 4 20% of the time. 6 | 7 | You can generate random numbers between 0 and 1 uniformly. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem153/README.md: -------------------------------------------------------------------------------- 1 | Find an efficient algorithm to find the smallest distance (measured in number of words) between any two given words in a string. 2 | 3 | For example, given words "hello", and "world" and a text content of "dog cat hello cat dog dog hello cat world", return 1 because there's only one word "cat" in between the two words. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem153/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem153 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [TestCase("dog cat hello cat dog dog hello cat world", "hello", "world", 1)] 9 | public void TestFindMajorityElement_ShouldReturnCorrectly(string text, string word, string otherWord, int expectedResult) 10 | { 11 | // Arrange, Act 12 | var result = Solution.FindSmallestDistanceBetweenWords(text, word, otherWord); 13 | 14 | // Assert 15 | Assert.AreEqual(expectedResult, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem155/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by MongoDB. 2 | 3 | Given a list of elements, find the majority element, which appears more than half the time (> floor(len(lst) / 2.0)). 4 | 5 | You can assume that such element exists. 6 | 7 | For example, given [1, 2, 1, 1, 3, 4, 0], return 1. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem155/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem155 4 | { 5 | public class Solution 6 | { 7 | public static int FindMajorityElement(int[] numbers) 8 | { 9 | return numbers 10 | .GroupBy(n => n) 11 | .OrderByDescending(g => g.Count()) 12 | .First() 13 | .Key; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem155/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem155 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TestFindMajorityElement_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var numbers = new int[] { 1, 2, 1, 1, 3, 4, 0 }; 13 | var expectedResult = 1; 14 | 15 | // Act 16 | var actual = Solution.FindMajorityElement(numbers); 17 | 18 | // Assert 19 | Assert.AreEqual(expectedResult, actual); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem158/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Slack. 2 | 3 | You are given an N by M matrix of 0s and 1s. Starting from the top left corner, how many ways are there to reach the bottom right corner? 4 | 5 | You can only move right and down. 0 represents an empty space while 1 represents a wall you cannot walk through. 6 | 7 | For example, given the following matrix: 8 | 9 | [[0, 0, 1], 10 | [0, 0, 1], 11 | [1, 0, 0]] 12 | Return two, as there are only two ways to get to the bottom right: 13 | 14 | Right, down, down, right 15 | Down, right, down, right 16 | The top left corner and bottom right corner will always be 0. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem158/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem158 5 | { 6 | public class Solution 7 | { 8 | public static int GetWaysToReachBottomRight(int[][] matrix, int rows, int cols) 9 | { 10 | var ways = Enumerable.Range(0, rows) 11 | .Select(_ => Enumerable.Range(0, cols).Select(__ => 0).ToList()) 12 | .ToList(); 13 | 14 | Traverse(matrix, rows, cols, 0, 0, ways); 15 | 16 | return ways[rows - 1][cols - 1]; 17 | } 18 | 19 | private static void Traverse(int[][] matrix, int rows, int cols, int currentRow, int currentCol, List> ways) 20 | { 21 | if (currentRow == rows || currentCol == cols) 22 | { 23 | return; 24 | } 25 | 26 | if (matrix[currentRow][currentCol] == 1) 27 | { 28 | return; 29 | } 30 | 31 | ways[currentRow][currentCol]++; 32 | 33 | Traverse(matrix, rows, cols, currentRow + 1, currentCol, ways); 34 | Traverse(matrix, rows, cols, currentRow, currentCol + 1, ways); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem158/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem158 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestGetWaysToReachBottomRight_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var input = new[] 14 | { 15 | new[] {0, 0, 1}, 16 | new[] {0, 0, 1}, 17 | new[] {1, 0, 0} 18 | }; 19 | 20 | var rows = 3; 21 | var cols = 3; 22 | 23 | var expectedResult = 2; 24 | 25 | // Act 26 | var actualResult = Solution.GetWaysToReachBottomRight(input, rows, cols); 27 | 28 | // Assert 29 | actualResult.Should().Be(expectedResult); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem159/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given a string, return the first recurring character in it, or null if there is no recurring character. 4 | 5 | For example, given the string "acbbac", return "b". Given the string "abcdef", return null. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem159/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem159 2 | { 3 | public class Solution 4 | { 5 | public static char? FindFirstRecurringCharacter(string input) 6 | { 7 | if (input.Length < 2) 8 | { 9 | return null; 10 | } 11 | 12 | var previous = input[0]; 13 | for (var i = 1; i < input.Length; i++) 14 | { 15 | var current = input[i]; 16 | 17 | if (previous == current) 18 | { 19 | return current; 20 | } 21 | 22 | previous = current; 23 | } 24 | 25 | return null; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/140-159/Problem159/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem159 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TestFindFirstRecurringCharacter_ThereIsRecurringCharacter_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = "acbbac"; 13 | var expectedResult = 'b'; 14 | 15 | // Act 16 | var result = Solution.FindFirstRecurringCharacter(input); 17 | 18 | // Assert 19 | Assert.AreEqual(expectedResult, result); 20 | } 21 | 22 | [Test] 23 | public void TestFindFirstRecurringCharacter_ThereIsNoRecurringCharacter_ShouldReturnNull() 24 | { 25 | // Arrange 26 | var input = "abcdef"; 27 | 28 | // Act 29 | var result = Solution.FindFirstRecurringCharacter(input); 30 | 31 | // Assert 32 | Assert.IsNull(result); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem161/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Given a 32-bit integer, return the number with its bits reversed. 4 | 5 | For example, given the binary number 1111 0000 1111 0000 1111 0000 1111 0000, return 0000 1111 0000 1111 0000 1111 0000 1111. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem161/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem161 5 | { 6 | public class Solution 7 | { 8 | public static string GetReversedBitRepresentation(uint number) 9 | { 10 | return string.Join(string.Empty, Convert.ToString(number, 2) 11 | .Reverse()); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem163/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Jane Street. 2 | 3 | Given an arithmetic expression in [Reverse Polish Notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation), write a program to evaluate it. 4 | 5 | The expression is given as a list of numbers and operands. For example: [5, 3, '+'] should return 5 + 3 = 8. 6 | 7 | For example, [15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-'] should return 5, since it is equivalent to ((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1)) = 5. 8 | 9 | You can assume the given expression is always valid. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem163/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem163 4 | { 5 | public class Solution 6 | { 7 | public static int EvaluateReversePolishNotation(object[] items) 8 | { 9 | var stack = new Stack(); 10 | 11 | foreach (var item in items) 12 | { 13 | if (item is int number) 14 | { 15 | stack.Push(number); 16 | } 17 | 18 | if (item is char @operator) 19 | { 20 | var b = stack.Pop(); 21 | var a = stack.Pop(); 22 | 23 | var result = DoOperation(a, b, @operator); 24 | 25 | stack.Push(result); 26 | } 27 | } 28 | 29 | return stack.Pop(); 30 | } 31 | 32 | private static int DoOperation(int a, int b, char @operator) 33 | { 34 | if (@operator == '+') 35 | { 36 | return a + b; 37 | } 38 | 39 | if (@operator == '-') 40 | { 41 | return a - b; 42 | } 43 | 44 | if (@operator == '*') 45 | { 46 | return a * b; 47 | } 48 | 49 | if (@operator == '/') 50 | { 51 | return a / b; 52 | } 53 | 54 | return 0; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem163/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem163 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [TestCase(8, new object[] { 5, 3, '+' })] 9 | [TestCase(5, new object[] { 15, 7, 1, 1, '+', '-', '/', 3, '*', 2, 1, 1, '+', '+', '-' })] 10 | public void TestEvaluateReversePolishNotation_ShouldReturnCorrectly(int expectedResult, object[] items) 11 | { 12 | // Arrange, Act 13 | var result = Solution.EvaluateReversePolishNotation(items); 14 | 15 | // Assert 16 | Assert.AreEqual(expectedResult, result); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem164/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem164/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem164 4 | { 5 | public class Solution 6 | { 7 | public static int FindDuplicate(int[] array) 8 | { 9 | var set = new HashSet(); 10 | 11 | foreach (var number in array) 12 | { 13 | if (set.Contains(number)) 14 | { 15 | return number; 16 | } 17 | 18 | set.Add(number); 19 | } 20 | 21 | return 0; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem164/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem164 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TestFindDuplicate_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = new[] { 1, 2, 3, 4, 4 }; 13 | var expectedResult = 4; 14 | 15 | // Act 16 | var result = Solution.FindDuplicate(input); 17 | 18 | // Assert 19 | Assert.AreEqual(expectedResult, result); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem166/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Uber. 2 | 3 | Implement a 2D iterator class. It will be initialized with an array of arrays, and should implement the following methods: 4 | 5 | next(): returns the next element in the array of arrays. If there are no more elements, raise an exception. 6 | has_next(): returns whether or not the iterator still has elements left. 7 | For example, given the input [[1, 2], [3], [], [4, 5, 6]], calling next() repeatedly should output 1, 2, 3, 4, 5, 6. 8 | 9 | Do not use flatten or otherwise clone the arrays. Some of the arrays can be empty. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem166/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem166 6 | { 7 | public class TwoDIterator : IEnumerable 8 | { 9 | private readonly int[][] arrays; 10 | 11 | public TwoDIterator(int[][] arrays) 12 | { 13 | this.arrays = arrays; 14 | } 15 | 16 | public IEnumerator GetEnumerator() 17 | { 18 | foreach (var array in arrays) 19 | { 20 | foreach (var element in array) 21 | { 22 | yield return element; 23 | } 24 | } 25 | } 26 | 27 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem166/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using FluentAssertions; 4 | using NUnit.Framework; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem166 7 | { 8 | [TestFixture] 9 | public class Tests 10 | { 11 | [Test] 12 | public void Test_ShouldReturnCorrectly() 13 | { 14 | // Arrange 15 | var input = new[] 16 | { 17 | new int[] {1, 2}, 18 | new int[] {3}, 19 | new int[] { }, 20 | new int[] {4, 5, 6}, 21 | }; 22 | 23 | var iterator = new TwoDIterator(input); 24 | 25 | var expectedResult = new List {1, 2, 3, 4, 5, 6}; 26 | 27 | // Act 28 | var actualResult = iterator.ToList(); 29 | 30 | // Assert 31 | actualResult.Should().BeEquivalentTo(expectedResult); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem167/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Airbnb. 2 | 3 | Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome. 4 | 5 | For example, given the list ["code", "edoc", "da", "d"], return [(0, 1), (1, 0), (2, 3)]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/160-179/Problem167/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem167 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetPairsOfUniqueIndicesWithConcatenationOfWordsIsPalindrome_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var input = new List { "code", "edoc", "da", "d" }; 15 | var expectedResult = new List<(int first, int second)> { (0, 1), (1, 0), (2, 3) }; 16 | 17 | // Act 18 | var actualResult = Solution.GetPairsOfUniqueIndicesWithConcatenationOfWordsIsPalindrome(input); 19 | 20 | // Assert 21 | actualResult.Should().BeEquivalentTo(expectedResult); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem181/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Given a string, split it into as few strings as possible such that each string is a palindrome. 4 | 5 | For example, given the input string racecarannakayak, return ["racecar", "anna", "kayak"]. 6 | 7 | Given the input string abc, return ["a", "b", "c"]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem181/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem181 4 | { 5 | public class Solution 6 | { 7 | public static IEnumerable SplitToFewestPalindromes(string input) 8 | { 9 | var result = new List(); 10 | 11 | var currentString = input; 12 | 13 | var currentLength = currentString.Length; 14 | 15 | while (!string.IsNullOrEmpty(currentString)) 16 | { 17 | var current = currentString.Substring(0, currentLength); 18 | 19 | if (IsPalindrome(current)) 20 | { 21 | result.Add(current); 22 | currentString = currentString.Remove(0, currentLength); 23 | currentLength = currentString.Length; 24 | } 25 | else 26 | { 27 | currentLength--; 28 | } 29 | } 30 | 31 | return result; 32 | } 33 | 34 | private static bool IsPalindrome(string input) 35 | { 36 | for (int i = 0; i <= input.Length / 2; i++) 37 | { 38 | if (input[i] != input[input.Length - i - 1]) 39 | { 40 | return false; 41 | } 42 | } 43 | 44 | return true; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem181/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem181 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [TestCase("racecarannakayak", new[] { "racecar", "anna", "kayak" })] 11 | [TestCase("abc", new[] { "a", "b", "c" })] 12 | public void TestSplitToFewestPalindromes_ShouldReturnCorrectly(string input, IEnumerable expectedResult) 13 | { 14 | // Arrange, Act 15 | var actualResult = Solution.SplitToFewestPalindromes(input); 16 | 17 | // Assert 18 | actualResult.Should().BeEquivalentTo(expectedResult); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem184/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Amazon. 2 | 3 | Given n numbers, find the greatest common denominator between them. 4 | 5 | For example, given the numbers [42, 56, 14], return 14. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem184/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem184 4 | { 5 | public class Solution 6 | { 7 | public static int FindGreatestCommonDenominator(int[] numbers) 8 | { 9 | var greatestDenominator = numbers.Min(); 10 | 11 | while (greatestDenominator > 0) 12 | { 13 | if (IsDenominator(numbers, greatestDenominator)) 14 | { 15 | return greatestDenominator; 16 | } 17 | } 18 | 19 | return greatestDenominator; 20 | } 21 | 22 | private static bool IsDenominator(int[] numbers, int number) 23 | { 24 | return numbers 25 | .All(n => n % number == 0); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem184/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem184 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TesFindGreatestCommonDenominator_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = new[] { 42, 56, 14}; 13 | var expectedResult = 14; 14 | 15 | // Act 16 | var result = Solution.FindGreatestCommonDenominator(input); 17 | 18 | // Assert 19 | Assert.AreEqual(expectedResult, result); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem195/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Let A be an N by M matrix in which every row and every column is sorted. 4 | 5 | Given i1, j1, i2, and j2, compute the number of elements of M smaller than M[i1, j1] and larger than M[i2, j2]. 6 | 7 | For example, given the following matrix: 8 | 9 | [[1, 3, 7, 10, 15, 20], 10 | [2, 6, 9, 14, 22, 25], 11 | [3, 8, 10, 15, 25, 30], 12 | [10, 11, 12, 23, 30, 35], 13 | [20, 25, 30, 35, 40, 45]] 14 | 15 | And i1 = 1, j1 = 1, i2 = 3, j2 = 3, return 15 as there are 15 numbers in the matrix smaller than 6 or greater than 23. 16 | 17 | --------------------------------- 18 | Actually, the given matrix has 14 numbers, smaller than 6 or greater than 23 (1, 3, 2, 25, 3, 25, 30, 30, 35, 25, 30, 35, 40, 45) -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem195/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem195 2 | { 3 | public class Solution 4 | { 5 | public static int FindNumberOfElementsBetween(int[][] matrix, int i1, int j1, int i2, int j2) 6 | { 7 | var itemsCount = 0; 8 | 9 | for (int i = 0; i < matrix.Length; i++) 10 | { 11 | var col = 0; 12 | 13 | while (matrix[i][col] < matrix[i1][j1]) 14 | { 15 | itemsCount++; 16 | col++; 17 | } 18 | 19 | col = matrix[i].Length - 1; 20 | 21 | while (matrix[i][col] > matrix[i2][j2]) 22 | { 23 | itemsCount++; 24 | col--; 25 | } 26 | } 27 | 28 | return itemsCount; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/100-199/180-199/Problem195/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem195 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TesFindNumberOfElementsBetween_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = new[] { 13 | new int[] {1, 3, 7, 10, 15, 20 }, 14 | new int[] {2, 6, 9, 14, 22, 25 }, 15 | new int[] { 3, 8, 10, 15, 25, 30}, 16 | new int[] {10, 11, 12, 23, 30, 35 }, 17 | new int[] {20, 25, 30, 35, 40, 45 }, 18 | }; 19 | 20 | var i1 = 1; 21 | var j1 = 1; 22 | var i2 = 3; 23 | var j2 = 3; 24 | 25 | var expectedResult = 14; 26 | 27 | // Act 28 | var result = Solution.FindNumberOfElementsBetween(input, i1, j1, i2, j2); 29 | 30 | // Assert 31 | Assert.AreEqual(expectedResult, result); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/200-219/202/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Palantir. 2 | 3 | Write a program that checks whether an integer is a palindrome. For example, 121 is a palindrome, as well as 888. 678 is not a palindrome. Do not convert the integer into a string. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/200-219/202/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem202 5 | { 6 | public class Solution 7 | { 8 | public static bool IsPalindrome(int number) 9 | { 10 | var queue = new Queue(); 11 | var stack = new Stack(); 12 | 13 | while (number > 0) 14 | { 15 | var nextDigit = number % 10; 16 | 17 | queue.Enqueue(nextDigit); 18 | stack.Push(nextDigit); 19 | 20 | number /= 10; 21 | } 22 | 23 | while (queue.Any() && stack.Any()) 24 | { 25 | if (queue.Dequeue() != stack.Pop()) 26 | { 27 | return false; 28 | } 29 | } 30 | 31 | return true; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/200-219/202/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem202 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase(121)] 10 | [TestCase(888)] 11 | public void TestIsPalindrome_IsPalindrome_ShouldReturnTrue(int number) 12 | { 13 | // Arrange, Act 14 | var actualResult = Solution.IsPalindrome(number); 15 | 16 | // Assert 17 | actualResult.Should().BeTrue(); 18 | } 19 | 20 | [TestCase(678)] 21 | public void TestIsPalindrome_IsNotPalindrome_ShouldReturnFalse(int number) 22 | { 23 | // Arrange, Act 24 | var actualResult = Solution.IsPalindrome(number); 25 | 26 | // Assert 27 | actualResult.Should().BeFalse(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/200-219/208/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by LinkedIn. 2 | 3 | Given a linked list of numbers and a pivot k, partition the linked list so that all nodes less than k come before nodes greater than or equal to k. 4 | 5 | For example, given the linked list 5 -> 1 -> 8 -> 0 -> 3 and k = 3, the solution could be 1 -> 0 -> 5 -> 8 -> 3. 6 | 7 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/200-219/208/Tests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem208 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestPartitionList_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var list = new LinkedList(); 15 | list.Add(5); 16 | list.Add(1); 17 | list.Add(8); 18 | list.Add(0); 19 | list.Add(3); 20 | 21 | var k = 3; 22 | 23 | var expectedResult = new LinkedList(); 24 | expectedResult.Add(1); 25 | expectedResult.Add(0); 26 | expectedResult.Add(5); 27 | expectedResult.Add(8); 28 | expectedResult.Add(3); 29 | 30 | // Act 31 | var actualResult = Solution.PartitionList(list, k); 32 | 33 | // Assert 34 | actualResult.Should().BeEquivalentTo(expectedResult); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/226/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Airbnb. 2 | 3 | You come across a dictionary of sorted words in a language you've never seen before. Write a program that returns the correct order of letters in this language. 4 | 5 | For example, given ['xww', 'wxyz', 'wxyw', 'ywx', 'ywz'], you should return ['x', 'z', 'w', 'y']. 6 | 7 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/226/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Collections.Specialized; 3 | using FluentAssertions; 4 | using NUnit.Framework; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem226 7 | { 8 | [TestFixture] 9 | public class Tests 10 | { 11 | [Test] 12 | public void TestGetOrderedLetters_ShouldReturnCorrectly() 13 | { 14 | // Arrange 15 | var orderedWords = new List { "xww", "wxyz", "wxyw", "ywx", "ywz" }; 16 | 17 | var expectedLetters = new List { 'x', 'z', 'w', 'y' }; 18 | 19 | // Act 20 | var actual = Solution.GetOrderedLetters(orderedWords); 21 | 22 | // Assert 23 | actual.Should().BeEquivalentTo(expectedLetters); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/237/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Amazon. 2 | 3 | A tree is symmetric if its data and shape remain unchanged when it is reflected about the root node. The following tree is an example: 4 | 5 | 4 6 | / | \ 7 | 3 5 3 8 | / \ 9 | 9 9 10 | Given a k-ary tree, determine whether it is symmetric. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/237/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem237 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestIsTreeSymmetric_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var k = 3; 14 | 15 | var root = new Solution.TreeNode(4, 16 | new Solution.TreeNode(3, new Solution.TreeNode(9)), 17 | new Solution.TreeNode(5), 18 | new Solution.TreeNode(3, new Solution.TreeNode(9)) 19 | ); 20 | 21 | // Act 22 | var result = Solution.IsTreeSymmetric(root, k); 23 | 24 | // Assert 25 | result.Should().BeTrue(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/Problem232/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Implement a PrefixMapSum class with the following methods: 4 | 5 | `insert(key: str, value: int)`: Set a given key's value in the map. If the key already exists, overwrite the value. 6 | `sum(prefix: str)`: Return the sum of all values of keys that begin with a given prefix. 7 | 8 | For example, you should be able to run the following code: 9 | 10 | ``` 11 | mapsum.insert("columnar", 3) 12 | assert mapsum.sum("col") == 3 13 | 14 | mapsum.insert("column", 2) 15 | assert mapsum.sum("col") == 5 16 | ``` -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/Problem232/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem232 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TestSum_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var mapsum = new Solution(); 13 | mapsum.Insert("columnar", 3); 14 | mapsum.Insert("column", 2); 15 | 16 | var expectedResult = 5; 17 | 18 | // Act 19 | var sum = mapsum.Sum("col"); 20 | 21 | // Assert 22 | Assert.AreEqual(expectedResult, sum); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/Problem233/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Implement a PrefixMapSum class with the following methods: 4 | 5 | `insert(key: str, value: int)`: Set a given key's value in the map. If the key already exists, overwrite the value. 6 | `sum(prefix: str)`: Return the sum of all values of keys that begin with a given prefix. 7 | 8 | For example, you should be able to run the following code: 9 | 10 | ``` 11 | mapsum.insert("columnar", 3) 12 | assert mapsum.sum("col") == 3 13 | 14 | mapsum.insert("column", 2) 15 | assert mapsum.sum("col") == 5 16 | ``` -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/Problem233/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem233 2 | { 3 | public class Solution 4 | { 5 | public static int Fibonacci(int n) 6 | { 7 | if (n == 1 || n == 0) 8 | { 9 | return 1; 10 | } 11 | 12 | return Fibonacci(n - 1) + Fibonacci(n - 2); 13 | } 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/220-239/Problem233/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem233 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TestFibonacci_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var expectedResult = 89; 13 | var n = 10; 14 | 15 | // Act 16 | var result = Solution.Fibonacci(n); 17 | 18 | // Assert 19 | Assert.AreEqual(expectedResult, result); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/241/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Palantir. 2 | 3 | In academia, the h-index is a metric used to calculate the impact of a researcher's papers. It is calculated as follows: 4 | 5 | A researcher has index h if at least h of her N papers have h citations each. If there are multiple h satisfying this formula, the maximum is chosen. 6 | 7 | For example, suppose N = 5, and the respective citations of each paper are [4, 3, 0, 1, 5]. Then the h-index would be 3, since the researcher has 3 papers with at least 3 citations. 8 | 9 | Given a list of paper citations of a researcher, calculate their h-index. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/241/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem241 4 | { 5 | public class Solution 6 | { 7 | public static int GetHIndex(int[] paperCitations) 8 | { 9 | var n = paperCitations.Length; 10 | var index = 0; 11 | 12 | for (var i = 1; i <= n; i++) 13 | { 14 | if (HasIndex(i, paperCitations, n)) 15 | { 16 | index = i; 17 | } 18 | } 19 | 20 | return index; 21 | } 22 | 23 | public static bool HasIndex(int index, int[] citations, int n) 24 | { 25 | return citations 26 | .Where(paperCitations => paperCitations >= index) 27 | .ToList() 28 | .Count >= index; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/241/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem241 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [TestCase(3, new[] { 4, 3, 0, 1, 5 })] 9 | public void TestGetHIndex_ShouldReturnCorrectly(int expectedResult, int[] paperCitations) 10 | { 11 | // Arrange, Act 12 | var index = Solution.GetHIndex(paperCitations); 13 | 14 | // Assert 15 | Assert.AreEqual(expectedResult, index); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/245/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Yelp. 2 | 3 | You are given an array of integers, where each element represents the maximum number of steps that can be jumped going forward from that element. Write a function to return the minimum number of jumps you must take in order to get from the start to the end of the array. 4 | 5 | For example, given [6, 2, 4, 0, 5, 1, 1, 4, 2, 9], you should return 2, as the optimal solution involves jumping from 6 to 5, and then from 5 to 9. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/245/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem245 6 | { 7 | public class Solution 8 | { 9 | public static int GetMinimumNumberOfJumpsToEnd(List input) => 10 | GetMinimumNumberOfJumpsToEnd(input, 0, 0); 11 | 12 | private static int GetMinimumNumberOfJumpsToEnd(List input, int currentIndex, int currentJumps) 13 | { 14 | if (currentIndex >= input.Count) 15 | { 16 | return int.MaxValue; 17 | } 18 | 19 | if (currentIndex == input.Count - 1) 20 | { 21 | return currentJumps; 22 | } 23 | 24 | var minJumps = Enumerable.Range(1, input[currentIndex]) 25 | .Select(jump => GetMinimumNumberOfJumpsToEnd(input, currentIndex + jump, currentJumps + 1)) 26 | .ToList(); 27 | 28 | if (!minJumps.Any()) 29 | { 30 | return int.MaxValue; 31 | } 32 | return minJumps.Min(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/245/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem245 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetMinimumNumberOfJumpsToEnd_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var input = new List { 6, 2, 4, 0, 5, 1, 1, 4, 2, 9 }; 15 | var expectedResult = 2; 16 | 17 | // Act 18 | var actualResult = Solution.GetMinimumNumberOfJumpsToEnd(input); 19 | 20 | // Assert 21 | actualResult.Should().Be(expectedResult); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/246/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Dropbox. 2 | 3 | Given a list of words, determine whether the words can be chained to form a circle. A word X can be placed in front of another word Y in a circle if the last character of X is same as the first character of Y. 4 | 5 | For example, the words ['chair', 'height', 'racket', touch', 'tunic'] can form the following circle: chair --> racket --> touch --> height --> tunic --> chair. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/246/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem246 5 | { 6 | public class Solution 7 | { 8 | public static bool CanWordsBeChained(IList words) 9 | { 10 | return words 11 | .Any(word => CanWordsBeChained(words.Where(w => w != word).ToList(), word, word)); 12 | } 13 | 14 | private static bool CanWordsBeChained(IList remainingWords, string currentWord, string firstWord) 15 | { 16 | if (!remainingWords.Any() && currentWord.Last() == firstWord.First()) 17 | { 18 | return true; 19 | } 20 | 21 | var nextPossibleWords = remainingWords 22 | .Where(word => word.StartsWith(currentWord.Last())) 23 | .ToList(); 24 | 25 | return nextPossibleWords 26 | .Any(word => CanWordsBeChained(remainingWords.Where(w => w != word).ToList(), word, firstWord)); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/246/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem246 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestCanWordsBeChained_CanBeChained_ShouldReturnTrue() 11 | { 12 | // Arrange 13 | var words = new List { 14 | "chair", "height", "racket", "touch", "tunic" 15 | }; 16 | 17 | // Act 18 | var result = Solution.CanWordsBeChained(words); 19 | 20 | // Assert 21 | Assert.IsTrue(result); 22 | } 23 | 24 | [Test] 25 | public void TestCanWordsBeChained_CanNotBeChained_ShouldReturnFalse() 26 | { 27 | // Arrange 28 | var words = new List { 29 | "chair", "height", "racket" 30 | }; 31 | 32 | // Act 33 | var result = Solution.CanWordsBeChained(words); 34 | 35 | // Assert 36 | Assert.IsFalse(result); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/Problem258/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Morgan Stanley. 2 | 3 | In Ancient Greece, it was common to write text with the first line going left to right, the second line going right to left, and continuing to go back and forth. This style was called "boustrophedon". 4 | 5 | Given a binary tree, write an algorithm to print the nodes in boustrophedon order. 6 | 7 | For example, given the following tree: 8 | 9 | 1 10 | / \ 11 | 2 3 12 | / \ / \ 13 | 4 5 6 7 14 | You should return [1, 3, 2, 4, 5, 6, 7]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/Problem258/Tests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem258 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestGetBoustrophedonText_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var root = new Node(1, 14 | new Node(2, 15 | new Node(4), 16 | new Node(5)), 17 | new Node(3, 18 | new Node(6), 19 | new Node(7))); 20 | 21 | var expectedResult = new int[] { 1, 3, 2, 4, 5, 6, 7 }; 22 | 23 | // Act 24 | var actual = Solution.GetBoustrophedonText(root); 25 | 26 | // Assert 27 | CollectionAssert.AreEqual(expectedResult, actual); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/Problem259/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Two Sigma. 2 | 3 | Ghost is a two-person word game where players alternate appending letters to a word. The first person who spells out a word, or creates a prefix for which there is no possible continuation, loses. Here is a sample game: 4 | 5 | Player 1: g 6 | Player 2: h 7 | Player 1: o 8 | Player 2: s 9 | Player 1: t [loses] 10 | Given a dictionary of words, determine the letters the first player should start with, such that with optimal play they cannot lose. 11 | 12 | For example, if the dictionary is ["cat", "calf", "dog", "bear"], the only winning start letter would be b. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/Problem259/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using DailyCodingProblem.Solutions.Common; 4 | using Moq; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem259 7 | { 8 | public class Solution 9 | { 10 | public static IEnumerable GetStartLetters(ICollection words) 11 | { 12 | var result = new List(); 13 | 14 | var startLetters = words 15 | .Where(w => w.Length % 2 == 0 && words.Where(word => word[0] == w[0]).ToList().Count == 1) 16 | .Select(w => w[0]); 17 | 18 | result.AddRange(startLetters); 19 | 20 | return result; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/240-259/Problem259/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using DailyCodingProblem.Solutions.Common; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem259 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [TestCase(1, new[] { "cat", "calf", "dog", "bear" }, new[] { 'b' })] 11 | public void TestGetStartLetters_ShouldReturnCorrectly(int id, string[] letters, char[] expectedResult) 12 | { 13 | // Arrange, Act 14 | var actualResult = Solution.GetStartLetters(letters); 15 | 16 | // Assert 17 | CollectionAssert.AreEquivalent(expectedResult, actualResult); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/260/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Pinterest. 2 | 3 | The sequence [0, 1, ..., N] has been jumbled, and the only clue you have for its order is an array representing whether each number is larger or smaller than the last. Given this information, reconstruct an array that is consistent with it. 4 | For example, given [None, +, +, -, +], you could return [1, 2, 3, 0, 4]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/260/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem260 5 | { 6 | public class Solution 7 | { 8 | public const char Greater = '+'; 9 | public const char Lesser = '-'; 10 | 11 | public static int[] ReconstructArray(char[] clues) 12 | { 13 | var n = clues.Length; 14 | 15 | var firstNumber = clues 16 | .Count(c => c == Lesser); 17 | 18 | var result = new int[n]; 19 | 20 | result[0] = firstNumber; 21 | var greaterNumber = firstNumber + 1; 22 | var lesserNumber = 0; 23 | 24 | for (int i = 1; i < n; i++) 25 | { 26 | if (clues[i] == Greater) 27 | { 28 | result[i] = greaterNumber++; 29 | } 30 | else 31 | { 32 | result[i] = lesserNumber++; 33 | } 34 | } 35 | 36 | return result; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/260/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem260 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TestReconstructArray_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var clues = new[] 13 | { 14 | ' ', '+', '+', '-', '+' 15 | }; 16 | 17 | // Act 18 | var reconstruction = Solution.ReconstructArray(clues); 19 | 20 | // Assert 21 | Assert.IsTrue(IsReconstructionValid(clues, reconstruction)); 22 | } 23 | 24 | private bool IsReconstructionValid(char[] clues, int[] reconstruction) 25 | { 26 | for (int i = 1; i < reconstruction.Length; i++) 27 | { 28 | if (clues[i] == Solution.Greater && reconstruction[i] <= reconstruction[0]) 29 | { 30 | return false; 31 | } 32 | 33 | if (clues[i] == Solution.Lesser && reconstruction[i] >= reconstruction[0]) 34 | { 35 | return false; 36 | } 37 | } 38 | 39 | return true; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/263/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Nest. 2 | 3 | Create a basic sentence checker that takes in a stream of characters and determines whether they form valid sentences. If a sentence is valid, the program should print it out. 4 | 5 | We can consider a sentence valid if it conforms to the following rules: 6 | 7 | - The sentence must start with a capital letter, followed by a lowercase letter or a space. 8 | - All other characters must be lowercase letters, separators (',', ';', ':') or terminal marks ('.', '?', '!'). 9 | - There must be a single space between each word. 10 | - The sentence must end with a terminal mark immediately following a word. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/267/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Oracle. 2 | 3 | You are presented with an 8 by 8 matrix representing the positions of pieces on a chess board. The only pieces on the board are the black king and various white pieces. Given this matrix, determine whether the king is in check. 4 | 5 | For details on how each piece moves, see here. 6 | 7 | For example, given the following matrix: 8 | 9 | ...K.... 10 | ........ 11 | .B...... 12 | ......P. 13 | .......R 14 | ..N..... 15 | ........ 16 | .....Q.. 17 | 18 | You should return True, since the bishop is attacking the king diagonally. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/268/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Indeed. 2 | 3 | Given a 32-bit positive integer N, determine whether it is a power of four in faster than O(log N) time. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/268/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem268 4 | { 5 | public class Solution 6 | { 7 | public static bool IsPowerOf(int number, int poweredNumber) 8 | { 9 | return Math.Log(number, poweredNumber) % 1 == 0; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/268/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem268 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [TestCase(16, 4)] 9 | [TestCase(64, 4)] 10 | [TestCase(256, 4)] 11 | public void TestIsPowerOf_NumberIsPower_ShouldReturnTrue(int number, int poweredNumber) 12 | { 13 | // Arrange, Act 14 | var result = Solution.IsPowerOf(number, poweredNumber); 15 | 16 | // Assert 17 | Assert.IsTrue(result); 18 | } 19 | 20 | [TestCase(32, 4)] 21 | [TestCase(128, 4)] 22 | [TestCase(11, 4)] 23 | public void TestIsPowerOf_NumberIsNotPower_ShouldReturnFalse(int number, int poweredNumber) 24 | { 25 | // Arrange, Act 26 | var result = Solution.IsPowerOf(number, poweredNumber); 27 | 28 | // Assert 29 | Assert.IsFalse(result); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/270/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Twitter. 2 | 3 | A network consists of nodes labeled 0 to N. You are given a list of edges (a, b, t), describing the time t it takes for a message to be sent from node a to node b. Whenever a node receives a message, it immediately passes the message on to a neighboring node, if possible. 4 | 5 | Assuming all nodes are connected, determine how long it will take for every node to receive a message that begins at node 0. 6 | 7 | For example, given N = 5, and the following edges: 8 | 9 | edges = [ 10 | (0, 1, 5), 11 | (0, 2, 3), 12 | (0, 5, 4), 13 | (1, 3, 8), 14 | (2, 3, 1), 15 | (3, 5, 10), 16 | (3, 4, 5) 17 | ] 18 | You should return 9, because propagating the message from 0 -> 2 -> 3 -> 4 will take that much time. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/270/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem270 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetMessagePropagationTime_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var edges = new List<(int start, int finish, int time)> 15 | { 16 | (0, 1, 5), 17 | (0, 2, 3), 18 | (0, 5, 4), 19 | (1, 3, 8), 20 | (2, 3, 1), 21 | (3, 5, 10), 22 | (3, 4, 5) 23 | }; 24 | var n = 5; 25 | 26 | var expectedResult = 9; 27 | 28 | // Act 29 | var actualResult = Solution.GetMessagePropagationTime(edges, n); 30 | 31 | // Assert 32 | actualResult.Should().Be(expectedResult); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/273/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Apple. 2 | 3 | A fixed point in an array is an element whose value is equal to its index. Given a sorted array of distinct elements, return a fixed point, if one exists. Otherwise, return False. 4 | 5 | For example, given [-6, 0, 2, 40], you should return 2. Given [1, 5, 7, 8], you should return False. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/273/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem273 4 | { 5 | public class Solution 6 | { 7 | public static int? GetFixedPoint(int[] input) 8 | { 9 | var fixedPoints = input 10 | .Where((element, index) => element == index) 11 | .ToList(); 12 | 13 | if (fixedPoints.Any()) 14 | { 15 | return fixedPoints[0]; 16 | } 17 | 18 | return null; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/273/Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem273 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestGetFixedPoint_ThereIsFixedPoint_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var input = new[] { -6, 0, 2, 40 }; 14 | var expectedResult = 2; 15 | 16 | // Act 17 | var actual = Solution.GetFixedPoint(input); 18 | 19 | // Assert 20 | Assert.AreEqual(expectedResult, actual); 21 | } 22 | 23 | [Test] 24 | public void TestGetFixedPoint_ThereIsNoFixedPoint_ShouldReturnNull() 25 | { 26 | // Arrange 27 | var input = new[] { 1, 5, 7, 8 }; 28 | 29 | // Act 30 | var actual = Solution.GetFixedPoint(input); 31 | 32 | // Assert 33 | Assert.IsNull(actual); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/274/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Facebook. 2 | 3 | Given a string consisting of parentheses, single digits, and positive and negative signs, convert the string into a mathematical expression to obtain the answer. 4 | 5 | Don't use eval or a similar built-in parser. 6 | 7 | For example, given '-1 + (2 + 3)', you should return 4. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/274/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem274 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase("-1 + (2 + 3)", 4)] 10 | [TestCase("(-1 + (2 + 3)) + 5 - (3 + 7)", -1)] 11 | public void TestEval_ShouldReturnCorrectly(string input, int expectedResult) 12 | { 13 | // Arrange, Act 14 | var actualResult = Solution.Eval(input); 15 | 16 | // Assert 17 | actualResult.Should().Be(expectedResult); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/275/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Epic. 2 | 3 | The "look and say" sequence is defined as follows: beginning with the term 1, each subsequent term visually describes the digits appearing in the previous term. The first few terms are as follows: 4 | 5 | 1 6 | 11 7 | 21 8 | 1211 9 | 111221 10 | As an example, the fourth term is 1211, since the third term consists of one 2 and one 1. 11 | 12 | Given an integer N, print the Nth term of this sequence. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/260-279/275/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem275 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [TestCase(1, "1")] 9 | [TestCase(4, "1211")] 10 | [TestCase(5, "111221")] 11 | public void TestGetNthTermOfSequence_ShouldReturnCorrectly(int n, string expectedResult) 12 | { 13 | // Arrange, Act 14 | var actual = Solution.GetNthTermOfSequence(n); 15 | 16 | // Assert 17 | Assert.AreEqual(expectedResult, actual); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/282/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Netflix. 2 | 3 | Given an array of integers, determine whether it contains a Pythagorean triplet. Recall that a Pythogorean triplet (a, b, c) is defined by the equation a^2+ b^2= c^2. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/282/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem282 4 | { 5 | public class Solution 6 | { 7 | public static bool ContainsPythagoreanTriplet(int[] input) 8 | { 9 | return input 10 | .Any(a => 11 | input.Any(b => 12 | input.Any(c => (a * a + b * b) == (c * c)))); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/282/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem282 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void TestContainsPythagoreanTriplet_Contains_ShouldReturnTrue() 10 | { 11 | // Arrange 12 | var input = new[] { 3, 4, 5, 12, 255 }; 13 | 14 | // Act 15 | var result = Solution.ContainsPythagoreanTriplet(input); 16 | 17 | // Assert 18 | Assert.IsTrue(result); 19 | } 20 | 21 | [Test] 22 | public void TestContainsPythagoreanTriplet_DoesNotContain_ShouldReturnFalse() 23 | { 24 | // Arrange 25 | var input = new[] { 3, 12, 255 }; 26 | 27 | // Act 28 | var result = Solution.ContainsPythagoreanTriplet(input); 29 | 30 | // Assert 31 | Assert.IsFalse(result); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/292/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Twitter. 2 | 3 | A teacher must divide a class of students into two teams to play dodgeball. Unfortunately, not all the kids get along, and several refuse to be put on the same team as that of their enemies. 4 | 5 | Given an adjacency list of students and their enemies, write an algorithm that finds a satisfactory pair of teams, or returns False if none exists. 6 | 7 | For example, given the following enemy graph you should return the teams {0, 1, 4, 5} and {2, 3}. 8 | 9 | students = { 10 | 0: [3], 11 | 1: [2], 12 | 2: [1, 4], 13 | 3: [0, 4, 5], 14 | 4: [2, 3], 15 | 5: [3] 16 | } 17 | On the other hand, given the input below, you should return False. 18 | 19 | students = { 20 | 0: [3], 21 | 1: [2], 22 | 2: [1, 3, 4], 23 | 3: [0, 2, 4, 5], 24 | 4: [2, 3], 25 | 5: [3] 26 | } -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/292/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem292 5 | { 6 | public class Solution 7 | { 8 | public static IEnumerable> FindSatisfactoryPairOfTeams(IList> input) 9 | { 10 | var firstTeam = new List(); 11 | var secondTeam = new List(); 12 | 13 | for (var i = 0; i < input.Count; i++) 14 | { 15 | var studentEnemies = input[i]; 16 | 17 | if (!firstTeam.Any(student => studentEnemies.Contains(student))) 18 | { 19 | firstTeam.Add(i); 20 | } 21 | else if (!secondTeam.Any(student => studentEnemies.Contains(student))) 22 | { 23 | secondTeam.Add(i); 24 | } 25 | else 26 | { 27 | return null; 28 | } 29 | } 30 | 31 | return new List> { firstTeam, secondTeam }; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/295/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Stitch Fix. 2 | 3 | Pascal's triangle is a triangular array of integers constructed with the following formula: 4 | 5 | The first row consists of the number 1. 6 | For each subsequent row, each element is the sum of the numbers directly above it, on either side. 7 | For example, here are the first few rows: 8 | 9 | 1 10 | 1 1 11 | 1 2 1 12 | 1 3 3 1 13 | 1 4 6 4 1 14 | Given an input k, return the kth row of Pascal's triangle. 15 | 16 | Bonus: Can you do this using only O(k) space? -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/295/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem295 4 | { 5 | public class Solution 6 | { 7 | public static int[] GetKthRow(int k) 8 | { 9 | var result = new int[k]; 10 | result[0] = 1; 11 | 12 | for (var row = 1; row < k; row++) 13 | { 14 | result[row] = 1; 15 | for (var i = row - 1; i > 0; i--) 16 | { 17 | result[i] = result[i] + result[i - 1]; 18 | } 19 | } 20 | 21 | return result; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/200-299/280-299/295/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem295 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [TestCase(1, new[] { 1 })] 9 | [TestCase(2, new[] { 1, 1 })] 10 | [TestCase(3, new[] { 1, 2, 1 })] 11 | [TestCase(4, new[] { 1, 3, 3, 1 })] 12 | [TestCase(5, new[] { 1, 4, 6, 4, 1 })] 13 | public void TestGetKthRow_ShouldReturnCorrectly(int k, int[] expectedResult) 14 | { 15 | // Arrange, Act 16 | var result = Solution.GetKthRow(k); 17 | 18 | // Assert 19 | CollectionAssert.AreEqual(expectedResult, result); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/313/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Citrix. 2 | 3 | You are given a circular lock with three wheels, each of which display the numbers 0 through 9 in order. Each of these wheels rotate clockwise and counterclockwise. 4 | 5 | In addition, the lock has a certain number of "dead ends", meaning that if you turn the wheels to one of these combinations, the lock becomes stuck in that state and cannot be opened. 6 | 7 | Let us consider a "move" to be a rotation of a single wheel by one digit, in either direction. Given a lock initially set to 000, a target combination, and a list of dead ends, write a function that returns the minimum number of moves required to reach the target state, or None if this is impossible. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/314/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Spotify. 2 | 3 | You are the technical director of WSPT radio, serving listeners nationwide. For simplicity's sake we can consider each listener to live along a horizontal line stretching from 0 (west) to 1000 (east). 4 | 5 | Given a list of N listeners, and a list of M radio towers, each placed at various locations along this line, determine what the minimum broadcast range would have to be in order for each listener's home to be covered. 6 | 7 | For example, suppose listeners = [1, 5, 11, 20], and towers = [4, 8, 15]. In this case the minimum range would be 5, since that would be required for the tower at position 15 to reach the listener at position 20. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/314/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem314 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetMinimumBroadcastRange_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var listeners= new List { 1, 5, 11, 20 }; 15 | var towers= new List { 4, 8, 15 }; 16 | var expectedMinimumRange = 5; 17 | 18 | // Act 19 | var actualMinimumRange = Solution.GetMinimumBroadcastRange(listeners, towers); 20 | 21 | // Assert 22 | actualMinimumRange.Should().Be(expectedMinimumRange); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/315/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | In linear algebra, a Toeplitz matrix is one in which the elements on any given diagonal from top left to bottom right are identical. 4 | 5 | Here is an example: 6 | 7 | 1 2 3 4 8 8 | 5 1 2 3 4 9 | 4 5 1 2 3 10 | 7 4 5 1 2 11 | 12 | Write a program to determine whether a given input is a Toeplitz matrix. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/315/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem315 2 | { 3 | public class Solution 4 | { 5 | public static bool IsToeplitzMatrix(int[][] matrix, int rows, int cols) 6 | { 7 | var row = 0; 8 | var col = cols - 1; 9 | 10 | while (row != rows - 1 && col != 0) 11 | { 12 | var currentRow = row + 1; 13 | var currentCol = col + 1; 14 | 15 | var number = matrix[row][col]; 16 | 17 | while (currentRow < rows && currentCol < cols) 18 | { 19 | if (matrix[currentRow][currentCol] != number) 20 | { 21 | return false; 22 | } 23 | 24 | currentRow++; 25 | currentCol++; 26 | } 27 | 28 | if (col > 0) 29 | { 30 | col--; 31 | } 32 | else 33 | { 34 | row++; 35 | } 36 | } 37 | 38 | return true; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/315/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem315 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestIsToeplitzMatrix_SatisfiesCondition_ShouldReturnTrue() 11 | { 12 | // Arrange 13 | var matrix = new[] 14 | { 15 | new []{ 1, 2, 3, 4, 8 }, 16 | new []{ 5, 1, 2, 3, 4 }, 17 | new []{ 4, 5, 1, 2, 3 }, 18 | new []{ 7, 4, 5, 1, 2 }, 19 | }; 20 | 21 | // Act 22 | var actualResult = Solution.IsToeplitzMatrix(matrix, 4, 5); 23 | 24 | // Assert 25 | actualResult.Should().BeTrue(); 26 | } 27 | 28 | [Test] 29 | public void TestIsToeplitzMatrix_DoesnotSatisfyCondition_ShouldReturnTrue() 30 | { 31 | // Arrange 32 | var matrix = new[] 33 | { 34 | new []{ 1, 2, 3, 4, 5 }, 35 | new []{ 5, 1, 2, 3, 5 }, 36 | new []{ 4, 5, 1, 2, 3 }, 37 | new []{ 7, 4, 5, 1, 2 }, 38 | }; 39 | 40 | // Act 41 | var actualResult = Solution.IsToeplitzMatrix(matrix, 4, 5); 42 | 43 | // Assert 44 | actualResult.Should().BeFalse(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/317/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Yahoo. 2 | 3 | Write a function that returns the bitwise AND of all integers between M and N, inclusive. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/317/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem317 4 | { 5 | public class Solution 6 | { 7 | public static int GetBitwiseAnd(int m, int n) 8 | { 9 | if (n < m) 10 | { 11 | return GetBitwiseAnd(n, m); 12 | } 13 | 14 | return Enumerable 15 | .Range(m, n - m + 1) 16 | .Aggregate((acc, x) => acc & x); 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/300-319/317/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem317 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [TestCase(4, 13, 0)] 9 | public void TestGetBitwiseAnd_ShouldReturnCorrectly(int m, int n, int expectedResult) 10 | { 11 | // Arrange, Act 12 | var actual = Solution.GetBitwiseAnd(m, n); 13 | 14 | // Assert 15 | Assert.AreEqual(expectedResult, actual); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/321/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by PagerDuty. 2 | 3 | Given a positive integer N, find the smallest number of steps it will take to reach 1. 4 | 5 | There are two kinds of permitted steps: 6 | 7 | You may decrement N to N - 1. 8 | If a * b = N, you may decrement N to the larger of a and b. 9 | For example, given 100, you can reach 1 in five steps with the following route: 100 -> 10 -> 9 -> 3 -> 2 -> 1. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/321/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem321 5 | { 6 | public class Solution 7 | { 8 | public static int FindSmallestNumberOfStepsToReachOne(int n) => FindSmallestNumberOfStepsToReachOne(n, 0); 9 | 10 | private static int FindSmallestNumberOfStepsToReachOne(int n, int currentSteps) 11 | { 12 | if (n < 0) 13 | { 14 | return int.MaxValue; 15 | } 16 | 17 | if (n == 1) 18 | { 19 | return currentSteps; 20 | } 21 | 22 | var items = Enumerable.Range(1, n / 2) 23 | .Where(number => n % number == 0) 24 | .Select(number => Math.Max(n / number, number)) 25 | .Distinct() 26 | .ToList(); 27 | 28 | items.Remove(n); 29 | items.Add(n - 1); 30 | 31 | return items 32 | .Select(number => FindSmallestNumberOfStepsToReachOne(number, currentSteps + 1)) 33 | .Min(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/321/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem321 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase(100,5)] 10 | public void TestFindSmallestNumberOfStepsToReachOne_ShouldReturnCorrectly(int n, int expectedResult) 11 | { 12 | // Arrange, Act 13 | var actualResult = Solution.FindSmallestNumberOfStepsToReachOne(n); 14 | 15 | // Assert 16 | actualResult.Should().Be(expectedResult); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/326/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Netflix. 2 | 3 | A Cartesian tree with sequence S is a binary tree defined by the following two properties: 4 | 5 | It is heap-ordered, so that each parent value is strictly less than that of its children. 6 | An in-order traversal of the tree produces nodes with values that correspond exactly to S. 7 | For example, given the sequence [3, 2, 6, 1, 9], the resulting Cartesian tree would be: 8 | 9 | 1 10 | / \ 11 | 2 9 12 | / \ 13 | 3 6 14 | Given a sequence S, construct the corresponding Cartesian tree. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/326/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using DailyCodingProblem.Solutions.Common; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem326 5 | { 6 | public class Solution 7 | { 8 | public static Node BuildCartesianTree(IList input) 9 | { 10 | var root = (Node)null; 11 | var current = (Node)null; 12 | 13 | foreach (var number in input) 14 | { 15 | if (root == null) 16 | { 17 | root = new Node(number); 18 | current = root; 19 | } 20 | else if (current.Value > number) 21 | { 22 | current = new Node(number, current); 23 | root = current; 24 | } 25 | else if (current.Value < number) 26 | { 27 | current.Right = new Node(number); 28 | } 29 | } 30 | 31 | return root; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/326/Tests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem326 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestBuildCartesianTree_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var input = new[] { 3, 2, 6, 1, 9 }; 15 | 16 | var expectedResult = new Node( 17 | 1, 18 | new Node(2, 19 | new Node(3), 20 | new Node(6)), 21 | new Node(9) 22 | ); 23 | 24 | // Act 25 | var actualResult = Solution.BuildCartesianTree(input); 26 | 27 | // Assert 28 | actualResult.Should().BeEquivalentTo(expectedResult, 29 | opts => opts.Excluding(c => c.Parent).IgnoringCyclicReferences()); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/329/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Amazon. 2 | 3 | The stable marriage problem is defined as follows: 4 | 5 | Suppose you have N men and N women, and each person has ranked their prospective opposite-sex partners in order of preference. 6 | 7 | For example, if N = 3, the input could be something like this: 8 | 9 | guy_preferences = { 10 | 'andrew': ['caroline', 'abigail', 'betty'], 11 | 'bill': ['caroline', 'betty', 'abigail'], 12 | 'chester': ['betty', 'caroline', 'abigail'], 13 | } 14 | 15 | gal_preferences = { 16 | 'abigail': ['andrew', 'bill', 'chester'], 17 | 'betty': ['bill', 'andrew', 'chester'], 18 | 'caroline': ['bill', 'chester', 'andrew'] 19 | } 20 | Write an algorithm that pairs the men and women together in such a way that no two people of opposite sex would both rather be with each other than with their current partners. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/332/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Jane Street. 2 | 3 | Given integers M and N, write a program that counts how many positive integer pairs (a, b) satisfy the following conditions: 4 | 5 | a + b = M 6 | a XOR b = N -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/332/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem332 2 | { 3 | public class Solution 4 | { 5 | public static int GetNumberOfPairs(int m, int n) 6 | { 7 | var result = 0; 8 | 9 | for (var i = 1; i < m; i++) 10 | { 11 | var xor = i ^ (m - i); 12 | if ( xor== n) 13 | { 14 | result++; 15 | } 16 | } 17 | 18 | return result; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/332/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem332 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase(5, 5, 2)] 10 | public void TestGetNumberOfPairs_ShouldReturnCorrectly(int m, int n, int expectedResult) 11 | { 12 | // Arrange, Act 13 | var actualResult = Solution.GetNumberOfPairs(m, n); 14 | 15 | // Assert 16 | actualResult.Should().Be(expectedResult); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/339/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Microsoft. 2 | 3 | Given an array of numbers and a number k, determine if there are three entries in the array which add up to the specified number k. For example, given [20, 303, 3, 4, 25] and k = 49, return true as 20 + 4 + 25 = 49. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/339/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem339 2 | { 3 | public class Solution 4 | { 5 | public static bool AreThereThreeEntriesWhichAddUpToK(int k, int[] numbers) 6 | { 7 | return AreThereThreeEntriesWhichAddUpToK(k, numbers, 0, 0, 0); 8 | } 9 | 10 | private static bool AreThereThreeEntriesWhichAddUpToK(int k, int[] numbers, int currentIndex, int currentSum, 11 | int summedNumbers) 12 | { 13 | if (currentIndex == numbers.Length || currentSum > k || summedNumbers > 3) 14 | { 15 | return false; 16 | } 17 | 18 | if (summedNumbers == 2 && (currentSum + numbers[currentIndex] == k)) 19 | { 20 | return true; 21 | } 22 | 23 | return AreThereThreeEntriesWhichAddUpToK(k, numbers, currentIndex + 1, currentSum, summedNumbers) || 24 | AreThereThreeEntriesWhichAddUpToK(k, numbers, currentIndex + 1, currentSum + numbers[currentIndex], 25 | summedNumbers + 1); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/320-339/339/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem339 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestAreThereThreeEntriesWhichAddUpToK_ShouldReturnTrue() 11 | { 12 | // Arrange 13 | var numbers = new[] { 20, 303, 3, 4, 25 }; 14 | var k = 49; 15 | 16 | // Act 17 | var actualResult = Solution.AreThereThreeEntriesWhichAddUpToK(k, numbers); 18 | 19 | // Assert 20 | actualResult.Should().BeTrue(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/342/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Stripe. 2 | 3 | reduce (also known as fold) is a function that takes in an array, a combining function, and an initial value and builds up a result by calling the combining function on each element of the array, left to right. For example, we can write sum() in terms of reduce: 4 | 5 | def add(a, b): 6 | return a + b 7 | 8 | def sum(lst): 9 | return reduce(lst, add, 0) 10 | This should call add on the initial value with the first element of the array, and then the result of that with the second element of the array, and so on until we reach the end, when we return the sum of the array. 11 | 12 | Implement your own version of reduce. 13 | 14 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/342/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem342 6 | { 7 | public class Solution 8 | { 9 | public static T Reduce(IList list, Func combiningFunc, T intialValue) 10 | { 11 | if (!list.Any()) 12 | { 13 | return intialValue; 14 | } 15 | 16 | var result = combiningFunc.Invoke(intialValue, list.First()); 17 | 18 | for (var i = 1; i < list.Count; i++) 19 | { 20 | result = combiningFunc.Invoke(result, list[i]); 21 | } 22 | 23 | return result; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/342/Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using FluentAssertions; 5 | using NUnit.Framework; 6 | 7 | namespace DailyCodingProblem.Solutions.Problem342 8 | { 9 | [TestFixture] 10 | public class Tests 11 | { 12 | [Test] 13 | public void TestReduce_ShouldReturnCorrectly() 14 | { 15 | // Arrange 16 | var input = Enumerable.Range(1, 10).ToList(); 17 | var expectedResult = input.Sum(); 18 | 19 | var sumFunc = new Func((a, b) => a + b); 20 | 21 | // Act 22 | var actualResult = Solution.Reduce(input, sumFunc, 0); 23 | 24 | // Assert 25 | actualResult.Should().Be(expectedResult); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/346/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Airbnb. 2 | 3 | You are given a huge list of airline ticket prices between different cities around the world on a given day. These are all direct flights. Each element in the list has the format (source_city, destination, price). 4 | 5 | Consider a user who is willing to take up to k connections from their origin city A to their destination B. Find the cheapest fare possible for this journey and print the itinerary for that journey. 6 | 7 | For example, our traveler wants to go from JFK to LAX with up to 3 connections, and our input flights are as follows: 8 | 9 | [ 10 | ('JFK', 'ATL', 150), 11 | ('ATL', 'SFO', 400), 12 | ('ORD', 'LAX', 200), 13 | ('LAX', 'DFW', 80), 14 | ('JFK', 'HKG', 800), 15 | ('ATL', 'ORD', 90), 16 | ('JFK', 'LAX', 500), 17 | ] 18 | 19 | Due to some improbably low flight prices, the cheapest itinerary would be JFK -> ATL -> ORD -> LAX, costing $440. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/346/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem346 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetCheapestItinerary_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var fligths = new List<(string source, string destination, int price)> 15 | { 16 | ("JFK", "ATL", 150), 17 | ("ATL", "SFO", 400), 18 | ("ORD", "LAX", 200), 19 | ("LAX", "DFW", 80), 20 | ("JFK", "HKG", 800), 21 | ("ATL", "ORD", 90), 22 | ("JFK", "LAX", 500) 23 | }; 24 | 25 | var source = "JFK"; 26 | var destination = "LAX"; 27 | var maxConnections = 3; 28 | 29 | var expected = "JFK -> ATL -> ORD -> LAX"; 30 | 31 | // Act 32 | var result = Solution.GetCheapestItinerary(source, destination, maxConnections, fligths); 33 | 34 | // Assert 35 | result.Should().Be(expected); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/349/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Grammarly. 2 | 3 | [Soundex](https://en.wikipedia.org/wiki/Soundex) is an algorithm used to categorize phonetically, such that two names that sound alike but are spelled differently have the same representation. 4 | 5 | Soundex maps every name to a string consisting of one letter and three numbers, like M460. 6 | 7 | One version of the algorithm is as follows: 8 | 9 | Remove consecutive consonants with the same sound (for example, change ck -> c). 10 | Keep the first letter. The remaining steps only apply to the rest of the string. 11 | Remove all vowels, including y, w, and h. 12 | Replace all consonants with the following digits: 13 | b, f, p, v → 1 14 | c, g, j, k, q, s, x, z → 2 15 | d, t → 3 16 | l → 4 17 | m, n → 5 18 | r → 6 19 | If you don't have three numbers yet, append zeros until you do. Keep the first three numbers. 20 | Using this scheme, Jackson and Jaxen both map to J250. 21 | 22 | Implement Soundex. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/349/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem349 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase("Jackson", "J250")] 10 | [TestCase("Jaxen", "J250")] 11 | public void TestSoundex_ShouldReturnCorrectly(string input, string expected) 12 | { 13 | // Arrange, Act 14 | var actual = Solution.Soundex(input); 15 | 16 | // Assert 17 | actual.Should().BeEquivalentTo(expected); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/355/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Airbnb. 2 | 3 | You are given an array X of floating-point numbers x1, x2, ... xn. These can be rounded up or down to create a corresponding array Y of integers y1, y2, ... yn. 4 | 5 | Write an algorithm that finds an appropriate Y array with the following properties: 6 | 7 | The rounded sums of both arrays should be equal. 8 | The absolute pairwise difference between elements is minimized. In other words, |x1- y1| + |x2- y2| + ... + |xn- yn| should be as small as possible. 9 | For example, suppose your input is [1.3, 2.3, 4.4]. In this case you cannot do better than [1, 2, 5], which has an absolute difference of |1.3 - 1| + |2.3 - 2| + |4.4 - 5| = 1. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/355/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem355 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestRoundArrayToSmallestDifferenceWithEqualSums_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var input = new[] { 1.3, 2.3, 4.4 }; 14 | var expectedResult = new[] {1, 2, 5}; 15 | 16 | // Act 17 | var actualResult = Solution.RoundArrayToSmallestDifferenceWithEqualSums(input); 18 | 19 | // Assert 20 | actualResult.Should().BeEquivalentTo(expectedResult); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/356/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Netflix. 2 | 3 | Implement a queue using a set of fixed-length arrays. 4 | 5 | The queue should support enqueue, dequeue, and get_size operations. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/340-359/356/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using FluentAssertions; 4 | using NUnit.Framework; 5 | 6 | namespace DailyCodingProblem.Solutions.Problem356 7 | { 8 | [TestFixture] 9 | public class Tests 10 | { 11 | [Test] 12 | public void Test_ShouldReturnCorrectly() 13 | { 14 | var arrays = new List 15 | { 16 | new int[3], 17 | new int[2], 18 | }; 19 | 20 | var queue = new FixedLengthArraysQueue(arrays); 21 | 22 | var items = Enumerable.Range(0, 4).ToList(); 23 | 24 | foreach (var item in items) 25 | { 26 | queue.Enqueue(item); 27 | } 28 | 29 | queue.Size.Should().Be(items.Count); 30 | 31 | var result = items 32 | .Select(_ => queue.Dequeue()) 33 | .ToList(); 34 | 35 | result.Should().BeEquivalentTo(items); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/360/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Spotify. 2 | 3 | You have access to ranked lists of songs for various users. Each song is represented as an integer, and more preferred songs appear earlier in each list. For example, the list [4, 1, 7] indicates that a user likes song 4 the best, followed by songs 1 and 7. 4 | 5 | Given a set of these ranked lists, interleave them to create a playlist that satisfies everyone's priorities. 6 | 7 | For example, suppose your input is {[1, 7, 3], [2, 1, 6, 7, 9], [3, 9, 5]}. In this case a satisfactory playlist could be [2, 1, 6, 7, 3, 9, 5]. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/360/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem360 5 | { 6 | public class Solution 7 | { 8 | public static IEnumerable GetPlaylist(List> ranks) 9 | { 10 | var result = new List(); 11 | 12 | while (ranks.Any(ranking => ranking.Any())) 13 | { 14 | foreach (var rank in ranks) 15 | { 16 | if (rank.Any()) 17 | { 18 | var next = rank.First(); 19 | 20 | var containingRanks = ranks.Where(r => r.Contains(next)).ToList(); 21 | 22 | if (containingRanks.Count(r => r.First() != next) == containingRanks.Count - 1 && containingRanks.Count > 1) 23 | { 24 | continue; 25 | } 26 | 27 | ranks 28 | .Where(r => r.Contains(next)) 29 | .ToList() 30 | .ForEach(r => r.Remove(next)); 31 | 32 | result.Add(next); 33 | 34 | break; 35 | } 36 | } 37 | } 38 | 39 | return result; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/360/Tests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using FluentAssertions; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Solutions.Problem360 6 | { 7 | [TestFixture] 8 | public class Tests 9 | { 10 | [Test] 11 | public void TestGetPlaylist_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var input = new List> 15 | { 16 | new List {1, 7, 3 }, 17 | new List { 2, 1, 6, 7, 9}, 18 | new List { 3, 9, 5}, 19 | }; 20 | 21 | var expectedResult = new List{ 2, 1, 6, 7, 3, 9, 5 }; 22 | 23 | // Act 24 | var actualResult = Solution.GetPlaylist(input); 25 | 26 | // Assert 27 | actualResult.Should().BeEquivalentTo(expectedResult); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/363/AddSubtractBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem363 2 | { 3 | public class AddSubtractBuilder 4 | { 5 | private int result; 6 | private bool isAddNext = true; 7 | 8 | private AddSubtractBuilder(int initialNumber) 9 | { 10 | this.result = initialNumber; 11 | } 12 | 13 | public AddSubtractBuilder Append(int number) 14 | { 15 | result += isAddNext ? number : -number; 16 | isAddNext = !isAddNext; 17 | 18 | return this; 19 | } 20 | 21 | public int Compute() 22 | { 23 | return this.result; 24 | } 25 | 26 | public static AddSubtractBuilder Create(int initialNumber) => new AddSubtractBuilder(initialNumber); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/363/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Squarespace. 2 | 3 | Write a function, add_subtract, which alternately adds and subtracts curried arguments. Here are some sample operations: 4 | 5 | add_subtract(7) -> 7 6 | 7 | add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0 8 | 9 | add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11 -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/363/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem363 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase(7, 7)] 10 | [TestCase(1, 0, 2, 3)] 11 | [TestCase(-5, 11, 10, 3, 9)] 12 | public void TestCompute_ShouldReturnCorrectly(int initialNumber, int expectedResult, params int[] otherNumbers) 13 | { 14 | // Arrange 15 | var builder = AddSubtractBuilder.Create(initialNumber); 16 | 17 | foreach (var otherNumber in otherNumbers) 18 | { 19 | builder.Append(otherNumber); 20 | } 21 | 22 | // Act 23 | var actualResut = builder.Compute(); 24 | 25 | // Assert 26 | actualResut.Should().Be(expectedResult); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/367/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Two Sigma. 2 | 3 | Given two sorted iterators, merge it into one iterator. 4 | 5 | For example, given these two iterators: 6 | 7 | foo = iter([5, 10, 15]) 8 | bar = iter([3, 8, 9]) 9 | You should be able to do: 10 | 11 | for num in merge_iterators(foo, bar): 12 | print(num) 13 | 14 | # 3 15 | # 5 16 | # 8 17 | # 9 18 | # 10 19 | # 15 20 | Bonus: Make it work without pulling in the contents of the iterators in memory. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/367/Solution.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem367 4 | { 5 | public class Solution 6 | { 7 | public static IEnumerable MergeIterators(IEnumerator first, IEnumerator second) 8 | { 9 | var firstAny = first.MoveNext(); 10 | var secondAny = second.MoveNext(); 11 | 12 | while (firstAny || secondAny) 13 | { 14 | int current; 15 | 16 | if (firstAny && secondAny) 17 | { 18 | if (first.Current < second.Current) 19 | { 20 | current = first.Current; 21 | firstAny = first.MoveNext(); 22 | } 23 | else 24 | { 25 | current = second.Current; 26 | secondAny = second.MoveNext(); 27 | } 28 | } 29 | else if (firstAny) 30 | { 31 | current = first.Current; 32 | firstAny = first.MoveNext(); 33 | } 34 | else 35 | { 36 | current = second.Current; 37 | secondAny = second.MoveNext(); 38 | } 39 | 40 | yield return current; 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/367/Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using FluentAssertions; 5 | using NUnit.Framework; 6 | 7 | namespace DailyCodingProblem.Solutions.Problem367 8 | { 9 | [TestFixture] 10 | public class Tests 11 | { 12 | [Test] 13 | public void TestMergeIterators_ShouldReturnCorrectly() 14 | { 15 | // Arrange 16 | var firstList = new List { 5, 10, 15 }; 17 | var secondList = new List { 3, 8, 9 }; 18 | var expectedResult = new List { 3, 5, 8, 9, 10, 15 }; 19 | 20 | using (var firstIterator = firstList.GetEnumerator()) 21 | using (var secondIterator = secondList.GetEnumerator()) 22 | { 23 | // Act 24 | var actualResult = Solution.MergeIterators(firstIterator, secondIterator).ToList(); 25 | 26 | // Assert 27 | 28 | actualResult.Should().BeEquivalentTo(expectedResult); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/368/KeyValueStore.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem368 4 | { 5 | public class KeyValueStore 6 | { 7 | private readonly IDictionary store = new Dictionary(); 8 | private readonly IDictionary maxKeys = new Dictionary(); 9 | 10 | public void Update(int key, int value) 11 | { 12 | if (store.ContainsKey(key)) 13 | { 14 | store[key] = value; 15 | } 16 | else 17 | { 18 | store.Add(key, value); 19 | } 20 | 21 | if (maxKeys.ContainsKey(value) && maxKeys[value] < key) 22 | { 23 | maxKeys[value] = key; 24 | } 25 | else 26 | { 27 | maxKeys.Add(value, key); 28 | } 29 | } 30 | 31 | public int? Get(int key) 32 | { 33 | if (store.TryGetValue(key, out var value)) 34 | { 35 | return value; 36 | } 37 | 38 | return null; 39 | } 40 | 41 | public int? MaxKey(int value) 42 | { 43 | if (maxKeys.TryGetValue(value, out var key)) 44 | { 45 | return key; 46 | } 47 | 48 | return null; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/368/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | Implement a key value store, where keys and values are integers, with the following methods: 4 | 5 | update(key, vl): updates the value at key to val, or sets it if doesn't exist 6 | get(key): returns the value with key, or None if no such value exists 7 | max_key(val): returns the largest key with value val, or None if no key with that value exists 8 | For example, if we ran the following calls: 9 | 10 | kv.update(1, 1) 11 | kv.update(2, 1) 12 | And then called kv.max_key(1), it should return 2, since it's the largest key with value 1. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/368/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem368 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [Test] 10 | public void TestKeyValueStore_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var store = new KeyValueStore(); 14 | store.Update(1, 1); 15 | store.Update(2, 1); 16 | 17 | var expectedMaxKey = 2; 18 | 19 | // Act 20 | var actualMaxKey = store.MaxKey(1); 21 | 22 | // Assert 23 | actualMaxKey.Should().Be(expectedMaxKey); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/369/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwetanow/DailyCodingProblem/af11ffee4986e50e11a047bff35467d8c88f72b4/DailyCodingProblem.Solutions/300-399/360-379/369/README.md -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/371/README.md: -------------------------------------------------------------------------------- 1 | This problem was asked by Google. 2 | 3 | You are given a series of arithmetic equations as a string, such as: 4 | 5 | y = x + 1 6 | 5 = x + 3 7 | 10 = z + y + 2 8 | The equations use addition only and are separated by newlines. Return a mapping of all variables to their values. If it's not possible, then return null. In this example, you should return: 9 | 10 | { 11 | x: 2, 12 | y: 3, 13 | z: 5 14 | } -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/372/README.md: -------------------------------------------------------------------------------- 1 | Good morning! Here's your coding interview problem for today. 2 | 3 | This problem was asked by Amazon. 4 | 5 | Write a function that takes a natural number as input and returns the number of digits the input has. 6 | 7 | Constraint: don't use any loops. -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/372/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem372 2 | { 3 | public class Solution 4 | { 5 | public static int GetNumberOfDigits(int number) => number.ToString().Length; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/300-399/360-379/372/Tests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Solutions.Problem372 5 | { 6 | [TestFixture] 7 | public class Tests 8 | { 9 | [TestCase(12, 2)] 10 | [TestCase(5555, 4)] 11 | public void TestGetNumberOfDigits_ShouldReturnCorrectly(int number, int expectedResult) 12 | { 13 | // Arrange, Act 14 | var actual = Solution.GetNumberOfDigits(number); 15 | 16 | // Assert 17 | actual.Should().Be(expectedResult); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/Common/Node.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Common 2 | { 3 | public class Node 4 | { 5 | public Node(T value) 6 | { 7 | Value = value; 8 | } 9 | 10 | public Node(T value, Node left, Node right) 11 | : this(value) 12 | { 13 | this.Left = left; 14 | this.Right = right; 15 | } 16 | 17 | public Node(T value, Node left = null, Node right = null, Node parent = null) 18 | : this(value,left,right) 19 | { 20 | this.Parent = parent; 21 | } 22 | 23 | public Node Right { get; set; } 24 | 25 | public Node Left { get; set; } 26 | 27 | public Node Parent { get; set; } 28 | 29 | public T Value { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/DailyCodingProblem.Solutions.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/SolutionTemplate/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cwetanow/DailyCodingProblem/af11ffee4986e50e11a047bff35467d8c88f72b4/DailyCodingProblem.Solutions/SolutionTemplate/README.md -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/SolutionTemplate/Solution.cs: -------------------------------------------------------------------------------- 1 | namespace DailyCodingProblem.Solutions.Problem 2 | { 3 | public class Solution 4 | { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /DailyCodingProblem.Solutions/SolutionTemplate/Tests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Solutions.Problem 4 | { 5 | [TestFixture] 6 | public class Tests 7 | { 8 | [Test] 9 | public void Test_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | 13 | // Act 14 | 15 | // Assert 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/01-19/Problem01Tests/TwoNumbersEqualTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem01Tests 5 | { 6 | [TestFixture] 7 | public class TwoNumbersEqualTests 8 | { 9 | [Test] 10 | public void TestProblem01Solution_NumbersAddUpToK_ShouldReturnTrue() 11 | { 12 | // Arrange 13 | var numbers = new[] { 10, 15, 3, 7 } 14 | .ToList(); 15 | var k = 17; 16 | 17 | // Act 18 | var result = Solutions.Problem01.Solution.TwoNumbersEqual(numbers, k); 19 | 20 | // Assert 21 | Assert.IsTrue(result); 22 | } 23 | 24 | [Test] 25 | public void TestProblem01Solution_NumbersDoNotAddUpToK_ShouldReturnFalse() 26 | { 27 | // Arrange 28 | var numbers = new[] { 10, 15, 3, 7 } 29 | .ToList(); 30 | var k = 16; 31 | 32 | // Act 33 | var result = Solutions.Problem01.Solution.TwoNumbersEqual(numbers, k); 34 | 35 | // Assert 36 | Assert.IsFalse(result); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/01-19/Problem02Tests/GetArrayOfProductsTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem02Tests 4 | { 5 | [TestFixture] 6 | public class GetArrayOfProductsTests 7 | { 8 | [TestCase(1, new[] { 1, 2, 3, 4, 5 }, new[] { 120, 60, 40, 30, 24 })] 9 | [TestCase(2, new[] { 3, 2, 1 }, new[] { 2, 3, 6 })] 10 | public void TestGetArrayOfProducts_ShouldReturnCorrectly(int _, int[] input, int[] expectedResult) 11 | { 12 | // Arrange, Act 13 | var result = DailyCodingProblem.Solutions.Problem02.Solution.GetArrayOfProducts(input); 14 | 15 | // Assert 16 | CollectionAssert.AreEqual(expectedResult, result); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/20-39/Problem26Tests/AddTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using DailyCodingProblem.Tests.Problem26Tests.Mock; 3 | 4 | namespace DailyCodingProblem.Tests.Problem26Tests 5 | { 6 | [TestFixture] 7 | public class AddTests 8 | { 9 | [TestCase(1)] 10 | [TestCase(2)] 11 | [TestCase(1234)] 12 | public void TestAdd_ShouldAddElementToList(int element) 13 | { 14 | // Arrange 15 | var list = new ExtendedWithContainsLinkedList(); 16 | 17 | // Act 18 | list.Add(element); 19 | 20 | // Assert 21 | Assert.IsTrue(list.Contains(element)); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/20-39/Problem26Tests/Mock/ExtendedLinkedList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using DailyCodingProblem.Solutions.Problem26; 3 | 4 | namespace DailyCodingProblem.Tests.Problem26Tests.Mock 5 | { 6 | public class ExtendedWithContainsLinkedList 7 | : ExtendedLinkedList 8 | where T : IComparable, IEquatable 9 | { 10 | public bool Contains(T element) 11 | { 12 | if (this.Root == null) 13 | { 14 | return false; 15 | } 16 | 17 | var currentNode = this.Root; 18 | 19 | while (currentNode != null) 20 | { 21 | if (currentNode.Value.CompareTo(element) == 0) 22 | { 23 | return true; 24 | } 25 | 26 | currentNode = currentNode.Next; 27 | } 28 | 29 | return false; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/40-59/Problem40Tests/GetNonDuplicatedIntegerTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem40Tests 4 | { 5 | [TestFixture] 6 | public class GetNonDuplicatedIntegerTests 7 | { 8 | [TestCase(1, new int[] { 6, 1, 3, 3, 3, 6, 6 })] 9 | [TestCase(19, new int[] { 13, 19, 13, 13 })] 10 | public static void TestGetNonDuplicatedInteger_ShouldReturnCorrectly(int expected, int[] input) 11 | { 12 | // Arrange, Act 13 | var actual = Solutions.Problem40.Solution.GetNonDuplicatedInteger(input); 14 | 15 | // Assert 16 | Assert.AreEqual(expected, actual); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/40-59/Problem50Tests/SolutionTests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Problem50; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem50Tests 5 | { 6 | [TestFixture] 7 | public class SolutionTests 8 | { 9 | [Test] 10 | public void TestProblem50Solution_ShouldReturnCorrectResult() 11 | { 12 | // Arrange 13 | var root = new Node('*', new Node('+', new Node(3), new Node(2)), new Node('+', new Node(4), new Node(5))); 14 | 15 | var expectedResult = 45; 16 | 17 | // Act 18 | var result = Solution.Evaluate(root); 19 | 20 | // Assert 21 | Assert.AreEqual(expectedResult, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem61Tests/PowTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | 4 | namespace DailyCodingProblem.Tests.Problem61Tests 5 | { 6 | [TestFixture] 7 | public class PowTests 8 | { 9 | [TestCase(2, 10)] 10 | [TestCase(3, 3)] 11 | [TestCase(10, 3)] 12 | public void TestPow_ShouldReturnCorrectly(int number, int power) 13 | { 14 | // Arrange 15 | var expectedResult = Math.Pow(number, power); 16 | 17 | // Arrange, Act 18 | var result = Solutions.Problem61.Solution.Pow(number, power); 19 | 20 | // Assert 21 | Assert.AreEqual(expectedResult, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem62Tests/SolutionTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem62Tests 4 | { 5 | [TestFixture] 6 | public class SolutionTests 7 | { 8 | [TestCase(2, 2, 2)] 9 | [TestCase(5, 5, 70)] 10 | public void TestProblem62Solution_ShouldReturnCorrectResult(int n, int m, int expectedResult) 11 | { 12 | // Arrange 13 | // Act 14 | var result = Solutions.Problem62.Solution.GetNumberOfWaysToCorner(n, m); 15 | 16 | // Assert 17 | Assert.AreEqual(expectedResult, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem69Tests/GetLargestProductTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem69Tests 4 | { 5 | [TestFixture] 6 | public class GetLargestProductTests 7 | { 8 | [Test] 9 | public void TestGetLargestProduct_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var numbers = new int[] { -10, -10, 5, 2 }; 13 | 14 | var expectedResult = 500; 15 | 16 | // Act 17 | var result = Solutions.Problem69.Solution.GetLargestProduct(numbers); 18 | 19 | // Assert 20 | Assert.AreEqual(expectedResult, result); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem70Tests/GetNthPerfectNumberTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem70Tests 4 | { 5 | [TestFixture] 6 | public class GetNthPerfectNumberTests 7 | { 8 | [TestCase(1, 19)] 9 | [TestCase(2, 28)] 10 | [TestCase(10, 109)] 11 | [TestCase(36, 370)] 12 | public void TestGetNthPerfectNumber_ShouldReturnCorrectly(int n, int expectedResult) 13 | { 14 | // Arrange, Act 15 | var result = Solutions.Problem70.Solution.GetNthPerfectNumber(n); 16 | 17 | // Assert 18 | Assert.AreEqual(expectedResult, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem73Tests/ReverseTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using DailyCodingProblem.Solutions.Problem73; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Tests.Problem73Tests 6 | { 7 | [TestFixture] 8 | public class ReverseTests 9 | { 10 | [Test] 11 | public void TestReverse_ShouldWorkCorrectly() 12 | { 13 | // Arrange 14 | var linkedList = new Solutions.Common.LinkedList(); 15 | var list = new List(); 16 | 17 | for (int i = 0; i < 10; i++) 18 | { 19 | linkedList.Add(i); 20 | list.Add(i); 21 | } 22 | 23 | list.Reverse(); 24 | 25 | // Act 26 | linkedList.Reverse(); 27 | 28 | // Assert 29 | CollectionAssert.AreEqual(list, linkedList.ToList()); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem74Tests/GetXOccurencesCountTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem74Tests 4 | { 5 | [TestFixture] 6 | public class GetXOccurencesCountTests 7 | { 8 | [TestCase(6, 12, 4)] 9 | [TestCase(2, 12, 0)] 10 | public void TestGetXOccurencesCount_ShouldReturnCorrectly(int n, int x, int expected) 11 | { 12 | // Arrange, Act 13 | var actual = Solutions.Problem74.Solution.GetXOccurencesCount(n, x); 14 | 15 | // Assert 16 | Assert.AreEqual(expected, actual); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem75Tests/GetLongestIncreasingSubsequenceTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem75Tests 4 | { 5 | [TestFixture] 6 | public class GetLongestIncreasingSubsequenceTests 7 | { 8 | [Test] 9 | public void TestGetLongestIncreasingSubsequence_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = new int[] { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 }; 13 | var expectedResult = new int[] { 0, 2, 6, 9, 11, 15 }; 14 | 15 | // Act 16 | var actualResult = Solutions.Problem75.Solution.GetLongestIncreasingSubsequence(input); 17 | 18 | // Assert 19 | CollectionAssert.AreEqual(expectedResult, actualResult); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/60-79/Problem77Tests/MergeOverlappingIntervalsTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System.Collections.Generic; 3 | 4 | namespace DailyCodingProblem.Tests.Problem77Tests 5 | { 6 | [TestFixture] 7 | public class MergeOverlappingIntervalsTests 8 | { 9 | [Test] 10 | public void TestMergeOverlappingIntervals_ShouldReturnCorrectResult() 11 | { 12 | // Arrange 13 | var input = new List<(int start, int end)> { 14 | (1, 3), (5, 8), (4, 10), (20, 25) 15 | }; 16 | 17 | var expectedResult = new List<(int start, int end)> { 18 | (1, 3), (4, 10), (20, 25) 19 | }; 20 | 21 | // Act 22 | var actual = Solutions.Problem77.Solution.MergeOverlappingIntervals(input); 23 | 24 | // Assert 25 | CollectionAssert.AreEqual(expectedResult, actual); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/80-99/Problem80Tests/GetDeepestNodeTests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem80Tests 5 | { 6 | [TestFixture] 7 | public class GetDeepestNodeTests 8 | { 9 | [Test] 10 | public void TestGetDeepestNode_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var root = new Node("a", 14 | new Node("b", 15 | new Node("d"), null), 16 | new Node("c")); 17 | 18 | var expected = "d"; 19 | 20 | // Act 21 | var deepestNode = Solutions.Problem80.Solution.GetDeepestNode(root); 22 | 23 | // Assert 24 | Assert.AreEqual(expected, deepestNode); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/80-99/Problem81Tests/GetPossibleLettersTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System.Collections.Generic; 3 | 4 | namespace DailyCodingProblem.Tests.Problem81Tests 5 | { 6 | [TestFixture] 7 | public class GetPossibleLettersTests 8 | { 9 | [Test] 10 | public void TestGetPossibleLetters_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var mappings = new Dictionary { 14 | { '2', new string[] { "a", "b", "c" } }, 15 | { '3', new string[] { "d", "e", "f" } } 16 | }; 17 | 18 | var inputNumber = "23"; 19 | 20 | var expectedResult = new string[] { 21 | "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" 22 | }; 23 | 24 | // Act 25 | var actualResult = Solutions.Problem81.Solution.GetPossibleLetters(inputNumber, mappings); 26 | 27 | // Assert 28 | CollectionAssert.AreEqual(expectedResult, actualResult); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/80-99/Problem85Tests/GetNumberTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem85Tests 4 | { 5 | [TestFixture] 6 | public class GetNumberTests 7 | { 8 | [TestCase(2, 15)] 9 | [TestCase(1, 5)] 10 | [TestCase(6, 42)] 11 | public void TestGetNumber_BIsOne_ShouldReturnX(int x, int y) 12 | { 13 | // Arrange 14 | var b = 1; 15 | 16 | // Act 17 | var result = Solutions.Problem85.Solution.GetNumber(b, x, y); 18 | 19 | // Assert 20 | Assert.AreEqual(x, result); 21 | } 22 | 23 | [TestCase(2, 15)] 24 | [TestCase(1, 5)] 25 | [TestCase(6, 42)] 26 | public void TestGetNumber_BIsZero_ShouldReturnY(int x, int y) 27 | { 28 | // Arrange 29 | var b = 0; 30 | 31 | // Act 32 | var result = Solutions.Problem85.Solution.GetNumber(b, x, y); 33 | 34 | // Assert 35 | Assert.AreEqual(y, result); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/80-99/Problem88Tests/DivideTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem88Tests 4 | { 5 | [TestFixture] 6 | public class DivideTests 7 | { 8 | [TestCase(5, 2)] 9 | [TestCase(12, 7)] 10 | [TestCase(1573, 22)] 11 | [TestCase(1756254, 4589)] 12 | public void TestDivide_ShouldReturnCorrectly(int divident, int divisor) 13 | { 14 | // Arrange 15 | var expected = divident / divisor; 16 | 17 | // Act 18 | var result = Solutions.Problem88.Solution.Divide(divident, divisor); 19 | 20 | // Assert 21 | Assert.AreEqual(expected, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/01-99/80-99/Problem89Tests/IsBinaryTreeTests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem89Tests 5 | { 6 | [TestFixture] 7 | public class IsBinaryTreeTests 8 | { 9 | [Test] 10 | public void TestIsBinaryTree_InputIsBinaryTree_ShouldReturnTrue() 11 | { 12 | // Arrange 13 | var root = new Node(5, 14 | new Node(3, 15 | new Node(2), new Node(4)), 16 | new Node(7, 17 | new Node(6), new Node(8))); 18 | 19 | // Act 20 | var result = Solutions.Problem89.Solution.IsBinaryTree(root); 21 | 22 | // Assert 23 | Assert.IsTrue(result); 24 | } 25 | 26 | [Test] 27 | public void TestIsBinaryTree_InputIsNotBinaryTree_ShouldReturnFalse() 28 | { 29 | // Arrange 30 | var root = new Node(1, new Node(3), new Node(5)); 31 | 32 | // Act 33 | var result = Solutions.Problem89.Solution.IsBinaryTree(root); 34 | 35 | // Assert 36 | Assert.IsFalse(result); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem101Tests/GetPrimeNumbersWithCurrentSumTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem101Tests 4 | { 5 | [TestFixture] 6 | public class GetPrimeNumbersWithCurrentSumTests 7 | { 8 | [TestCase(4, 2, 2)] 9 | public void TestGetPrimeNumbersWithCurrentSum_ShouldReturnCorrectly(int input, int expectedLeft, int expectedRight) 10 | { 11 | // Arrange, Act 12 | var (left, right) = Solutions.Problem101.Solution.GetPrimeNumbersWithSum(input); 13 | 14 | // Assert 15 | Assert.AreEqual(expectedLeft, left); 16 | Assert.AreEqual(expectedRight, right); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem102Tests/GetContiguousElementsThatSumToKTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem102Tests 4 | { 5 | [TestFixture] 6 | public class GetContiguousElementsThatSumToKTests 7 | { 8 | [Test] 9 | public void TestGetContiguousElementsThatSumToK_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var k = 9; 13 | var list = new[] { 1, 2, 3, 4, 5 }; 14 | 15 | var expectedResult = new[] { 2, 3, 4 }; 16 | 17 | // Act 18 | var actual = Solutions.Problem102.Solution.GetContiguousElementsThatSumToK(list, k); 19 | 20 | // Assert 21 | CollectionAssert.AreEqual(expectedResult, actual); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem104Tests/IsSinglyLinkedListPalindromeTests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem104Tests 5 | { 6 | [TestFixture] 7 | public class IsSinglyLinkedListPalindromeTests 8 | { 9 | [Test] 10 | public void TestIsSinglyLinkedListPalindrome_ListIsPalindrome_ShouldReturnTrue() 11 | { 12 | // Arrange 13 | var list = new LinkedList(); 14 | list.Add(1); 15 | list.Add(4); 16 | list.Add(3); 17 | list.Add(4); 18 | list.Add(1); 19 | 20 | // Act 21 | var result = Solutions.Problem104.Solution.IsSinglyLinkedListPalindrome(list); 22 | 23 | // Assert 24 | Assert.IsTrue(result); 25 | } 26 | 27 | [Test] 28 | public void TestIsSinglyLinkedListPalindrome_ListIsNotPalindrome_ShouldReturnFalse() 29 | { 30 | // Arrange 31 | var list = new LinkedList(); 32 | list.Add(1); 33 | list.Add(4); 34 | 35 | // Act 36 | var result = Solutions.Problem104.Solution.IsSinglyLinkedListPalindrome(list); 37 | 38 | // Assert 39 | Assert.IsFalse(result); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem106Tests/CanReachLastIndexTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem106Tests 4 | { 5 | [TestFixture] 6 | public class CanReachLastIndexTests 7 | { 8 | [Test] 9 | public void TestCanReachLastIndex_CanReach_ShouldReturnTrue() 10 | { 11 | // Arrange 12 | var list = new[] { 2, 0, 1, 0 }; 13 | 14 | // Act 15 | var result = Solutions.Problem106.Solution.CanReachLastIndex(list); 16 | 17 | // Assert 18 | Assert.IsTrue(result); 19 | } 20 | 21 | [Test] 22 | public void TestCanReachLastIndex_CanNotReach_ShouldReturnFalse() 23 | { 24 | // Arrange 25 | var list = new[] { 1, 1, 0, 1 }; 26 | 27 | // Act 28 | var result = Solutions.Problem106.Solution.CanReachLastIndex(list); 29 | 30 | // Assert 31 | Assert.IsFalse(result); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem107Tests/PrintTreeLevelWiseTests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem107Tests 5 | { 6 | [TestFixture] 7 | public class PrintTreeLevelWiseTests 8 | { 9 | [Test] 10 | public void TestPrintTreeLevelWise_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var root = new Solutions.Common.Node(1, 14 | new Node(2), 15 | new Node(3, 16 | new Node(4), 17 | new Node(5))); 18 | 19 | var expectedResult = "1 2 3 4 5"; 20 | 21 | // Act 22 | var actual = Solutions.Problem107.Solution.PrintTreeLevelWise(root); 23 | 24 | // Assert 25 | Assert.AreEqual(expectedResult, actual); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem110Tests/GetPathsToLeavesTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using DailyCodingProblem.Solutions.Common; 3 | using NUnit.Framework; 4 | 5 | namespace DailyCodingProblem.Tests.Problem110Tests 6 | { 7 | [TestFixture] 8 | public class GetPathsToLeavesTests 9 | { 10 | [Test] 11 | public void TestGetPathsToLeaves_ShouldReturnCorrectly() 12 | { 13 | // Arrange 14 | var root = new Node(1, 15 | new Node(2), 16 | new Node(3, 17 | new Node(4), 18 | new Node(5))); 19 | 20 | var expected = new List>() 21 | { 22 | new List { 1, 2 }, 23 | new List { 1, 3,4 }, 24 | new List { 1, 3,5 } 25 | }; 26 | 27 | // Act 28 | var actual = Solutions.Problem110.Solution.GetPathsToLeaves(root); 29 | 30 | // Assert 31 | CollectionAssert.AreEquivalent(expected, actual); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem113Tests/ReverseWordsTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem113Tests 4 | { 5 | [TestFixture] 6 | public class ReverseWordsTests 7 | { 8 | [Test] 9 | public void TestReverseWords_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = "hello world here"; 13 | var expectedResult = "here world hello"; 14 | 15 | // Act 16 | var actual = Solutions.Problem113.Solution.ReverseWords(input); 17 | 18 | // Assert 19 | Assert.AreEqual(expectedResult, actual); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem118Tests/GetSquaredSortedElementsTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem118Tests 4 | { 5 | [TestFixture] 6 | public class GetIntervalThatCoversOthersTests 7 | { 8 | [Test] 9 | public void TestGetSquaredSortedElements_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = new int[] { -9, -2, 0, 2, 3 }; 13 | 14 | var expectedResult = new int[] { 0, 4, 4, 9, 81 }; 15 | 16 | // Act 17 | var actualResult = DailyCodingProblem.Solutions.Problem118.Solution.GetSquaredSortedElements(input); 18 | 19 | // Assert 20 | CollectionAssert.AreEqual(expectedResult, actualResult); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/100-119/Problem119Tests/GetIntervalThatCoversOthersTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem119Tests 5 | { 6 | [TestFixture] 7 | public class GetIntervalThatCoversOthersTests 8 | { 9 | [Test] 10 | public void TestGetIntervalThatCoversOthers_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var input = new List<(int start, int end)> { (0, 3), (2, 6), (3, 4), (6, 9) }; 14 | 15 | var expectedStart = 3; 16 | var expectedEnd = 6; 17 | 18 | // Act 19 | var (start, end) = DailyCodingProblem.Solutions.Problem119.Solution.GetIntervalThatCoversOthers(input); 20 | 21 | // Assert 22 | Assert.AreEqual(expectedStart, start); 23 | Assert.AreEqual(expectedEnd, end); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/120-139/Problem122Tests/GetMaxNumberOfCoinsTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem122Tests 4 | { 5 | [TestFixture] 6 | public class GetMaxNumberOfCoinsTests 7 | { 8 | [Test] 9 | public void TestGetMaxNumberOfCoins_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var input = new int[3][] 13 | { 14 | new int[]{0,3,1,1}, 15 | new int[]{2,0,0,4}, 16 | new int[]{1,5,3,1}, 17 | }; 18 | 19 | var expectedResult = 12; 20 | 21 | // Act 22 | var result = Solutions.Problem122.Solution.GetMaxNumberOfCoins(input); 23 | 24 | // Assert 25 | Assert.AreEqual(expectedResult, result); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/120-139/Problem123Tests/IsNumberTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem123Tests 4 | { 5 | [TestFixture] 6 | public class IsNumberTests 7 | { 8 | [TestCase("10")] 9 | [TestCase("-10")] 10 | [TestCase("10.1")] 11 | [TestCase("-10.1")] 12 | [TestCase("1e5")] 13 | public void TestIsNumber_IsNumber_ShouldReturnTrue(string input) 14 | { 15 | // Arrange, Act 16 | var result = Solutions.Problem123.Solution.IsNumber(input); 17 | 18 | // Assert 19 | Assert.IsTrue(result); 20 | } 21 | 22 | [TestCase("a")] 23 | [TestCase("x 1")] 24 | [TestCase("a -2")] 25 | [TestCase("-")] 26 | public void TestIsNumber_IsNotNumber_ShouldReturnFalse(string input) 27 | { 28 | // Arrange, Act 29 | var result = Solutions.Problem123.Solution.IsNumber(input); 30 | 31 | // Assert 32 | Assert.IsFalse(result); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/120-139/Problem125Tests/GetNodesWithSumKTests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem125Tests 5 | { 6 | [TestFixture] 7 | public class GetNodesWithSumKTests 8 | { 9 | [Test] 10 | public void TestGetNodesWithSumK_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var root = new Node(10, 14 | new Node(5), 15 | new Node(15, 16 | new Node(11), 17 | new Node(15))); 18 | 19 | var k = 20; 20 | 21 | var expectedResult = new int[] { 5, 15 }; 22 | 23 | // Act 24 | var result = Solutions.Problem125.Solution.GetNodesWithSumK(root, k); 25 | 26 | // Assert 27 | CollectionAssert.AreEquivalent(expectedResult, result); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/120-139/Problem126Tests/RotateListTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System.Collections.Generic; 3 | 4 | namespace DailyCodingProblem.Tests.Problem126Tests 5 | { 6 | [TestFixture] 7 | public class RotateListTests 8 | { 9 | [Test] 10 | public void TestRotateList_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var input = new List { 1, 2, 3, 4, 5, 6 }; 14 | var k = 2; 15 | 16 | var expectedResult = new List { 3, 4, 5, 6, 1, 2 }; 17 | 18 | // Act 19 | var actual = Solutions.Problem126.Solution.RotateList(input, k); 20 | 21 | // Assert 22 | CollectionAssert.AreEqual(expectedResult, actual); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/120-139/Problem127Tests/SumNumbersTests.cs: -------------------------------------------------------------------------------- 1 | using DailyCodingProblem.Solutions.Common; 2 | using NUnit.Framework; 3 | 4 | namespace DailyCodingProblem.Tests.Problem127Tests 5 | { 6 | [TestFixture] 7 | public class SumNumbersTests 8 | { 9 | [Test] 10 | public void TestSumNumbers_ShouldReturnCorrectly() 11 | { 12 | // Arrange 13 | var left = new LinkedList(); 14 | left.Add(9); 15 | left.Add(9); 16 | 17 | var right = new LinkedList(); 18 | right.Add(5); 19 | right.Add(2); 20 | 21 | var expectedResult = new LinkedList(); 22 | expectedResult.Add(4); 23 | expectedResult.Add(2); 24 | expectedResult.Add(1); 25 | 26 | // Act 27 | var actual = Solutions.Problem127.Solution.SumNumbers(left, right); 28 | 29 | // Assert 30 | Assert.AreEqual(expectedResult, actual); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/120-139/Problem129Tests/GetSquareRootTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem129Tests 4 | { 5 | [TestFixture] 6 | public class GetSquareRootTests 7 | { 8 | [TestCase(9, 3)] 9 | public void TestGetSquareRoot_ShouldReturnCorrectly(int input, int expectedResult) 10 | { 11 | // Arrange, Act 12 | var actual = Solutions.Problem129.Solution.GetSquareRoot(input); 13 | 14 | // Assert 15 | Assert.AreEqual(expectedResult, actual); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/100-199/160-179/Problem161Tests/GetReversedBitRepresentationTests.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace DailyCodingProblem.Tests.Problem161Tests 4 | { 5 | [TestFixture] 6 | public class GetReversedBitRepresentationTests 7 | { 8 | [Test] 9 | public void TestGetReversedBitRepresentation_ShouldReturnCorrectly() 10 | { 11 | // Arrange 12 | var number = 4042322160;// 1111 0000 1111 0000 1111 0000 1111 0000; 13 | 14 | var expectedResult = "00001111000011110000111100001111"; 15 | 16 | // Act 17 | var result = Solutions.Problem161.Solution.GetReversedBitRepresentation(number); 18 | 19 | // Assert 20 | Assert.AreEqual(expectedResult, result); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /DailyCodingProblem.Tests/DailyCodingProblem.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ivan Mladenov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DailyCodingProblem [![Build Status](https://travis-ci.org/cwetanow/DailyCodingProblem.svg?branch=master)](https://travis-ci.org/cwetanow/DailyCodingProblem) 2 | 3 | This repository contains solutions of the [Daily Coding Problem](https://www.dailycodingproblem.com/) tasks 4 | --------------------------------------------------------------------------------