├── .gitignore ├── InterviewPreparation ├── AmazonJourney │ ├── Hard │ │ └── ConcatenatedWords.cs │ ├── Medium │ │ ├── Dijsktra.cs │ │ └── RobotBounded.cs │ ├── PriorityQueue.cs │ └── exercises.md ├── CommonExercises │ ├── AllPathFromOriginToEnd.cs │ ├── BSTFromInorderPreorder.cs │ ├── ConnectRight.cs │ ├── DiameterOfBTree.cs │ ├── Easy-Array │ │ ├── AddOne.cs │ │ ├── Duplicates.cs │ │ ├── IntersectionTwoArrays.cs │ │ ├── MoveZeroes.cs │ │ ├── RemoveDuplicates.cs │ │ ├── RotateArray.cs │ │ ├── RotateImage.cs │ │ ├── SingleNumberInArray.cs │ │ ├── TwoSum.cs │ │ └── ValidSudoku.cs │ ├── Easy-DynamicProgramming │ │ ├── BuyAndSellStocks.cs │ │ ├── ClimbingStairs.cs │ │ └── HouseRobber.cs │ ├── Easy-List │ │ ├── DeleteNode.cs │ │ ├── DetectCycle.cs │ │ ├── MergeTwoSortedLists.cs │ │ ├── Palindrome.cs │ │ ├── RemoveNTHElement.cs │ │ └── ReverseList.cs │ ├── Easy-SearchAndSorting │ │ ├── FirstBadVersion.cs │ │ └── MergeTwoSortedArray.cs │ ├── Easy-String │ │ ├── Atoi.cs │ │ ├── CountAndSay.cs │ │ ├── FirstUniqueCharcs.cs │ │ ├── LongestPrefix.cs │ │ ├── Palindrome.cs │ │ ├── ReverseInteger.cs │ │ ├── ReverseString.cs │ │ ├── StrStr.cs │ │ └── ValidAnagram.cs │ ├── Easy-Trees │ │ ├── ConvertSortedArrayToBinarySearchTree.cs │ │ ├── LevelOrderTrasversal.cs │ │ └── SymmetricTree.cs │ ├── GenerateAllBalancedParenthesis.cs │ ├── GetAllSubsets.cs │ ├── GroupAnagramsSolved.cs │ ├── Hard-Array │ │ ├── 4SumII.cs │ │ ├── ContainerWithMostWater.cs │ │ ├── FindDuplicate.cs │ │ ├── FirstMissingPositive.cs │ │ ├── GameOfLife.cs │ │ ├── LongestConsecutive.cs │ │ ├── MinWindowString.cs │ │ ├── ProductOfArrayExceptSelf.cs │ │ └── SlidingWindowMaximum.cs │ ├── Hard-DynamicProgramming │ │ └── MaximumProduct.cs │ ├── Hard-Graph │ │ ├── BinaryTreeMaximumPathSum.cs │ │ ├── CourseSchedule.cs │ │ ├── CourseScheduleII.cs │ │ ├── CriticalEdge.cs │ │ ├── NumberOfProvinces.cs │ │ └── WorldLadder.cs │ ├── Hard-List │ │ └── MergeKSortedLists.cs │ ├── IncreasingTripletSolutions.cs │ ├── Intersection2Lists.cs │ ├── InvertBinaryTree.cs │ ├── KadaneAlgorithm.cs │ ├── LetterTotalCombinations.cs │ ├── ListNode.cs │ ├── LongestMountain.cs │ ├── LongestPalindromeEx.cs │ ├── LongestSubstringWORepeated.cs │ ├── MaximumDepth.cs │ ├── Medium-DynamicProgramming │ │ ├── AllUniquePaths.cs │ │ ├── CoinChangeDP.cs │ │ ├── JumpGame.cs │ │ ├── LongestCommonSubsequence.cs │ │ ├── LongestIncreasingSubsequence.cs │ │ └── MinimumAscii.cs │ ├── Medium-Other │ │ └── MajorityElement.cs │ ├── Medium-Search │ │ ├── SearchIn2DArrayII.cs │ │ └── SearchInRotatedSortedArray.cs │ ├── MergeIntervals.cs │ ├── MinHeight.cs │ ├── Monotonic.cs │ ├── MoveZeros.cs │ ├── Node.cs │ ├── NumberOfIslands.cs │ ├── OddEvenList.cs │ ├── PathSum.cs │ ├── PeakElement.cs │ ├── SearchRangeBS.cs │ ├── SetZeroesMatrix.cs │ ├── SortColorsEx.cs │ ├── SpiralOrdering.cs │ ├── SubSequenceCheck.cs │ ├── SumTwoNumbers.cs │ ├── ThirdMaximum.cs │ ├── ThreeSum.cs │ ├── TopFrequentK.cs │ ├── TotalPermutations.cs │ ├── TreeNode.cs │ ├── ValidateBST.cs │ ├── WordSearch.cs │ └── ZigzagOrderTrasversal.cs ├── InterviewPreparation.csproj ├── InterviewPreparation.sln ├── KthLargestInBST.cs ├── MicrosoftExcercises │ ├── Easy │ │ ├── AddStrings.cs │ │ ├── BalancedHeightTree.cs │ │ ├── BalancedTreeFromSortedArray.cs │ │ ├── BestTimeToSellAndBuy.cs │ │ ├── BestTimetoBuyandSellStockII.cs │ │ ├── BinarySum.cs │ │ ├── BinaryTreePath.cs │ │ ├── BuyStock.cs │ │ ├── ClimbingStairs.cs │ │ ├── CountPrimes.cs │ │ ├── DeleteDuplicates.cs │ │ ├── DeleteNode.cs │ │ ├── DiameterOfBinaryTree.cs │ │ ├── ExcelTitle.cs │ │ ├── ExcelTitleToNumber.cs │ │ ├── FactorialTralingZeroes.cs │ │ ├── Fibonacci.cs │ │ ├── FindAllNumbersDisappearedArray.cs │ │ ├── FindSecondMinimumValue.cs │ │ ├── FirstUniqueCharInString.cs │ │ ├── FizzBuzz.cs │ │ ├── IntersectionOfTwoList.cs │ │ ├── IsPalindrome.cs │ │ ├── IsSubtree.cs │ │ ├── IsSymmetric.cs │ │ ├── LinkedListCycle.cs │ │ ├── LongestPrefix.cs │ │ ├── LowestCommonAncestor.cs │ │ ├── LowestCommonAncestorI.cs │ │ ├── MajorityElement.cs │ │ ├── MinDepth.cs │ │ ├── MinStack.cs │ │ ├── MostCommonWords.cs │ │ ├── MoveZeroes.cs │ │ ├── MyHashMap.cs │ │ ├── MyQueue.cs │ │ ├── MyStack.cs │ │ ├── NumberOfBalloons.cs │ │ ├── PalindromeLinkedList.cs │ │ ├── PascalTriangle.cs │ │ ├── PathSum.cs │ │ ├── PeakElement.cs │ │ ├── PerformShiftString.cs │ │ ├── PivotIndex.cs │ │ ├── PlusOne.cs │ │ ├── RectangleOverlap.cs │ │ ├── RemoveElements.cs │ │ ├── RemoveElementsFromList.cs │ │ ├── ReverseInteger.cs │ │ ├── ReverseOnlyLetters.cs │ │ ├── ReverseString.cs │ │ ├── ReverseString2.cs │ │ ├── ReverseStringIII.cs │ │ ├── RomanToInteger.cs │ │ ├── RotateString.cs │ │ ├── SQRT.cs │ │ ├── SmallestRange.cs │ │ ├── SortByParity.cs │ │ ├── StrStr.cs │ │ ├── SummaryRanges.cs │ │ ├── ThirdNumber.cs │ │ ├── TwoSumSorted.cs │ │ ├── UniqueEmailAddress.cs │ │ ├── VPalindrome.cs │ │ ├── ValidAnagram.cs │ │ ├── ValidParentheses.cs │ │ └── WordPattern.cs │ ├── Hard │ │ ├── AlienDict.cs │ │ ├── Autocomplete.cs │ │ ├── BasicCalculator.cs │ │ ├── DungeonGame.cs │ │ ├── EditDistance.cs │ │ ├── FirstMissingPositiveSWAP.cs │ │ ├── FrogGame.cs │ │ ├── IntegerToEnglishWords.cs │ │ ├── LargestHistogram.cs │ │ ├── LargestRectangleInMatrix.cs │ │ ├── LongestIncreasingPathInMatrix.cs │ │ ├── LongestValidParentheses.cs │ │ ├── MaxFreqStack.cs │ │ ├── MaximumPathBinaryTree.cs │ │ ├── MedianOfDataStream.cs │ │ ├── MedianSorted.cs │ │ ├── MergeKLists.cs │ │ ├── MinWindowSubstring.cs │ │ ├── QueensChess.cs │ │ ├── RegularExpressionMatching.cs │ │ ├── ReverseKNodes.cs │ │ ├── ReversePairs.cs │ │ ├── SerializeAndDeserialize.cs │ │ ├── SerializeDeserializeNTree.cs │ │ ├── SkyLine.cs │ │ ├── SlidingWindowMaximum.cs │ │ ├── SudokuSolver.cs │ │ ├── TrappingRainWater.cs │ │ ├── UniquePaths3.cs │ │ ├── ValidNumber.cs │ │ ├── WildCardMatch.cs │ │ ├── WildcardMatching.cs │ │ ├── WordBreakII.cs │ │ ├── WordLadder.cs │ │ ├── WordLadderII.cs │ │ └── WordSearchII.cs │ ├── Medium │ │ ├── 3Sum.cs │ │ ├── 3SumClosest.cs │ │ ├── 4Sum.cs │ │ ├── AllNodesDistanceK.cs │ │ ├── BSTIterator.cs │ │ ├── BasicCalculatorII.cs │ │ ├── BattleShip.cs │ │ ├── BiPartite.cs │ │ ├── BinaryTreeLevelOrderTraversalII.cs │ │ ├── BinaryTreeRightSideView.cs │ │ ├── BuildTreePostorderInorder.cs │ │ ├── BuildingsWithanOceanView.cs │ │ ├── Calendar2.cs │ │ ├── CalendarI.cs │ │ ├── CanJump.cs │ │ ├── CanPartition.cs │ │ ├── CheapestFlightsWithinKStops.cs │ │ ├── CheckCompletenessOfABinaryTree.cs │ │ ├── CheckSubarraySum.cs │ │ ├── ChessKnightProbability.cs │ │ ├── CloneGraph.cs │ │ ├── ClosestKPoints.cs │ │ ├── CoinChange.cs │ │ ├── CoinChangeII.cs │ │ ├── CombinationSum.cs │ │ ├── CombinationSumIII.cs │ │ ├── Combinations.cs │ │ ├── CombinationsII.cs │ │ ├── CompareVersion.cs │ │ ├── CompressString.cs │ │ ├── ConnectRightcs.cs │ │ ├── ContainerWithMostWater.cs │ │ ├── CopyListWithRandomPointer.cs │ │ ├── CountAllPalindromesDP.cs │ │ ├── CountAndSay.cs │ │ ├── CountSquares.cs │ │ ├── CourseSchedule.cs │ │ ├── CourseScheduleII.cs │ │ ├── DailyTemperatures.cs │ │ ├── DecodeString.cs │ │ ├── DecodeWays.cs │ │ ├── DeleteNodeInBST.cs │ │ ├── DetectCycleII.cs │ │ ├── DistribuiteCoins.cs │ │ ├── DivideTwoIntegers.cs │ │ ├── EncodeAndDecodeTinyUrl.cs │ │ ├── ExclusiveTime.cs │ │ ├── FindAllDuplicates.cs │ │ ├── FindClosestElements.cs │ │ ├── FindDuplicatesSubtree.cs │ │ ├── FindPeakElement.cs │ │ ├── FindSmallestKPairs.cs │ │ ├── FindTheDuplicateNumber.cs │ │ ├── FirstAndLast.cs │ │ ├── Flatten.cs │ │ ├── FlattenNestedListIterator.cs │ │ ├── GameOfLife.cs │ │ ├── GasStation.cs │ │ ├── GenerateBalancedParenthesis.cs │ │ ├── GroupAnagrams.cs │ │ ├── HouseRobber.cs │ │ ├── HtmlParser.cs │ │ ├── InorderTraversal.cs │ │ ├── InsertInterval.cs │ │ ├── InsertIntoBST.cs │ │ ├── IntegerToRoman.cs │ │ ├── IntegerToString.cs │ │ ├── InterleavingString.cs │ │ ├── Itinerary.cs │ │ ├── KSmallestInMatrix.cs │ │ ├── KnightDialer.cs │ │ ├── KthElementInBST.cs │ │ ├── LCA3.cs │ │ ├── LIS.cs │ │ ├── LRUCache.cs │ │ ├── LargestNumber.cs │ │ ├── LetterCasePermutation.cs │ │ ├── LetterCombination.cs │ │ ├── LevelOrder.cs │ │ ├── LongestIncreasingConsecutive.cs │ │ ├── LongestPalindrome.cs │ │ ├── LongestPalindromeSubSeq.cs │ │ ├── LongestSubstringNoRepeat.cs │ │ ├── LowestCommonAncestorBinaryTree.cs │ │ ├── MajorityII.cs │ │ ├── MaxProduct.cs │ │ ├── MaximalNetworkRank.cs │ │ ├── MaximalSquare.cs │ │ ├── MaximumUniqueCharacters.cs │ │ ├── MaximumWidthBinaryTree.cs │ │ ├── MergeIntervals.cs │ │ ├── Min2Anagram.cs │ │ ├── MinPathSum.cs │ │ ├── Minesweeper.cs │ │ ├── MinimumInSorted.cs │ │ ├── MinimumSubarraySumK.cs │ │ ├── MultiplyStrings.cs │ │ ├── MyLinkedList.cs │ │ ├── MyPow.cs │ │ ├── NextGreaterElementII.cs │ │ ├── NextPermutation.cs │ │ ├── NumberOfProvinces.cs │ │ ├── OddEvenList.cs │ │ ├── PacificAtlantic.cs │ │ ├── PartitionLabels.cs │ │ ├── PartitionList.cs │ │ ├── PathSumII.cs │ │ ├── PeekingIterator.cs │ │ ├── Permutations.cs │ │ ├── PopulateRightII.cs │ │ ├── PrintedTree.cs │ │ ├── ProductExcept.cs │ │ ├── RandomPickWithWeight.cs │ │ ├── RangeSumQuery2D.cs │ │ ├── RectangleArea.cs │ │ ├── RemoveAllOcurrences.cs │ │ ├── RemoveCommnets.cs │ │ ├── RemoveDuplicatesStringLexo.cs │ │ ├── RemoveKDigits.cs │ │ ├── RemoveNthElementList.cs │ │ ├── ReorderList.cs │ │ ├── RestoreIpAddress.cs │ │ ├── ReverseLinkedListII.cs │ │ ├── ReversePolishNotation.cs │ │ ├── ReverseWords.cs │ │ ├── RotateArray.cs │ │ ├── RotateList.cs │ │ ├── RotatingTheBox.cs │ │ ├── Search2DArray.cs │ │ ├── SearchArraySortedSecret.cs │ │ ├── SearchIn2dMatrix.cs │ │ ├── SearchSuggestionsSystem.cs │ │ ├── SetMatrixToZeroInPlace.cs │ │ ├── ShoppingOffers.cs │ │ ├── Shuffle.cs │ │ ├── SimplifyPath.cs │ │ ├── SortCharactersByFrequency.cs │ │ ├── SortDiagonally.cs │ │ ├── SortList.cs │ │ ├── SortedListToBST.cs │ │ ├── SpiralMatrix.cs │ │ ├── StringWOAAABBB.cs │ │ ├── SubArraySum.cs │ │ ├── Subsets.cs │ │ ├── SwapPairs.cs │ │ ├── SwappingNodesInALinkedList.cs │ │ ├── TOPKFrequent.cs │ │ ├── TaskScheduler.cs │ │ ├── TopKFrequentWords.cs │ │ ├── TotalAnagrams.cs │ │ ├── Triangle.cs │ │ ├── TriangleNumber.cs │ │ ├── Trie.cs │ │ ├── TrimBST.cs │ │ ├── UniquePaths.cs │ │ ├── UniquePathsII.cs │ │ ├── ValidSudoku.cs │ │ ├── ValidTicTacToe.cs │ │ ├── ValidTree.cs │ │ ├── ValidateBST.cs │ │ ├── WebCrawler.cs │ │ ├── WordBreak.cs │ │ └── WordSearch.cs │ ├── Node.cs │ └── Premium │ │ ├── BSTToList.cs │ │ ├── BoundaryOfABTree.cs │ │ ├── ClosestBinaryTreeValue.cs │ │ ├── DesignHitCounter.cs │ │ ├── DistinctIslands.cs │ │ ├── FindCelebrity.cs │ │ ├── InsertInCyclicSortedList.cs │ │ ├── LargestBST.cs │ │ ├── MeetingRoomsII.cs │ │ ├── MovingAverage.cs │ │ ├── OneEditDistance.cs │ │ ├── PalindromePermutation.cs │ │ ├── ReverseWords3.cs │ │ ├── TheMaze.cs │ │ ├── TicTacToeDesign.cs │ │ ├── VerticalOrder.cs │ │ ├── WallsAndGates.cs │ │ └── WiggleSort.cs ├── Program.cs ├── Shared │ ├── GraphNode.cs │ └── UnionFind.cs ├── Theory │ ├── arrays │ │ ├── prefix-sum.md │ │ └── sliding-windows-technique.md │ ├── backtracking │ │ └── backtracking.md │ ├── bst │ │ └── binary-tree-traversals.md │ ├── linked-list │ │ └── two-pointers-technique.md │ ├── sorting-and-searching │ │ ├── dutch-national-flag.md │ │ └── quickselect.md │ ├── strings │ │ └── KMP-search-pattern.md │ └── union-find │ │ └── union-find-resource.md └── TrainExercises │ ├── DailyChallengesLC │ ├── DeepestLeavesSum.cs │ └── LongestIncreasingPathInMatrix.cs │ ├── Easy │ ├── BuyAndSellStocks.cs │ ├── CommonChars.cs │ ├── CountBinarySubStrings.cs │ ├── FindDiff.cs │ ├── LargestSubstringBetweenTwoEqualCharacters.cs │ ├── LicensePlate.cs │ ├── MaximumScoreAfterSplittingaString.cs │ ├── MinimunChangesAlternatingBinaryString.cs │ ├── Shuffle.cs │ ├── SplitStringInBalancedStrings.cs │ ├── SumAllDigits.cs │ └── ValidAnagrams.cs │ └── Medium │ ├── SerializeAndDeserializedBST.cs │ └── SurroundedRegionscs.cs ├── LICENSE ├── OOD ├── OOD.csproj └── StackOverflow │ ├── Account.cs │ ├── Admin.cs │ ├── Answer.cs │ ├── Badge.cs │ ├── Bounty.cs │ ├── Comment.cs │ ├── Enums.cs │ ├── Guest.cs │ ├── ISearch.cs │ ├── Member.cs │ ├── Moderator.cs │ ├── Notification.cs │ ├── Question.cs │ └── Tag.cs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.xlsx 2 | /InterviewPreparation/bin/ 3 | bin 4 | obj 5 | .vs -------------------------------------------------------------------------------- /InterviewPreparation/AmazonJourney/Medium/RobotBounded.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.AmazonJourney.Medium 2 | { 3 | public class RobotBounded 4 | { 5 | public bool IsRobotBounded(string instructions) 6 | { 7 | (int x, int y) position = (0, 0); 8 | (int dx, int dy) direction = (0, 1); 9 | 10 | for (int i = 0; i < 4; i++) 11 | { 12 | foreach (var instruction in instructions) 13 | { 14 | switch (instruction) 15 | { 16 | case 'G': 17 | position = (position.x + direction.dx, position.y + direction.dy); 18 | break; 19 | case 'L': 20 | direction = (-direction.dy, direction.dx); 21 | break; 22 | case 'R': 23 | direction = (direction.dy, -direction.dx); 24 | break; 25 | } 26 | } 27 | } 28 | 29 | return position == (0, 0) && direction == (0, 1); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/AmazonJourney/exercises.md: -------------------------------------------------------------------------------- 1 | | Name | Resource link | Status | Difficulty | 2 | | --------- | ----- | | ----- | -----:| 3 | | Nth Fibonacci | https://leetcode.com/problems/robot-bounded-in-circle/ | :white_check_mark: | Medium | 4 | | Concatenated Words | https://leetcode.com/problems/concatenated-words | :white_check_mark: | Hard | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/BSTFromInorderPreorder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.Exercises 4 | { 5 | public class BSTFromInorderPreorder 6 | { 7 | // Recursive way 8 | 9 | int preorderIndex = 0; 10 | Dictionary inorderPositions; 11 | 12 | public TreeNode BuildTree(int[] preorder, int[] inorder) 13 | { 14 | inorderPositions = new Dictionary(); 15 | 16 | for (int i = 0; i < inorder.Length; i++) 17 | { 18 | inorderPositions.Add(inorder[i], i); 19 | } 20 | 21 | return ArrayToTree(preorder, inorder, 0, preorder.Length - 1); 22 | } 23 | 24 | private TreeNode ArrayToTree(int[] preorder, int[] inorder, int left, int right) 25 | { 26 | if (left > right) 27 | { 28 | return null; 29 | } 30 | 31 | int rootValue = preorder[preorderIndex++]; 32 | TreeNode root = new TreeNode(rootValue); 33 | 34 | root.left = ArrayToTree(preorder, inorder, left, inorderPositions[rootValue] - 1); 35 | root.right = ArrayToTree(preorder, inorder, inorderPositions[rootValue] + 1, right); 36 | 37 | return root; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/DiameterOfBTree.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.Exercises 4 | { 5 | public class DiameterOfBTree 6 | { 7 | private int diameter; 8 | public int DiameterOfBinaryTree(TreeNode root) 9 | { 10 | diameter = 0; 11 | 12 | LongestPath(root); 13 | 14 | return diameter; 15 | } 16 | 17 | public int LongestPath(TreeNode root) 18 | { 19 | if (root == null) 20 | { 21 | return 0; 22 | } 23 | 24 | var leftDepth = LongestPath(root.left); 25 | var rightDepth = LongestPath(root.right); 26 | 27 | diameter = Math.Max(diameter, leftDepth + rightDepth); 28 | 29 | return Math.Max(leftDepth, rightDepth) + 1; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Array/AddOne.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_Array 4 | { 5 | class AddOne 6 | { 7 | // https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/559/ 8 | 9 | public int[] PlusOne(int[] digits) 10 | { 11 | int carry = 1; 12 | 13 | for (int i = digits.Length - 1; i >= 0 && carry != 0; i--) 14 | { 15 | var actual = digits[i] + carry; 16 | carry = actual / 10; 17 | 18 | digits[i] = actual % 10; 19 | } 20 | 21 | if (carry != 0) 22 | { 23 | digits = new int[] { 1 }.Concat(digits).ToArray(); 24 | } 25 | 26 | return digits; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Array/Duplicates.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_Array 4 | { 5 | //https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/578/ 6 | class Duplicates 7 | { 8 | public bool ContainsDuplicate(int[] nums) 9 | { 10 | var hs = new HashSet(); 11 | 12 | for (int i = 0; i < nums.Length; i++) 13 | { 14 | if (hs.Contains(nums[i])) 15 | { 16 | return true; 17 | } 18 | 19 | hs.Add(nums[i]); 20 | } 21 | 22 | return false; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Array/MoveZeroes.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_Array 2 | { 3 | // https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/567/ 4 | class MoveZeroes 5 | { 6 | public void Move(int[] nums) 7 | { 8 | var fillIndex = 0; 9 | 10 | for (int i = 0; i < nums.Length; i++) 11 | { 12 | if (nums[i] != 0) 13 | { 14 | if (fillIndex != i) 15 | { 16 | nums[fillIndex] = nums[i]; 17 | nums[i] = 0; 18 | } 19 | 20 | fillIndex++; 21 | } 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Array/RemoveDuplicates.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_Array 2 | { 3 | class RemoveDuplicatesFromSortedArray 4 | 5 | { 6 | public int RemoveDuplicates(int[] nums) 7 | { 8 | if (nums.Length < 2) 9 | { 10 | return nums.Length; 11 | } 12 | 13 | var leftPosition = 0; 14 | 15 | for (int i = 0; i < nums.Length; i++) 16 | { 17 | if (i == 0 || nums[i] != nums[i - 1]) 18 | { 19 | nums[leftPosition] = nums[i]; 20 | leftPosition++; 21 | } 22 | } 23 | 24 | return leftPosition; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Array/RotateArray.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_Array 4 | { 5 | class RotateArray 6 | { 7 | public void Rotate(int[] nums, int k) 8 | { 9 | k = k % nums.Length; 10 | int count = 0; 11 | 12 | for (int start = 0; count < nums.Length; start++) 13 | { 14 | var current = start; 15 | var prev = nums[start]; 16 | 17 | do 18 | { 19 | var next = (current + k) % nums.Length; 20 | int tmp = nums[next]; 21 | nums[next] = prev; 22 | current = next; 23 | prev = tmp; 24 | count++; 25 | } while (current != start); 26 | } 27 | } 28 | 29 | public void RotateWithSpace(int[] nums, int k) 30 | { 31 | int[] aux = new int[nums.Length]; 32 | k = k % nums.Length; 33 | 34 | for (int i = 0; i < nums.Length; i++) 35 | { 36 | aux[(i + k) % nums.Length] = nums[i]; 37 | } 38 | 39 | Array.Copy(aux, nums, aux.Length); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Array/SingleNumberInArray.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_Array 2 | { 3 | class SingleNumberInArray 4 | { 5 | public int SingleNumber(int[] nums) 6 | { 7 | int result = 0; 8 | 9 | for (int i = 0; i < nums.Length; i++) 10 | { 11 | result = result ^ nums[i]; 12 | } 13 | 14 | return result; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Array/TwoSum.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_Array 4 | { 5 | //https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/546/ 6 | class TwoSum 7 | { 8 | public int[] GetTwoSum(int[] nums, int target) 9 | { 10 | var hs = new Dictionary(); 11 | 12 | for (int i = 0; i < nums.Length; i++) 13 | { 14 | if (hs.ContainsKey(target - nums[i])) 15 | { 16 | return new int[] { i, hs[target - nums[i]] }; 17 | } 18 | else 19 | { 20 | hs.Add(nums[i], i); 21 | } 22 | } 23 | 24 | return null; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-DynamicProgramming/BuyAndSellStocks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_DynamicProgramming 4 | { 5 | class BuyAndSellStocks 6 | { 7 | public int MaxProfit(int[] prices) 8 | { 9 | var profits = new int[prices.Length + 1]; 10 | var max = 0; 11 | 12 | for (int i = 1; i < prices.Length; i++) 13 | { 14 | profits[i] = Math.Max(0, profits[i - 1] + (prices[i] - prices[i - 1])); 15 | max = Math.Max(profits[i], max); 16 | } 17 | 18 | return max; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-DynamicProgramming/ClimbingStairs.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_DynamicProgramming 2 | { 3 | class ClimbingStairs 4 | { 5 | public int ClimbStairs(int n) 6 | { 7 | if (n == 1) 8 | { 9 | return 1; 10 | } 11 | 12 | if (n == 2) 13 | { 14 | return 2; 15 | } 16 | 17 | var arr = new int[n]; 18 | 19 | arr[0] = 1; 20 | arr[1] = 2; 21 | 22 | for (int i = 2; i < n; i++) 23 | { 24 | arr[i] = arr[i - 1] + arr[i - 2]; 25 | } 26 | 27 | return arr[n - 1]; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-DynamicProgramming/HouseRobber.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_DynamicProgramming 4 | { 5 | class HouseRobber 6 | { 7 | public int Rob(int[] nums) 8 | { 9 | if (nums.Length == 1) 10 | { 11 | return nums[0]; 12 | } 13 | 14 | if (nums.Length == 2) 15 | { 16 | return Math.Max(nums[0], nums[1]); 17 | } 18 | 19 | var prevPrev = nums[0]; 20 | var prev = Math.Max(nums[0], nums[1]); 21 | int current = 0; 22 | 23 | for (int i = 2; i < nums.Length; i++) 24 | { 25 | current = Math.Max(prevPrev + nums[i], prev); 26 | prevPrev = prev; 27 | prev = current; 28 | } 29 | 30 | return current; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-List/DeleteNode.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_List 4 | { 5 | class DeleteNode 6 | { 7 | public void Delete(ListNode node) 8 | { 9 | 10 | var aux = node; 11 | 12 | while (aux.next.next != null) 13 | { 14 | aux.val = aux.next.val; 15 | aux = aux.next; 16 | } 17 | 18 | aux.val = aux.next.val; 19 | aux.next = null; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-List/Palindrome.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_List 4 | { 5 | class Palindrome 6 | { 7 | public bool IsPalindrome(ListNode head) 8 | { 9 | if (head == null) 10 | { 11 | return true; 12 | } 13 | 14 | var fast = head; 15 | var slow = head; 16 | ListNode prev = null; 17 | 18 | while (fast != null && fast.next != null) 19 | { 20 | fast = fast.next.next; 21 | 22 | //reversing left side of the list 23 | var tmp = slow.next; 24 | slow.next = prev; 25 | prev = slow; 26 | slow = tmp; 27 | } 28 | 29 | if (fast != null) //odd 30 | { 31 | slow = slow.next; 32 | } 33 | 34 | while (slow != null) 35 | { 36 | if (slow.val != prev.val) 37 | { 38 | return false; 39 | } 40 | 41 | slow = slow.next; 42 | prev = prev.next; 43 | } 44 | 45 | return true; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-List/RemoveNTHElement.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_List 4 | { 5 | class RemoveNTHElement 6 | { 7 | public ListNode RemoveNthFromEnd(ListNode head, int n) 8 | { 9 | ListNode dummy = new ListNode(0); 10 | ListNode first = dummy; 11 | ListNode second = dummy; 12 | 13 | dummy.next = head; 14 | 15 | for (int i = 1; i <= n + 1; i++) 16 | { 17 | first = first.next; 18 | } 19 | 20 | while (first != null) 21 | { 22 | first = first.next; 23 | second = second.next; 24 | } 25 | 26 | second.next = second.next.next; 27 | 28 | return dummy.next; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-List/ReverseList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_List 4 | { 5 | class ReverseList 6 | { 7 | public ListNode Solution(ListNode head) 8 | { 9 | if (head == null || head.next == null) 10 | { 11 | return head; 12 | } 13 | else 14 | { 15 | var actual = Solution(head.next); 16 | head.next.next = head; 17 | head.next = null; 18 | 19 | return actual; 20 | } 21 | } 22 | 23 | public ListNode ReverseListTwoPointers(ListNode head) 24 | { 25 | if (head == null) 26 | { 27 | return null; 28 | } 29 | 30 | var slow = head; 31 | ListNode prev = null; 32 | 33 | while (slow != null) 34 | { 35 | var tmp = slow.next; 36 | slow.next = prev; 37 | prev = slow; 38 | slow = tmp; 39 | } 40 | 41 | return prev; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-SearchAndSorting/FirstBadVersion.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_SearchAndSorting 2 | { 3 | class FirstBadVersion 4 | { 5 | public int Solve(int n) 6 | { 7 | int low = 1; 8 | int high = n; 9 | 10 | while (low <= high) 11 | { 12 | var mid = low + (high - low) / 2; 13 | 14 | if (IsBadVersion(mid) && !IsBadVersion(mid - 1)) 15 | { 16 | return mid; 17 | } 18 | else if (IsBadVersion(mid)) 19 | { 20 | high = mid - 1; 21 | } 22 | else 23 | { 24 | low = mid + 1; 25 | } 26 | } 27 | 28 | return 1; 29 | } 30 | 31 | private bool IsBadVersion(int mid) // this is implemented on leetcode, for testing can be easily modified 32 | { 33 | throw new System.NotImplementedException(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-String/FirstUniqueCharcs.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_String 2 | { 3 | //https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/881/ 4 | class FirstUniqueCharcs 5 | { 6 | public int FirstUniqChar(string s) 7 | { 8 | 9 | // -1 repeated 10 | // 0 not found 11 | // > 0 arr[i] - 1 = position 12 | var charPositions = new int[26]; 13 | 14 | for (int i = 0; i < s.Length; i++) 15 | { 16 | if (charPositions[s[i] - 'a'] == 0) 17 | { 18 | charPositions[s[i] - 'a'] = i + 1; 19 | } 20 | else 21 | { 22 | charPositions[s[i] - 'a'] = -1; 23 | } 24 | } 25 | 26 | for (int i = 0; i < s.Length; i++) 27 | { 28 | if (charPositions[s[i] - 'a'] > 0) 29 | { 30 | return charPositions[s[i] - 'a'] - 1; 31 | } 32 | } 33 | 34 | return -1; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-String/LongestPrefix.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | namespace InterviewPreparation.CommonExercises.Easy_String 4 | { 5 | //https://leetcode.com/problems/longest-common-prefix 6 | class LongestPrefix 7 | { 8 | public string LongestCommonPrefix(string[] strs) 9 | { 10 | if (strs.Length == 0) 11 | { 12 | return ""; 13 | } 14 | 15 | var prefix = new StringBuilder(); 16 | 17 | int i = 0; 18 | int j = 0; 19 | char actualChar = ' '; 20 | 21 | while (true) 22 | { 23 | if (j >= strs[i].Length) 24 | { 25 | return prefix.ToString(); 26 | } 27 | 28 | if (i == 0) 29 | { 30 | actualChar = strs[i][j]; 31 | } 32 | else if (actualChar != strs[i][j]) 33 | { 34 | return prefix.ToString(); 35 | } 36 | 37 | i = (i + 1) % strs.Length; 38 | 39 | if (i == 0) 40 | { 41 | prefix.Append(actualChar); 42 | j++; 43 | } 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-String/Palindrome.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_String 2 | { 3 | class Palindrome 4 | { 5 | public bool IsPalindrome(string s) 6 | { 7 | int i = 0; 8 | int j = s.Length - 1; 9 | 10 | while (i < j) 11 | { 12 | while (i < j && !char.IsDigit(s[i]) && !char.IsLetter(s[i])) 13 | { 14 | i++; 15 | } 16 | while (i < j && !char.IsDigit(s[j]) && !char.IsLetter(s[j])) 17 | { 18 | j--; 19 | } 20 | 21 | if (i < j && char.ToLower(s[i]) != char.ToLower(s[j])) 22 | { 23 | 24 | return false; 25 | } 26 | 27 | i++; 28 | j--; 29 | } 30 | 31 | return true; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-String/ReverseInteger.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_String 2 | { 3 | class ReverseInteger 4 | { 5 | //https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/879/ 6 | public int Reverse(int x) 7 | { 8 | int rev = 0; 9 | while (x != 0) 10 | { 11 | int pop = x % 10; 12 | x /= 10; 13 | // Shitty condition 14 | if (rev > int.MaxValue / 10 || (rev == int.MaxValue / 10 && pop > 7)) return 0; 15 | if (rev < int.MinValue / 10 || (rev == int.MinValue / 10 && pop < -8)) return 0; 16 | rev = rev * 10 + pop; 17 | } 18 | return rev; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-String/ReverseString.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_String 2 | { 3 | class ReverseString 4 | { 5 | //https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/879/ 6 | public void Reverse(char[] s) 7 | { 8 | int i = 0; 9 | int j = s.Length - 1; 10 | char tmp; 11 | 12 | while (i < j) 13 | { 14 | tmp = s[i]; 15 | s[i] = s[j]; 16 | s[j] = tmp; 17 | 18 | j--; 19 | i++; 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-String/StrStr.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_String 2 | { 3 | class StrStr 4 | { 5 | public int CalculateStrStr(string haystack, string needle) 6 | { 7 | if (needle.Length == 0) 8 | { 9 | return 0; 10 | } 11 | 12 | // haystack = [1,2,3],-,-] actualLength = 3, i = 2, required = 5 13 | // needle = [3,2,1] 3 14 | for (int i = 0; i < haystack.Length && i + needle.Length - 1 <= haystack.Length - 1; i++) 15 | { 16 | if (haystack[i] == needle[0]) 17 | { 18 | var foundDiff = false; 19 | 20 | for (int j = 0; j < needle.Length && !foundDiff; j++) 21 | { 22 | foundDiff = haystack[i + j] != needle[j]; 23 | } 24 | 25 | if (!foundDiff) 26 | { 27 | return i; 28 | } 29 | } 30 | } 31 | 32 | return -1; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-String/ValidAnagram.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Easy_String 2 | { 3 | class ValidAnagram 4 | { 5 | //https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/882/ 6 | public bool IsAnagram(string s, string t) 7 | { 8 | int[] bucket = new int[26]; 9 | 10 | foreach (var character in s) 11 | { 12 | bucket[character - 'a']++; 13 | } 14 | 15 | foreach (var character in t) 16 | { 17 | bucket[character - 'a']--; 18 | } 19 | 20 | foreach (var b in bucket) 21 | { 22 | if (b != 0) 23 | { 24 | return false; 25 | } 26 | } 27 | 28 | return true; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Easy-Trees/ConvertSortedArrayToBinarySearchTree.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System; 3 | 4 | namespace InterviewPreparation.CommonExercises.Easy_Trees 5 | { 6 | class ConvertSortedArrayToBinarySearchTree 7 | { 8 | public TreeNode SortedArrayToBST(int[] nums) 9 | { 10 | return SortedArrayToBST(nums, 0, nums.Length - 1); 11 | } 12 | 13 | private TreeNode SortedArrayToBST(int[] nums, int low, int high) 14 | { 15 | if (low > high) 16 | { 17 | return null; 18 | } 19 | 20 | var mid = low + (high - low) / 2; 21 | 22 | Console.WriteLine(nums[mid]); 23 | var node = new TreeNode(nums[mid]); 24 | 25 | node.left = SortedArrayToBST(nums, low, mid - 1); 26 | node.right = SortedArrayToBST(nums, mid + 1, high); 27 | 28 | return node; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/GetAllSubsets.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.Exercises 4 | { 5 | class GetAllSubsets 6 | { 7 | public IList> Subsets(int[] nums) 8 | { 9 | var subsets = new List>(); 10 | 11 | GenerateSubsets(new List(), 0, subsets, nums); 12 | 13 | return subsets; 14 | } 15 | 16 | private void GenerateSubsets(IList actual, int index, IList> subsets, int[] nums) 17 | { 18 | subsets.Add(new List(actual)); 19 | 20 | for (int i = index; i < nums.Length; i++) 21 | { 22 | actual.Add(nums[i]); 23 | 24 | GenerateSubsets(actual, i + 1, subsets, nums); 25 | 26 | actual.RemoveAt(actual.Count - 1); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Hard-Array/ContainerWithMostWater.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.CommonExercises.Hard_Array 4 | { 5 | class ContainerWithMostWater 6 | { 7 | public int MaxArea(int[] height) 8 | { 9 | var maxArea = 0; 10 | var i = 0; 11 | var j = height.Length - 1; 12 | 13 | while (i < j) 14 | { 15 | var hI = height[i]; 16 | var hJ = height[j]; 17 | 18 | maxArea = Math.Max(maxArea, Math.Min(hI, hJ) * (j - i)); 19 | 20 | if (hI < hJ) 21 | { 22 | i++; 23 | } 24 | else 25 | { 26 | j--; 27 | } 28 | } 29 | 30 | return maxArea; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Hard-Array/FindDuplicate.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Hard_Array 2 | { 3 | class FindDuplicate 4 | { 5 | public int Solve(int[] nums) 6 | { 7 | var slow = 0; 8 | var fast = 0; 9 | 10 | while (fast < nums.Length && nums[fast] < nums.Length) 11 | { 12 | fast = nums[nums[fast]]; 13 | slow = nums[slow]; 14 | 15 | if (fast == slow) 16 | { 17 | break; 18 | } 19 | } 20 | 21 | slow = 0; 22 | 23 | while (slow != fast) 24 | { 25 | slow = nums[slow]; 26 | fast = nums[fast]; 27 | } 28 | 29 | return slow; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Hard-Array/FirstMissingPositive.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.CommonExercises.Hard_Array 5 | { 6 | class FirstMissingPositive 7 | { 8 | public int Solve(int[] nums) 9 | { 10 | nums = nums.Where(num => num > 0).ToHashSet().ToArray(); 11 | 12 | Array.Sort(nums); 13 | 14 | for (int i = 1; i < int.MaxValue; i++) 15 | { 16 | if (i == nums.Length + 1 || i != nums[i - 1]) 17 | { 18 | return i; 19 | } 20 | } 21 | 22 | return 301; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Hard-Array/LongestConsecutive.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.CommonExercises.Hard_Array 5 | { 6 | class LongestConsecutive 7 | { 8 | public int Solve(int[] nums) 9 | { 10 | var maxLongestStreak = 0; 11 | 12 | var hs = new HashSet(nums); 13 | 14 | for (int i = 0; i < nums.Length; i++) 15 | { 16 | var actual = nums[i]; 17 | 18 | if (!hs.Contains(actual - 1)) 19 | { 20 | var longestStreak = 0; 21 | 22 | while (hs.Contains(actual)) 23 | { 24 | longestStreak++; 25 | actual++; 26 | } 27 | 28 | maxLongestStreak = Math.Max(maxLongestStreak, longestStreak); 29 | } 30 | } 31 | 32 | return maxLongestStreak; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Hard-Array/ProductOfArrayExceptSelf.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.CommonExercises.Hard_Array 2 | { 3 | class ProductOfArrayExceptSelf 4 | { 5 | public int[] ProductExceptSelf(int[] nums) 6 | { 7 | var result = new int[nums.Length]; 8 | 9 | result[0] = 1; 10 | 11 | for (int i = 1; i < nums.Length; i++) 12 | { 13 | result[i] = result[i - 1] * nums[i - 1]; 14 | } 15 | 16 | var right = 1; 17 | 18 | for (int i = nums.Length - 2; i >= 0; i--) 19 | { 20 | right = right * nums[i + 1]; 21 | result[i] = result[i] * right; 22 | } 23 | 24 | return result; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Hard-Array/SlidingWindowMaximum.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.CommonExercises.Hard_Array 5 | { 6 | class SlidingWindowMaximum 7 | { 8 | public int[] MaxSlidingWindow(int[] nums, int k) 9 | { 10 | var result = new int[nums.Length - k + 1]; 11 | var queue = new LinkedList(); 12 | 13 | for (int i = 0; i < nums.Length; i++) 14 | { 15 | if (queue.Any() && queue.First.Value + k == i) 16 | { 17 | queue.RemoveFirst(); 18 | } 19 | 20 | while (queue.Any() && nums[i] > nums[queue.Last.Value]) 21 | { 22 | queue.RemoveLast(); 23 | } 24 | 25 | queue.AddLast(i); 26 | 27 | if (i + 1 - k >= 0) 28 | { 29 | result[i + 1 - k] = nums[queue.First.Value]; 30 | } 31 | } 32 | 33 | return result; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Hard-DynamicProgramming/MaximumProduct.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.CommonExercises.Hard_DynamicProgramming 4 | { 5 | class MaximumProduct 6 | { 7 | public int MaxProduct(int[] nums) 8 | { 9 | 10 | int max = nums[0], min = nums[0], res = nums[0]; 11 | 12 | for (int i = 1; i < nums.Length; i++) 13 | { 14 | int product1 = nums[i]; 15 | 16 | // nums[i] and max are positive 17 | int product2 = nums[i] * max; 18 | 19 | // nums[i] and min are negative 20 | int product3 = nums[i] * min; 21 | 22 | max = Math.Max(Math.Max(product1, product2), product3); 23 | min = Math.Min(Math.Min(product1, product2), product3); 24 | 25 | res = Math.Max(res, max); 26 | } 27 | 28 | return res; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/KadaneAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.Exercises 4 | { 5 | public class KadaneAlgorithm 6 | { 7 | // Largest Sum Contiguous Subarray 8 | public int KadanesAlgorithm(int[] array) 9 | { 10 | var currentMax = array[0]; 11 | var globalMax = array[0]; 12 | 13 | for (int i = 1; i < array.Length; i++) 14 | { 15 | var num = array[i]; 16 | currentMax = Math.Max(num, currentMax + num); 17 | globalMax = Math.Max(globalMax, currentMax); 18 | } 19 | 20 | return globalMax; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/ListNode.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class ListNode 4 | { 5 | public int val; 6 | public ListNode next; 7 | public ListNode(int val = 0, ListNode next = null) 8 | { 9 | this.val = val; 10 | this.next = next; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Medium-DynamicProgramming/AllUniquePaths.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.Exercises.DynamicProgramming 5 | { 6 | class AllUniquePaths 7 | { 8 | public int UniquePaths(int m, int n) 9 | { 10 | var matrix = new int[m, n]; 11 | 12 | for (int i = 0; i < n; i++) 13 | { 14 | matrix[0, i] = 1; 15 | } 16 | 17 | for (int i = 1; i < m; i++) 18 | { 19 | for (int j = 0; j < n; j++) 20 | { 21 | if (j == 0) 22 | { 23 | matrix[i, j] = 1; 24 | } 25 | else 26 | { 27 | matrix[i, j] = matrix[i - 1, j] + matrix[i, j - 1]; 28 | } 29 | } 30 | } 31 | 32 | return matrix[m - 1, n - 1]; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Medium-DynamicProgramming/CoinChangeDP.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.Exercises.DynamicProgramming 4 | { 5 | class CoinChangeDP 6 | { 7 | public int CoinChange(int[] coins, int amount) 8 | { 9 | var arr = new int[amount + 1]; 10 | 11 | for (int i = 0; i < amount + 1; i++) 12 | { 13 | arr[i] = amount + 1; 14 | } 15 | 16 | arr[0] = 0; 17 | 18 | for (int i = 1; i < coins.Length + 1; i++) 19 | { 20 | var denomination = coins[i - 1]; 21 | 22 | for (int j = denomination; j < amount + 1; j++) 23 | { 24 | arr[j] = Math.Min(arr[j], arr[j - denomination] + 1); 25 | } 26 | } 27 | 28 | return arr[amount] == amount + 1 ? -1 : arr[amount]; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Medium-DynamicProgramming/LongestCommonSubsequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.CommonExercises.Medium_DynamicProgramming 4 | { 5 | class LongestCommonSubsequence 6 | { 7 | public int Solve(string text1, string text2) 8 | { 9 | var m = text1.Length + 1; 10 | var n = text2.Length + 1; 11 | 12 | var dp = new int[m, n]; 13 | 14 | for (int i = 1; i < m; i++) 15 | { 16 | for (int j = 1; j < n; j++) 17 | { 18 | if (text1[i - 1] == text2[j - 1]) 19 | { 20 | dp[i, j] = dp[i - 1, j - 1] + 1; 21 | } 22 | else 23 | { 24 | dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]); 25 | } 26 | } 27 | } 28 | 29 | return dp[m - 1, n - 1]; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Medium-DynamicProgramming/LongestIncreasingSubsequence.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.Exercises.DynamicProgramming 4 | { 5 | class LongestIncreasingSubsequence 6 | { 7 | // o(n2) time o(n) space 8 | public int LengthOfLIS(int[] nums) 9 | { 10 | var LIS = new int[nums.Length]; 11 | var max = 1; 12 | 13 | Array.Fill(LIS, 1); 14 | 15 | for (int i = 1; i < LIS.Length; i++) 16 | { 17 | for (int j = 0; j < i; j++) 18 | { 19 | if (nums[i] > nums[j]) 20 | { 21 | LIS[i]++; 22 | max = Math.Max(max, LIS[i]); 23 | } 24 | } 25 | } 26 | 27 | return max; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Medium-Search/SearchIn2DArrayII.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises.Search 2 | { 3 | class SearchIn2DArrayII 4 | { 5 | public bool SearchMatrix(int[][] matrix, int target) 6 | { 7 | if (matrix == null || matrix.Length == 0) 8 | { 9 | return false; 10 | } 11 | 12 | var i = 0; 13 | var j = matrix[0].Length - 1; 14 | 15 | while (i <= matrix.Length - 1 && j >= 0) 16 | { 17 | if (matrix[i][j] == target) 18 | { 19 | return true; 20 | } 21 | else if (target < matrix[i][j]) 22 | { 23 | j--; 24 | } 25 | else 26 | { 27 | i++; 28 | } 29 | } 30 | 31 | return false; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Monotonic.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class Monotonic 4 | { 5 | public bool isMonotonic(int[] A) 6 | { 7 | bool increasing = true; 8 | bool decreasing = true; 9 | 10 | for (int i = 0; i < A.Length - 1; ++i) 11 | { 12 | if (A[i] > A[i + 1]) 13 | increasing = false; 14 | if (A[i] < A[i + 1]) 15 | decreasing = false; 16 | } 17 | 18 | return increasing || decreasing; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/MoveZeros.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class MoveZeroesClass 4 | { 5 | public void MoveZeroes(int[] nums) 6 | { 7 | var fillIndex = -1; 8 | 9 | for (int i = 0; i < nums.Length; i++) 10 | { 11 | if (fillIndex == -1) 12 | { 13 | fillIndex = nums[i] == 0 ? i : -1; 14 | } 15 | else if (i != fillIndex && nums[i] != 0) 16 | { 17 | nums[fillIndex] = nums[i]; 18 | fillIndex++; 19 | nums[i] = 0; 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/Node.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class Node 4 | { 5 | public int data; 6 | public Node left; 7 | public Node right; 8 | public Node next; 9 | private object val; 10 | 11 | public Node(object val) 12 | { 13 | this.val = val; 14 | } 15 | 16 | Node(int data) 17 | { 18 | this.data = data; 19 | left = null; 20 | right = null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/OddEvenList.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class OddEvenListEx 4 | { 5 | public ListNode OddEvenList(ListNode head) 6 | { 7 | ListNode dummyLeft = new ListNode(); 8 | ListNode dummyRight = new ListNode(); 9 | 10 | ListNode currentLeft = dummyLeft; 11 | ListNode currentRight = dummyRight; 12 | ListNode current = head; 13 | var index = 1; 14 | 15 | while (current != null) 16 | { 17 | if (index % 2 != 0) 18 | { 19 | currentLeft.next = new ListNode(current.val); 20 | currentLeft = currentLeft.next; 21 | } 22 | else 23 | { 24 | currentRight.next = new ListNode(current.val); 25 | currentRight = currentRight.next; 26 | } 27 | 28 | current = current.next; 29 | index++; 30 | } 31 | currentLeft.next = dummyRight.next; 32 | 33 | return dummyLeft.next; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/PathSum.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | class PathSum 4 | { 5 | public class TreeNode 6 | { 7 | public int val; 8 | public TreeNode left; 9 | public TreeNode right; 10 | public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) 11 | { 12 | this.val = val; 13 | this.left = left; 14 | this.right = right; 15 | } 16 | } 17 | 18 | // https://leetcode.com/problems/path-sum/submissions/ 19 | public class Solution 20 | { 21 | public bool HasPathSum(TreeNode root, int sum) 22 | { 23 | if (root == null) 24 | return false; 25 | else if (root.left == null && root.right == null && root.val == sum) 26 | return true; 27 | else 28 | return HasPathSum(root.left, sum - root.val) || HasPathSum(root.right, sum - root.val); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/PeakElement.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class PeakElement 4 | { 5 | public int FindPeakElement(int[] nums) 6 | { 7 | int left = 0; 8 | int right = nums.Length - 1; 9 | 10 | while (left <= right) // IMPORTANT <= 11 | { 12 | var mid = left + (right - left) / 2; // (right + left) / 2 Could lead to overflow ==> https://stackoverflow.com/questions/25571359/why-we-write-lohi-lo-2-in-binary-search 13 | 14 | if (((mid == 0 || nums[mid - 1] <= nums[mid]) && (mid == nums.Length - 1 || nums[mid + 1] <= nums[mid]))) 15 | { 16 | return mid; 17 | } 18 | else if (mid > 0 && nums[mid - 1] > nums[mid]) 19 | { 20 | right = mid - 1; 21 | } 22 | else 23 | { 24 | left = mid + 1; 25 | } 26 | } 27 | 28 | return 0; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/SortColorsEx.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class SortColorsEx 4 | { 5 | public void SortColors(int[] nums) 6 | { 7 | var left = 0; 8 | var right = nums.Length - 1; 9 | var index = 0; 10 | 11 | while (index <= right) 12 | { 13 | var actual = nums[index]; 14 | 15 | if (actual == 0) 16 | { 17 | Swap(left, index, nums); 18 | left++; 19 | index++; 20 | } 21 | else if (actual == 1) 22 | { 23 | index++; 24 | } 25 | else // actual == 2 26 | { 27 | Swap(right, index, nums); 28 | right--; 29 | } 30 | } 31 | } 32 | 33 | public void Swap(int i, int j, int[] nums) 34 | { 35 | var aux = nums[i]; 36 | 37 | nums[i] = nums[j]; 38 | nums[j] = aux; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/SubSequenceCheck.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | class SubSequenceCheck 4 | { 5 | 6 | // problem: https://leetcode.com/problems/is-subsequence/ 7 | // cool video: https://www.youtube.com/watch?v=UsPWwTY0xr4&ab_channel=NareshGupta 8 | 9 | public static bool IsSubsequence(string s, string t) 10 | { 11 | var i = 0; 12 | var j = 0; 13 | 14 | while (i < s.Length && j < t.Length) 15 | { 16 | if (s[i] == s[j]) 17 | { 18 | i++; 19 | } 20 | 21 | j++; 22 | } 23 | 24 | return i == s.Length; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/TreeNode.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class TreeNode 4 | { 5 | public int val; 6 | public TreeNode left; 7 | public TreeNode right; 8 | public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) 9 | { 10 | this.val = val; 11 | this.left = left; 12 | this.right = right; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /InterviewPreparation/CommonExercises/ValidateBST.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.Exercises 2 | { 3 | public class ValidateBST 4 | { 5 | //Recursive way 6 | public bool IsValidBST(TreeNode root) 7 | { 8 | return IsValidBST(root, null, null); 9 | } 10 | 11 | private bool IsValidBST(TreeNode root, int? min, int? max) 12 | { 13 | if (root == null) 14 | { 15 | return true; 16 | } 17 | 18 | if ((min != null && min >= root.val) || (max != null && max <= root.val)) 19 | { 20 | return false; 21 | } 22 | 23 | return IsValidBST(root.left, min, root.val) && IsValidBST(root.right, root.val, max); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /InterviewPreparation/InterviewPreparation.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /InterviewPreparation/KthLargestInBST.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation 5 | { 6 | public class KthLargestInBST 7 | { 8 | public int kthLargest(Node root, int K) 9 | { 10 | IList inorder = new List(); 11 | 12 | if (root != null) 13 | { 14 | Stack stack = new Stack(); 15 | Node curr = root; 16 | 17 | while (curr != null || stack.Count > 0) 18 | { 19 | while (curr != null) 20 | { 21 | stack.Push(curr); 22 | 23 | curr = curr.left; 24 | } 25 | 26 | curr = stack.Pop(); 27 | 28 | inorder.Add(curr.data); 29 | 30 | curr = curr.right; 31 | } 32 | } 33 | 34 | return inorder[inorder.Count - K]; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/AddStrings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class AddStrings 6 | { 7 | public string Solve(string num1, string num2) 8 | { 9 | var carry = 0; 10 | var i = num1.Length - 1; 11 | var j = num2.Length - 1; 12 | var result = new char[Math.Max(i, j) + 2]; 13 | var resultIndex = result.Length - 1; 14 | 15 | while (i >= 0 || j >= 0) 16 | { 17 | var n1 = i < 0 ? 0 : int.Parse(num1[i].ToString()); 18 | var n2 = j < 0 ? 0 : int.Parse(num2[j].ToString()); 19 | 20 | var sum = n1 + n2 + carry; 21 | 22 | result[resultIndex] = (char)('0' + sum % 10); 23 | 24 | carry = sum / 10; 25 | resultIndex--; 26 | i--; 27 | j--; 28 | } 29 | 30 | if (carry > 0) 31 | { 32 | result[0] = (char)('0' + carry); 33 | 34 | return new string(result); 35 | } 36 | else 37 | { 38 | return new string(result).Substring(1, result.Length - 1); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/BalancedHeightTree.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | class BalancedHeightTree 7 | { 8 | public bool IsBalanced(TreeNode root) 9 | { 10 | return IsBalancedAux(root) != -1; 11 | } 12 | 13 | public int IsBalancedAux(TreeNode root) 14 | { 15 | if (root == null) 16 | { 17 | return 0; 18 | } 19 | else 20 | { 21 | var left = IsBalancedAux(root.left); 22 | 23 | if (left == -1) 24 | { 25 | return -1; 26 | } 27 | 28 | var right = IsBalancedAux(root.right); 29 | 30 | if (right == -1) 31 | { 32 | return -1; 33 | } 34 | 35 | if (Math.Abs(left - right) > 1) 36 | { 37 | return -1; 38 | } 39 | 40 | return Math.Max(left, right) + 1; 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/BalancedTreeFromSortedArray.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class BalancedTreeFromSortedArray 6 | { 7 | public TreeNode SortedArrayToBST(int[] nums) 8 | { 9 | return SortedArrayToBST(nums, 0, nums.Length - 1); 10 | } 11 | 12 | public TreeNode SortedArrayToBST(int[] nums, int low, int high) 13 | { 14 | if (low > high) 15 | { 16 | return null; 17 | } 18 | else if (low == high) 19 | { 20 | return new TreeNode(nums[low]); 21 | } 22 | else 23 | { 24 | var mid = low + (high - low) / 2; 25 | var left = SortedArrayToBST(nums, low, mid - 1); 26 | var right = SortedArrayToBST(nums, mid + 1, high); 27 | 28 | return new TreeNode(nums[mid], left, right); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/BestTimeToSellAndBuy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class BestTimeToSellAndBuy 6 | { 7 | public int MaxProfit(int[] prices) 8 | { 9 | var profits = new int[prices.Length]; 10 | var max = 0; 11 | for (int i = 1; i < prices.Length; i++) 12 | { 13 | profits[i] = Math.Max(profits[i - 1] + (prices[i] - prices[i - 1]), 0); 14 | max = Math.Max(profits[i], max); 15 | } 16 | return max; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/BestTimetoBuyandSellStockII.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class BestTimetoBuyandSellStockII 4 | { 5 | public int MaxProfit(int[] prices) 6 | { 7 | var profit = 0; 8 | 9 | for (int i = 1; i < prices.Length; i++) 10 | { 11 | if (prices[i] > prices[i - 1]) 12 | { 13 | profit += prices[i] - prices[i - 1]; 14 | } 15 | } 16 | 17 | return profit; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/BinarySum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class BinarySum 6 | { 7 | public string AddBinary(string a, string b) 8 | { 9 | var result = new char[Math.Max(a.Length, b.Length) + 1]; 10 | 11 | var carry = 0; 12 | var i = a.Length - 1; 13 | var j = b.Length - 1; 14 | var resultIndex = result.Length - 1; 15 | 16 | while (i >= 0 || j >= 0) 17 | { 18 | var aValue = i >= 0 ? int.Parse(a[i].ToString()) : 0; 19 | var bValue = j >= 0 ? int.Parse(b[j].ToString()) : 0; 20 | var sum = aValue + bValue + carry; 21 | 22 | result[resultIndex] = sum == 1 || sum == 3 ? '1' : '0'; 23 | carry = sum == 2 || sum == 3 ? 1 : 0; 24 | 25 | i--; 26 | j--; 27 | resultIndex--; 28 | } 29 | 30 | if (carry == 1) 31 | { 32 | result[0] = '1'; 33 | 34 | return new string(result); 35 | } 36 | 37 | return new string(result).Substring(1, result.Length - 1); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/BinaryTreePath.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | class BinaryTreePath 7 | { 8 | public IList result; 9 | 10 | public IList BinaryTreePaths(TreeNode root) 11 | { 12 | result = new List(); 13 | BinaryTreePathsAux(root, ""); 14 | return result; 15 | } 16 | 17 | private void BinaryTreePathsAux(TreeNode root, string path) 18 | { 19 | if (root != null) 20 | { 21 | if (root.left == null && root.right == null) 22 | { 23 | path += $"{root.val}"; 24 | result.Add(path); 25 | } 26 | path += $"{root.val}->"; 27 | if (root.left != null) 28 | { 29 | BinaryTreePathsAux(root.left, path); 30 | } 31 | if (root.right != null) 32 | { 33 | BinaryTreePathsAux(root.right, path); 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/BuyStock.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class BuyStock 4 | { 5 | public int MaxProfit(int[] prices) 6 | { 7 | var profit = 0; 8 | 9 | for (int i = 1; i < prices.Length; i++) 10 | { 11 | var diff = prices[i] - prices[i - 1]; 12 | if (diff > 0) 13 | { 14 | profit += diff; 15 | } 16 | } 17 | 18 | return profit; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ClimbingStairs.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class ClimbingStairs 4 | { 5 | public int Solve(int n) 6 | { 7 | if (n < 3) 8 | { 9 | return n; 10 | } 11 | 12 | var dp = new int[n]; 13 | 14 | dp[0] = 1; 15 | dp[1] = 2; 16 | 17 | for (int i = 2; i < n; i++) 18 | { 19 | dp[i] = dp[i - 1] + dp[i - 2]; 20 | } 21 | 22 | return dp[n - 1]; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/CountPrimes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class CountPrimes 6 | { 7 | public int Solve(int n) 8 | { 9 | var isPrime = new bool[n]; 10 | 11 | var count = 0; 12 | 13 | for (int i = 2; i < n; i++) 14 | { 15 | isPrime[i] = true; 16 | } 17 | 18 | for (int i = 2; i < Math.Sqrt(n); i++) 19 | { 20 | if (isPrime[i]) 21 | { 22 | for (int j = i * i; j < n; j += i) 23 | { 24 | isPrime[j] = false; 25 | } 26 | } 27 | } 28 | 29 | for (int i = 2; i < n; i++) 30 | { 31 | if (isPrime[i]) 32 | { 33 | count++; 34 | } 35 | } 36 | 37 | return count; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/DeleteDuplicates.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class DeleteDuplicates 6 | { 7 | public ListNode Solve(ListNode head) 8 | { 9 | var actual = head; 10 | while (actual != null && actual.next != null) 11 | { 12 | if (actual.val == actual.next.val) 13 | { 14 | actual.next = actual.next.next; 15 | } 16 | else 17 | { 18 | actual = actual.next; 19 | } 20 | } 21 | return head; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/DeleteNode.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class DeleteNode 6 | { 7 | public void Solve(ListNode node) 8 | { 9 | var actual = node; 10 | ListNode prev = null; 11 | 12 | while (actual != null && actual.next != null) 13 | { 14 | actual.val = actual.next.val; 15 | 16 | prev = actual; 17 | actual = actual.next; 18 | } 19 | 20 | prev.next = null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/DiameterOfBinaryTree.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | class DiameterOfBinaryTree 7 | { 8 | private int diameter; 9 | 10 | public int Solve(TreeNode root) 11 | { 12 | diameter = 0; 13 | 14 | DiameterOfBinaryTreeAux(root); 15 | 16 | return diameter; 17 | } 18 | 19 | public int DiameterOfBinaryTreeAux(TreeNode root) 20 | { 21 | if (root == null) 22 | { 23 | return 0; 24 | } 25 | else 26 | { 27 | var left = DiameterOfBinaryTreeAux(root.left); 28 | var right = DiameterOfBinaryTreeAux(root.right); 29 | 30 | diameter = Math.Max(diameter, left + right); 31 | 32 | return Math.Max(left, right) + 1; 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ExcelTitle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace InterviewPreparation.MicrosoftExcercises.Easy 6 | { 7 | class ExcelTitle 8 | { 9 | public string ConvertToTitle(int columnNumber) 10 | { 11 | var sb = new StringBuilder(); 12 | var stack = new Stack(); 13 | 14 | while (columnNumber > 0) 15 | { 16 | columnNumber--; 17 | stack.Push((char)(columnNumber % 26 + 'A')); 18 | columnNumber /= 26; 19 | } 20 | 21 | while (stack.Count > 0) 22 | { 23 | Console.WriteLine(stack.Peek()); 24 | sb.Append(stack.Pop()); 25 | } 26 | 27 | return sb.ToString(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ExcelTitleToNumber.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class ExcelTitleToNumber 4 | { 5 | public int TitleToNumber(string columnTitle) 6 | { 7 | var result = 0; 8 | 9 | for (int i = 0; i < columnTitle.Length; i++) 10 | { 11 | result = result * 26 + (columnTitle[i] - 'A' + 1); 12 | } 13 | 14 | return result; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/FactorialTralingZeroes.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class FactorialTralingZeroes 4 | { 5 | public int TrailingZeroes(int n) 6 | { 7 | var total = 0; 8 | 9 | while (n > 0) 10 | { 11 | total = n / 5 + total; 12 | n = n / 5; 13 | } 14 | 15 | return total; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/Fibonacci.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class Fibonacci 4 | { 5 | public int Fib(int n) 6 | { 7 | if (n == 0) 8 | { 9 | return 0; 10 | } 11 | 12 | if (n == 1 || n == 2) 13 | { 14 | return 1; 15 | } 16 | 17 | var f1 = 0; 18 | var f2 = 1; 19 | int next; 20 | 21 | for (int i = 1; i < n; i++) 22 | { 23 | next = f1 + f2; 24 | f1 = f2; 25 | f2 = next; 26 | } 27 | 28 | return f2; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/FindSecondMinimumValue.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace InterviewPreparation.MicrosoftExcercises.Easy 6 | { 7 | class FindSecondMinimumValue 8 | { 9 | public int Solve(TreeNode root) 10 | { 11 | var min1 = long.MaxValue; 12 | var result = long.MaxValue; 13 | 14 | var stack = new Stack(); 15 | 16 | stack.Push(root); 17 | 18 | while (stack.Count > 0) 19 | { 20 | var actual = stack.Pop(); 21 | 22 | if (actual.val < min1) 23 | { 24 | result = min1; 25 | min1 = actual.val; 26 | } 27 | else if (actual.val != min1) 28 | { 29 | result = Math.Min(actual.val, result); 30 | } 31 | 32 | if (actual.left != null) 33 | { 34 | stack.Push(actual.left); 35 | stack.Push(actual.right); 36 | } 37 | } 38 | 39 | return result == long.MaxValue ? -1 : (int)result; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/FirstUniqueCharInString.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class FirstUniqueCharInString 4 | { 5 | public int FirstUniqChar(string s) 6 | { 7 | var bucket = new int[26]; 8 | 9 | for (int i = 0; i < s.Length; i++) 10 | { 11 | bucket[s[i] - 'a']++; 12 | } 13 | 14 | for (int i = 0; i < s.Length; i++) 15 | { 16 | if (bucket[s[i] - 'a'] == 1) 17 | { 18 | return i; 19 | } 20 | } 21 | return -1; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/FizzBuzz.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class FizzBuzz 6 | { 7 | public IList Solve(int n) 8 | { 9 | var result = new List(); 10 | 11 | for (int i = 1; i <= n; i++) 12 | { 13 | var div3 = i % 3 == 0; 14 | var div5 = i % 5 == 0; 15 | 16 | if (div3 && div5) 17 | { 18 | result.Add("FizzBuzz"); 19 | } 20 | else if (div3) 21 | { 22 | result.Add("Fizz"); 23 | } 24 | else if (div5) 25 | { 26 | result.Add("Buzz"); 27 | } 28 | else 29 | { 30 | result.Add($"{i}"); 31 | } 32 | } 33 | 34 | return result; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/IntersectionOfTwoList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace InterviewPreparation.MicrosoftExcercises.Easy 6 | { 7 | class IntersectionOfTwoList 8 | { 9 | public int[] Intersection(int[] nums1, int[] nums2) 10 | { 11 | if (nums1.Length > nums2.Length) 12 | { 13 | var aux = nums1; 14 | nums1 = nums2; 15 | nums2 = aux; 16 | } 17 | 18 | var hsNums1 = nums1.ToHashSet(); 19 | var solution = new HashSet(); 20 | 21 | foreach (var num in nums2) 22 | { 23 | if (hsNums1.Contains(num)) 24 | { 25 | solution.Add(num); 26 | } 27 | } 28 | 29 | return solution.ToArray(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/IsPalindrome.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class IsPalindrome 4 | { 5 | public bool Solve(int x) 6 | { 7 | var str = x.ToString(); 8 | int i = 0; 9 | int j = str.Length - 1; 10 | 11 | while (i <= j) 12 | { 13 | if (str[i] != str[j]) 14 | { 15 | return false; 16 | } 17 | 18 | i++; 19 | j--; 20 | } 21 | 22 | return true; 23 | } 24 | 25 | //follow up 26 | public bool Solve2(int x) 27 | { 28 | if (x < 0 || (x % 10 == 0 && x != 0)) 29 | { 30 | return false; 31 | } 32 | 33 | var reversed = 0; 34 | 35 | while (x > reversed) 36 | { 37 | reversed = (10 * reversed) + x % 10; 38 | x = x / 10; 39 | } 40 | 41 | return reversed == x || reversed / 10 == x; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/IsSubtree.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class IsSubtree 6 | { 7 | public bool Solve(TreeNode s, TreeNode t) 8 | { 9 | if (s == null && t == null) return true; 10 | 11 | if (s == null || t == null) return false; 12 | 13 | return Traverse(s, t) || Solve(s.left, t) || Solve(s.right, t); 14 | } 15 | 16 | public bool Traverse(TreeNode s, TreeNode t) 17 | { 18 | if (s == null && t == null) return true; 19 | 20 | if (s == null || t == null) return false; 21 | 22 | return s.val == t.val && Traverse(s.left, t.left) && Traverse(s.right, t.right); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/IsSymmetric.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | class IsSymmetric 7 | { 8 | public bool Solve(TreeNode root) 9 | { 10 | if (root == null) 11 | { 12 | return true; 13 | } 14 | 15 | var queue = new Queue(); 16 | 17 | queue.Enqueue(root); 18 | queue.Enqueue(root); 19 | 20 | while (queue.Count > 0) 21 | { 22 | var n1 = queue.Dequeue(); 23 | var n2 = queue.Dequeue(); 24 | 25 | if (n1 == null && n2 == null) 26 | { 27 | continue; 28 | } 29 | 30 | if ((n1 == null && n2 != null) || 31 | (n1 != null && n2 == null) || 32 | n1.val != n2.val) 33 | { 34 | return false; 35 | } 36 | 37 | queue.Enqueue(n1.left); 38 | queue.Enqueue(n2.right); 39 | queue.Enqueue(n1.right); 40 | queue.Enqueue(n2.left); 41 | } 42 | 43 | return true; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/LinkedListCycle.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class LinkedListCycle 6 | { 7 | public bool HasCycle(ListNode head) 8 | { 9 | if (head == null) 10 | { 11 | return false; 12 | } 13 | 14 | var fast = head; 15 | var slow = head; 16 | 17 | while (fast != null && fast.next != null) 18 | { 19 | fast = fast.next.next; 20 | slow = slow.next; 21 | 22 | if (fast == slow) 23 | { 24 | return true; 25 | } 26 | } 27 | 28 | return false; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/LongestPrefix.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class LongestPrefix 4 | { 5 | public string LongestCommonPrefix(string[] strs) 6 | { 7 | if (strs == null || strs.Length == 0) 8 | { 9 | return ""; 10 | } 11 | 12 | for (int j = 0; j < strs[0].Length; j++) 13 | { 14 | var charAt = strs[0][j]; 15 | 16 | for (int i = 1; i < strs.Length; i++) 17 | { 18 | if (j == strs[i].Length || charAt != strs[i][j]) 19 | { 20 | return strs[0].Substring(0, j); 21 | } 22 | } 23 | } 24 | 25 | return strs[0]; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/LowestCommonAncestor.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class LowestCommonAncestor 6 | { 7 | public TreeNode Solve(TreeNode root, TreeNode p, TreeNode q) 8 | { 9 | var p1 = root; 10 | 11 | while (p1 != null) 12 | { 13 | if (p1.val == p.val) 14 | { 15 | return p; 16 | } 17 | 18 | if (p1.val == q.val) 19 | { 20 | return q; 21 | } 22 | 23 | if ((p.val > p1.val && q.val < p1.val) || (q.val > p1.val && p.val < p1.val)) 24 | { 25 | return p1; 26 | } 27 | 28 | if (p.val > p1.val) 29 | { 30 | p1 = p1.right; 31 | } 32 | else 33 | { 34 | p1 = p1.left; 35 | } 36 | } 37 | 38 | return p1; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/LowestCommonAncestorI.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class LowestCommonAncestorI 6 | { 7 | public TreeNode Solve(TreeNode root, TreeNode p, TreeNode q) 8 | { 9 | var actual = root; 10 | 11 | while (actual != null) 12 | { 13 | if ((actual.val >= p.val && actual.val <= q.val) || 14 | (actual.val <= p.val && actual.val >= q.val)) 15 | { 16 | return actual; 17 | } 18 | else if (actual.val > p.val && actual.val > q.val) 19 | { 20 | actual = actual.left; 21 | } 22 | else 23 | { 24 | actual = actual.right; 25 | } 26 | } 27 | 28 | return actual; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/MajorityElement.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class MajorityElement 4 | { 5 | public int Solve(int[] nums) 6 | { 7 | int? candidate = null; 8 | int count = 0; 9 | 10 | foreach (var num in nums) 11 | { 12 | if (candidate == null) 13 | { 14 | candidate = num; 15 | count++; 16 | } 17 | else if (num == candidate) 18 | { 19 | count++; 20 | } 21 | else 22 | { 23 | count--; 24 | 25 | if (count == 0) 26 | { 27 | candidate = num; 28 | count = 1; 29 | } 30 | } 31 | } 32 | 33 | return candidate.Value; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/MinStack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | public class MinStack 7 | { 8 | 9 | private LinkedList minStack = new LinkedList(); 10 | private int min = int.MaxValue; 11 | 12 | /** initialize your data structure here. */ 13 | public MinStack() 14 | { 15 | minStack = new LinkedList(); 16 | min = int.MaxValue; 17 | } 18 | 19 | public void Push(int x) 20 | { 21 | minStack.AddFirst(x); 22 | 23 | min = Math.Min(min, x); 24 | } 25 | 26 | public void Pop() 27 | { 28 | var firtValue = minStack.First.Value; 29 | minStack.RemoveFirst(); 30 | 31 | if (min == firtValue) 32 | { 33 | min = int.MaxValue; 34 | 35 | foreach (var val in minStack) 36 | { 37 | min = Math.Min(val, min); 38 | } 39 | } 40 | } 41 | 42 | public int Top() 43 | { 44 | return minStack.First.Value; 45 | } 46 | 47 | public int GetMin() 48 | { 49 | return min; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/MoveZeroes.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class MoveZeroes 4 | { 5 | public void Solve(int[] nums) 6 | { 7 | var leftPosition = 0; 8 | 9 | for (int i = 0; i < nums.Length; i++) 10 | { 11 | if (nums[i] != 0) 12 | { 13 | nums[leftPosition] = nums[i]; 14 | if (i != leftPosition) 15 | { 16 | nums[i] = 0; 17 | } 18 | 19 | leftPosition++; 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/MyHashMap.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | public class MyHashMap 4 | { 5 | 6 | private int[] map; 7 | 8 | /** Initialize your data structure here. */ 9 | public MyHashMap() 10 | { 11 | map = new int[1000001]; 12 | } 13 | 14 | /** value will always be non-negative. */ 15 | public void Put(int key, int value) 16 | { 17 | map[key] = value + 1; 18 | } 19 | 20 | /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */ 21 | public int Get(int key) 22 | { 23 | return map[key] - 1; 24 | } 25 | 26 | /** Removes the mapping of the specified value key if this map contains a mapping for the key */ 27 | public void Remove(int key) 28 | { 29 | map[key] = 0; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/NumberOfBalloons.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class NumberOfBalloons 6 | { 7 | public int MaxNumberOfBalloons(string text) 8 | { 9 | var pattern = "balloon"; 10 | var freqText = BuildFrequency(text); 11 | var freqPattern = BuildFrequency(pattern); 12 | var total = int.MaxValue; 13 | 14 | for (int i = 0; i < 26; i++) 15 | { 16 | if (freqPattern[i] > 0) 17 | { 18 | total = Math.Min(total, freqText[i] / freqPattern[i]); 19 | } 20 | } 21 | 22 | return total; 23 | } 24 | 25 | private int[] BuildFrequency(string str) 26 | { 27 | var freq = new int[26]; 28 | 29 | foreach (var currentChar in str) 30 | { 31 | freq[currentChar - 'a']++; 32 | } 33 | 34 | return freq; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/PalindromeLinkedList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class PalindromeLinkedList 6 | { 7 | public bool IsPalindrome(ListNode head) 8 | { 9 | if (head == null) 10 | { 11 | return true; 12 | } 13 | 14 | var fast = head; 15 | var slow = head; 16 | ListNode prev = null; 17 | 18 | while (fast != null && fast.next != null) 19 | { 20 | fast = fast.next.next; 21 | 22 | var tmp = slow.next; 23 | slow.next = prev; 24 | prev = slow; 25 | 26 | slow = tmp; 27 | } 28 | 29 | if (fast != null && fast.next == null) 30 | { 31 | slow = slow.next; 32 | } 33 | 34 | while (slow != null) 35 | { 36 | if (prev.val != slow.val) 37 | { 38 | return false; 39 | } 40 | 41 | slow = slow.next; 42 | prev = prev.next; 43 | } 44 | 45 | return true; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/PascalTriangle.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | class PascalTriangle 7 | { 8 | public IList> Generate(int numRows) 9 | { 10 | var result = new List>(); 11 | var level = 1; 12 | List lastLevel = null; 13 | 14 | for (int i = 0; i < numRows; i++) 15 | { 16 | var actualLevel = new List(); 17 | for (int j = 0; j < level; j++) 18 | { 19 | if (j == 0 || j == level - 1) 20 | { 21 | actualLevel.Add(1); 22 | } 23 | else 24 | { 25 | actualLevel.Add(lastLevel.ElementAt(j - 1) + lastLevel.ElementAt(j)); 26 | } 27 | } 28 | 29 | result.Add(actualLevel); 30 | lastLevel = actualLevel; 31 | level++; 32 | } 33 | return result; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/PathSum.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class PathSum 6 | { 7 | public bool HasPathSum(TreeNode root, int sum) 8 | { 9 | if (root == null) 10 | { 11 | return false; 12 | } 13 | 14 | if (sum - root.val == 0 && root.left == null && root.right == null) 15 | { 16 | return true; 17 | } 18 | 19 | return HasPathSum(root.left, sum - root.val) || HasPathSum(root.right, sum - root.val); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/PeakElement.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class PeakElement 4 | { 5 | public int PeakIndexInMountainArray(int[] arr) 6 | { 7 | var low = 0; 8 | var high = arr.Length - 1; 9 | while (low < high) 10 | { 11 | var mid = low + (high - low) / 2; 12 | 13 | if (mid == 0 || mid == arr.Length - 1 || (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1])) 14 | { 15 | return mid; 16 | } 17 | else if (arr[mid] > arr[mid + 1] && arr[mid] < arr[mid - 1]) 18 | { 19 | high = mid; 20 | } 21 | else 22 | { 23 | low = mid + 1; 24 | } 25 | } 26 | return -1; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/PivotIndex.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class PivotIndex 4 | { 5 | public int Solve(int[] nums) 6 | { 7 | if (nums.Length == 1) 8 | { 9 | return 0; 10 | } 11 | var n = nums.Length; 12 | var suffixSum = new int[n]; 13 | suffixSum[n - 1] = nums[n - 1]; 14 | for (int i = n - 2; i > 0; i--) 15 | { 16 | suffixSum[i] = nums[i] + suffixSum[i + 1]; 17 | } 18 | if (suffixSum[1] == 0) 19 | { 20 | return 0; 21 | } 22 | var prefixSum = nums[0]; 23 | for (int i = 1; i <= n - 2; i++) 24 | { 25 | if (prefixSum == suffixSum[i + 1]) 26 | { 27 | return i; 28 | } 29 | prefixSum += nums[i]; 30 | } 31 | if (prefixSum == 0) 32 | { 33 | return n - 1; 34 | } 35 | return -1; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/PlusOne.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | class PlusOne 7 | { 8 | public int[] Solve(int[] digits) 9 | { 10 | var carry = 1; 11 | 12 | var list = new LinkedList(); 13 | 14 | for (int i = digits.Length - 1; i >= 0; i--) 15 | { 16 | var sum = digits[i] + carry; 17 | 18 | 19 | list.AddFirst(sum % 10); 20 | carry = sum / 10; 21 | } 22 | 23 | if (carry > 0) 24 | { 25 | list.AddFirst(carry); 26 | } 27 | 28 | return list.ToArray(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/RectangleOverlap.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class RectangleOverlap 4 | { 5 | public bool IsRectangleOverlap(int[] rec1, int[] rec2) 6 | { 7 | if (rec1[0] == rec1[2] || rec1[1] == rec1[3] || 8 | rec2[0] == rec2[2] || rec2[1] == rec2[3]) 9 | { 10 | // the line cannot have positive overlap 11 | return false; 12 | } 13 | 14 | return !(rec1[2] <= rec2[0] || // left 15 | rec1[3] <= rec2[1] || // bottom 16 | rec1[0] >= rec2[2] || // right 17 | rec1[1] >= rec2[3]); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/RemoveElements.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class RemoveElements 4 | { 5 | public int RemoveDuplicates(int[] nums) 6 | { 7 | if (nums.Length < 2) 8 | { 9 | return nums.Length; 10 | } 11 | var leftPosition = 0; 12 | 13 | for (int i = 1; i < nums.Length; i++) 14 | { 15 | if (nums[leftPosition] != nums[i]) 16 | { 17 | leftPosition++; 18 | nums[leftPosition] = nums[i]; 19 | } 20 | } 21 | 22 | return leftPosition + 1; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/RemoveElementsFromList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class RemoveElementsFromList 6 | { 7 | public ListNode RemoveElements(ListNode head, int val) 8 | { 9 | var actual = head; 10 | var dummy = new ListNode(0, head); 11 | var prev = dummy; 12 | 13 | while (actual != null) 14 | { 15 | if (actual.val == val) 16 | { 17 | prev.next = actual.next; 18 | } 19 | else 20 | { 21 | prev = actual; 22 | } 23 | 24 | actual = actual.next; 25 | } 26 | 27 | return dummy.next; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ReverseInteger.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class ReverseInteger 4 | { 5 | public int Reverse(int x) 6 | { 7 | var lastPositiveDigit = int.MaxValue % 10; 8 | var lastNegativeDigit = int.MinValue % 10; 9 | 10 | var rev = 0; 11 | 12 | while (x != 0) 13 | { 14 | var digit = x % 10; 15 | 16 | if (rev > int.MaxValue / 10 || (rev == int.MaxValue && digit > lastPositiveDigit)) 17 | { 18 | return 0; 19 | } 20 | 21 | if (rev < int.MinValue / 10 || (rev == int.MinValue && digit > lastNegativeDigit)) 22 | { 23 | return 0; 24 | } 25 | 26 | rev = rev * 10 + digit; 27 | 28 | x /= 10; 29 | } 30 | 31 | return rev; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ReverseOnlyLetters.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class ReverseOnlyLetters 6 | { 7 | public string Solve(string s) 8 | { 9 | var next = 0; 10 | var right = s.Length - 1; 11 | var sb = new StringBuilder(); 12 | 13 | for (int i = right; i >= 0; i--) 14 | { 15 | while (next < s.Length && !char.IsLetter(s[next])) 16 | { 17 | sb.Append(s[next]); 18 | next++; 19 | } 20 | 21 | if (char.IsLetter(s[i])) 22 | { 23 | sb.Append(s[i]); 24 | 25 | next++; 26 | } 27 | } 28 | 29 | while (next < s.Length && !char.IsLetter(s[next])) 30 | { 31 | sb.Append(s[next]); 32 | next++; 33 | } 34 | 35 | return sb.ToString(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ReverseString.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class ReverseString 4 | { 5 | public void Solve(char[] s) 6 | { 7 | int i = 0; 8 | int j = s.Length - 1; 9 | char tmp; 10 | 11 | while (i < j) 12 | { 13 | tmp = s[i]; 14 | s[i] = s[j]; 15 | s[j] = tmp; 16 | 17 | i++; 18 | j--; 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ReverseString2.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | public class RevereString2 4 | { 5 | public string ReverseStr(string s, int k) 6 | { 7 | var result = new StringBuilder(); 8 | 9 | int i = 0; 10 | 11 | while (i < s.Length) 12 | { 13 | if (i % (2 * k) == 0) 14 | { 15 | Reverse(s, i, k, result); 16 | 17 | i = i + k; 18 | } 19 | else 20 | { 21 | result.Append(s[i]); 22 | 23 | i++; 24 | } 25 | } 26 | 27 | return result.ToString(); 28 | } 29 | 30 | public void Reverse(string s, int start, int k, StringBuilder sb) 31 | { 32 | var end = start + k > s.Length ? s.Length : start + k; 33 | 34 | for (int i = end - 1; i >= start; i--) 35 | { 36 | sb.Append(s[i]); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/RomanToInteger.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class RomanToInteger 6 | { 7 | public int RomanToInt(string s) 8 | { 9 | var number = 0; 10 | var dict = new Dictionary() 11 | { 12 | {'I', 1}, 13 | {'V', 5}, 14 | {'X', 10}, 15 | {'L', 50}, 16 | {'C', 100}, 17 | {'D', 500}, 18 | {'M', 1000} 19 | }; 20 | 21 | for (int i = 0; i < s.Length; i++) 22 | { 23 | var actual = s[i]; 24 | 25 | if (i < s.Length - 1 && dict[actual] < dict[s[i + 1]]) 26 | { 27 | number -= dict[actual]; 28 | } 29 | else 30 | { 31 | number += dict[actual]; 32 | } 33 | } 34 | 35 | return number; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/SQRT.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class SQRT 4 | { 5 | public int MySqrt(int x) 6 | { 7 | if (x == 0) 8 | { 9 | return 0; 10 | } 11 | 12 | long low = 1; 13 | long high = (int.MaxValue / 2) + 1; 14 | 15 | while (low < high) 16 | { 17 | long mid = low + (high - low) / 2; 18 | long result = mid * mid; 19 | 20 | if (result == x) 21 | { 22 | return (int)mid; 23 | } 24 | else if (result > x) 25 | { 26 | high = mid; 27 | } 28 | else 29 | { 30 | low = mid + 1; 31 | } 32 | } 33 | 34 | return (int)low - 1; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/SmallestRange.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class SmallestRange 6 | { 7 | public int SmallestRangeI(int[] A, int k) 8 | { 9 | var min = A[0]; 10 | var max = A[0]; 11 | 12 | for (int i = 1; i < A.Length; i++) 13 | { 14 | min = Math.Min(min, A[i]); 15 | max = Math.Max(max, A[i]); 16 | } 17 | 18 | return Math.Max(0, (max - k) - (min + k)); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/SummaryRanges.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Easy 4 | { 5 | class SummaryRanges 6 | { 7 | public IList Solve(int[] nums) 8 | { 9 | var result = new List(); 10 | int i = 0; 11 | int? prev; 12 | 13 | while (i < nums.Length) 14 | { 15 | var start = nums[i]; 16 | prev = null; 17 | 18 | while (i < nums.Length && (prev == null || (nums[i] - prev == 1))) 19 | { 20 | prev = nums[i]; 21 | i++; 22 | } 23 | 24 | var range = start == prev ? $"{start}" : $"{start}->{prev}"; 25 | 26 | result.Add(range); 27 | } 28 | 29 | return result; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/TwoSumSorted.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class TwoSumSorted 4 | { 5 | public int[] TwoSum(int[] numbers, int target) 6 | { 7 | var i = 0; 8 | var j = numbers.Length - 1; 9 | 10 | while (i < j) 11 | { 12 | if (numbers[i] + numbers[j] == target) 13 | { 14 | return new int[] { i + 1, j + 1 }; 15 | } 16 | else if (numbers[i] + numbers[j] > target) 17 | { 18 | j--; 19 | } 20 | else 21 | { 22 | i++; 23 | } 24 | } 25 | 26 | return null; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/UniqueEmailAddress.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Easy 5 | { 6 | class UniqueEmailAddress 7 | { 8 | public int NumUniqueEmails(string[] emails) 9 | { 10 | var finalEmails = new HashSet(); 11 | 12 | foreach (var email in emails) 13 | { 14 | var parseEmail = new StringBuilder(); 15 | var atSignfound = false; 16 | var plusSignFound = false; 17 | 18 | for (var i = 0; i < email.Length; i++) 19 | { 20 | var actual = email[i]; 21 | 22 | atSignfound |= actual == '@'; 23 | plusSignFound |= actual == '+'; 24 | 25 | if (atSignfound) 26 | { 27 | parseEmail.Append(actual); 28 | } 29 | else if (!plusSignFound && actual != '.') 30 | { 31 | parseEmail.Append(actual); 32 | } 33 | } 34 | 35 | finalEmails.Add(parseEmail.ToString()); 36 | 37 | } 38 | 39 | return finalEmails.Count; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/VPalindrome.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class VPalindrome 4 | { 5 | public bool IsPalindrome(string s) 6 | { 7 | var i = 0; 8 | var j = s.Length - 1; 9 | 10 | while (i < j) 11 | { 12 | while (!char.IsLetterOrDigit(s[i]) && i < j) 13 | { 14 | i++; 15 | } 16 | 17 | while (!char.IsLetterOrDigit(s[j]) && i < j) 18 | { 19 | j--; 20 | } 21 | 22 | if (char.ToLower(s[i]) != char.ToLower(s[j])) 23 | { 24 | return false; 25 | } 26 | 27 | i++; 28 | j--; 29 | } 30 | 31 | return true; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Easy/ValidAnagram.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Easy 2 | { 3 | class ValidAnagram 4 | { 5 | public bool IsAnagram(string s, string t) 6 | { 7 | var bucket = new int[26]; 8 | 9 | for (int i = 0; i < s.Length; i++) 10 | { 11 | bucket[s[i] - 'a']++; 12 | } 13 | 14 | for (int i = 0; i < t.Length; i++) 15 | { 16 | bucket[t[i] - 'a']--; 17 | } 18 | 19 | 20 | for (int i = 0; i < bucket.Length; i++) 21 | { 22 | if (bucket[i] != 0) 23 | { 24 | return false; 25 | } 26 | } 27 | 28 | return true; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Hard/FirstMissingPositiveSWAP.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Hard 2 | { 3 | class FirstMissingPositiveSWAP 4 | { 5 | public int FirstMissingPositive(int[] nums) 6 | { 7 | for (int i = 0; i < nums.Length; i++) 8 | { 9 | while (nums[i] > 0 && nums[i] - 1 < nums.Length && 10 | nums[i] != i + 1 && 11 | nums[i] != nums[nums[i] - 1]) 12 | { 13 | var tmp = nums[i]; 14 | nums[i] = nums[nums[i] - 1]; 15 | nums[tmp - 1] = tmp; 16 | } 17 | } 18 | 19 | for (int i = 0; i < nums.Length; i++) 20 | { 21 | if (nums[i] != i + 1) 22 | { 23 | return i + 1; 24 | } 25 | } 26 | 27 | return nums.Length + 1; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Hard/LargestHistogram.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Hard 5 | { 6 | class LargestHistogram 7 | { 8 | public int Solve(int[] hist) 9 | { 10 | var max = 0; 11 | var stack = new Stack(); 12 | 13 | for (int i = 0; i <= hist.Length; i++) 14 | { 15 | var bar = i == hist.Length ? 0 : hist[i]; 16 | 17 | while (stack.Count > 0 && hist[stack.Peek()] >= bar) 18 | { 19 | var prevHeight = hist[stack.Pop()]; 20 | var leftBount = stack.Count > 0 ? stack.Pop() : -1; 21 | var diff = i - leftBount - 1; 22 | 23 | max = Math.Max(max, prevHeight * diff); 24 | } 25 | 26 | stack.Push(i); 27 | } 28 | 29 | return max; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Hard/SlidingWindowMaximum.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Hard 4 | { 5 | class SlidingWindowMaximum 6 | { 7 | public int[] Solve(int[] nums, int k) 8 | { 9 | if (k == 1) 10 | { 11 | return nums; 12 | } 13 | 14 | var deque = new LinkedList(); 15 | var result = new int[nums.Length - k + 1 < 0 ? 1 : nums.Length - k + 1]; 16 | 17 | for (int i = 0; i < nums.Length; i++) 18 | { 19 | while (deque.Count > 0 && deque.First.Value < i - k + 1) 20 | { 21 | deque.RemoveFirst(); 22 | } 23 | 24 | while (deque.Count > 0 && nums[i] > nums[deque.Last.Value]) 25 | { 26 | deque.RemoveLast(); 27 | } 28 | 29 | deque.AddLast(i); 30 | 31 | if (i >= k - 1) 32 | { 33 | result[i - k + 1] = nums[deque.First.Value]; 34 | } 35 | } 36 | 37 | return result; 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Hard/WildcardMatching.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Hard 2 | { 3 | class WildcardMatching 4 | { 5 | public bool IsMatch(string s, string p) 6 | { 7 | var cache = new bool?[s.Length + 1, p.Length + 1]; 8 | 9 | return IsMatch(s, p, 0, 0, cache); 10 | } 11 | 12 | private bool IsMatch(string s, string p, int sI, int pI, bool?[,] cache) 13 | { 14 | if (pI == p.Length) 15 | { 16 | return s.Length == sI; 17 | } 18 | 19 | if (cache[sI, pI] != null) 20 | { 21 | return cache[sI, pI].Value; 22 | } 23 | 24 | var firstMatch = sI < s.Length && (s[sI] == p[pI] || p[pI] == '?'); 25 | var result = false; 26 | 27 | if (p[pI] == '*') 28 | { 29 | result = IsMatch(s, p, sI, pI + 1, cache) || (sI < s.Length && IsMatch(s, p, sI + 1, pI, cache)); 30 | } 31 | else 32 | { 33 | result = firstMatch && IsMatch(s, p, sI + 1, pI + 1, cache); 34 | } 35 | 36 | cache[sI, pI] = result; 37 | 38 | return result; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/BSTIterator.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace InterviewPreparation.MicrosoftExcercises.Medium 6 | { 7 | public class BSTIterator 8 | { 9 | 10 | private int Index; 11 | private IList Iterator; 12 | 13 | public BSTIterator(TreeNode root) 14 | { 15 | Index = 0; 16 | Iterator = new List(); 17 | 18 | var actual = root; 19 | var stack = new Stack(); 20 | 21 | while (actual != null || stack.Count > 0) 22 | { 23 | while (actual != null) 24 | { 25 | stack.Push(actual); 26 | actual = actual.left; 27 | } 28 | 29 | actual = stack.Pop(); 30 | 31 | Iterator.Add(actual); 32 | 33 | actual = actual.right; 34 | } 35 | } 36 | 37 | public int Next() 38 | { 39 | return Iterator.ElementAt(Index++).val; 40 | } 41 | 42 | public bool HasNext() 43 | { 44 | return Index < Iterator.Count; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/BattleShip.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class BattleShip 4 | { 5 | public int CountBattleships(char[][] board) 6 | { 7 | var count = 0; 8 | for (int i = 0; i < board.Length; i++) 9 | { 10 | for (int j = 0; j < board[i].Length; j++) 11 | { 12 | if (board[i][j] == 'X' && 13 | ((i == 0 && j == 0) || (i == 0 && j > 0 && board[i][j - 1] != 'X') || 14 | (j == 0 && i > 0 && board[i - 1][j] != 'X') || 15 | (i > 0 && j > 0 && board[i][j - 1] != 'X' && board[i - 1][j] != 'X'))) 16 | { 17 | count++; 18 | } 19 | } 20 | } 21 | return count; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/BuildingsWithanOceanView.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class BuildingsWithanOceanView 6 | { 7 | public int[] FindBuildings(int[] arr) 8 | { 9 | var maxRightSoFar = -1; 10 | var stack = new Stack(); 11 | 12 | for (int i = arr.Length - 1; i >= 0; i--) 13 | { 14 | if (maxRightSoFar >= arr[i]) 15 | { 16 | continue; 17 | } 18 | 19 | maxRightSoFar = arr[i]; 20 | 21 | stack.Push(i); 22 | } 23 | 24 | var result = new int[stack.Count]; 25 | var index = 0; 26 | 27 | while (stack.Count > 0) 28 | { 29 | result[index++] = stack.Pop(); 30 | } 31 | 32 | return result; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CalendarI.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | public class CalendarI 6 | { 7 | private List books; 8 | 9 | public CalendarI() 10 | { 11 | books = new List(); 12 | } 13 | 14 | public bool Book(int start, int end) 15 | { 16 | foreach (var book in books) 17 | { 18 | if (!Avaliable(book, start, end)) 19 | { 20 | return false; 21 | } 22 | } 23 | 24 | books.Add(new int[] { start, end }); 25 | 26 | return true; 27 | } 28 | 29 | private bool Avaliable(int[] book, int start, int end) 30 | { 31 | return !(book[0] < end && start < book[1]); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CanJump.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class CanJump 6 | { 7 | public bool Solve(int[] nums) 8 | { 9 | int canWalk = nums[0]; 10 | for (int i = 1; i < nums.Length; i++) 11 | { 12 | if (canWalk == 0) 13 | { 14 | return false; 15 | } 16 | 17 | canWalk--; 18 | canWalk = Math.Max(canWalk, nums[i]); 19 | } 20 | return true; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CanPartition.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class CanPartition 4 | { 5 | public bool Solve(int[] nums) 6 | { 7 | var sum = 0; 8 | 9 | foreach (var num in nums) 10 | { 11 | sum += num; 12 | } 13 | 14 | if (sum % 2 == 0) 15 | { 16 | return CanPartitionAux(nums, sum / 2); 17 | } 18 | 19 | return false; 20 | } 21 | 22 | private bool CanPartitionAux(int[] nums, int target) 23 | { 24 | var n = nums.Length; 25 | var m = target + 1; 26 | var dp = new bool[m]; 27 | dp[0] = true; 28 | 29 | 30 | foreach (var num in nums) 31 | { 32 | for (int j = target; j >= num; j--) 33 | { 34 | dp[j] = dp[j] || dp[j - num]; 35 | } 36 | } 37 | 38 | return dp[m - 1]; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CheckSubarraySum.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class CheckSubarraySum 6 | { 7 | public bool Solve(int[] nums, int k) 8 | { 9 | var indexes = new Dictionary() 10 | { 11 | {0, -1} 12 | }; 13 | var sum = 0; 14 | 15 | for (int i = 0; i < nums.Length; i++) 16 | { 17 | sum = (sum + nums[i]) % k; 18 | 19 | if (indexes.ContainsKey(sum)) 20 | { 21 | var index = indexes[sum]; 22 | 23 | if (i - index > 1) 24 | { 25 | return true; 26 | } 27 | } 28 | else 29 | { 30 | indexes.Add(sum, i); 31 | } 32 | } 33 | 34 | return false; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/ClosestKPoints.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class ClosestKPoints 7 | { 8 | public int[][] KClosest(int[][] points, int k) 9 | { 10 | return points.OrderBy(point => Math.Abs(point[0] * point[0] + point[1] * point[1])).Take(k).ToArray(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CoinChange.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class CoinChange 6 | { 7 | public int Solve(int[] coins, int amount) 8 | { 9 | var dp = new int[amount + 1]; 10 | 11 | for (int i = 0; i < dp.Length; i++) 12 | { 13 | dp[i] = amount + 1; 14 | } 15 | 16 | dp[0] = 0; 17 | 18 | foreach (var coin in coins) 19 | { 20 | for (int i = coin; i < dp.Length; i++) 21 | { 22 | dp[i] = Math.Min(dp[i - coin] + 1, dp[i]); 23 | } 24 | } 25 | 26 | return dp[amount] == amount + 1 ? -1 : dp[dp.Length - 1]; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CompareVersion.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class CompareVersion 4 | { 5 | public int Solve(string v1, string v2) 6 | { 7 | var p1 = 0; 8 | var p2 = 0; 9 | 10 | while (p1 < v1.Length || p2 < v2.Length) 11 | { 12 | var val1 = 0; 13 | var val2 = 0; 14 | 15 | while (p1 < v1.Length && v1[p1] != '.') 16 | { 17 | val1 = val1 * 10 + v1[p1] - '0'; 18 | p1++; 19 | } 20 | 21 | while (p2 < v2.Length && v2[p2] != '.') 22 | { 23 | val2 = val2 * 10 + v2[p2] - '0'; 24 | p2++; 25 | } 26 | 27 | if (val1 > val2) 28 | { 29 | return 1; 30 | } 31 | else if (val1 < val2) 32 | { 33 | return -1; 34 | } 35 | else 36 | { 37 | p1++; 38 | p2++; 39 | } 40 | } 41 | 42 | return 0; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CompressString.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class CompressString 4 | { 5 | public int Compress(char[] chars) 6 | { 7 | if (chars.Length == 1) 8 | { 9 | return 1; 10 | } 11 | var actualIndex = 0; 12 | int i = 0; 13 | 14 | while (i < chars.Length) 15 | { 16 | int counter = 0; 17 | var letterGroup = chars[i]; 18 | 19 | while (i < chars.Length && chars[i] == letterGroup) 20 | { 21 | counter++; 22 | i++; 23 | } 24 | 25 | chars[actualIndex] = letterGroup; 26 | actualIndex++; 27 | 28 | if (counter > 1) 29 | { 30 | var counterString = counter.ToString(); 31 | 32 | for (int j = 0; j < counterString.Length; j++) 33 | { 34 | chars[actualIndex] = counterString[j]; 35 | actualIndex++; 36 | } 37 | } 38 | } 39 | 40 | return actualIndex; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/ContainerWithMostWater.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class ContainerWithMostWater 6 | { 7 | public int MaxArea(int[] height) 8 | { 9 | var max = 0; 10 | var i = 0; 11 | var j = height.Length - 1; 12 | 13 | while (i < j) 14 | { 15 | var length = j - i; 16 | int actualHeight; 17 | 18 | if (height[i] == height[j]) 19 | { 20 | actualHeight = height[j]; 21 | j--; 22 | i++; 23 | } 24 | else if (height[i] > height[j]) 25 | { 26 | actualHeight = height[j]; 27 | j--; 28 | } 29 | else 30 | { 31 | actualHeight = height[i]; 32 | i++; 33 | } 34 | 35 | max = Math.Max(max, length * actualHeight); 36 | } 37 | 38 | return max; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CountAllPalindromesDP.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class CountAllPalindromesDP 6 | { 7 | public int CountSubstrings(string s) 8 | { 9 | var n = s.Length; 10 | 11 | var dp = new bool[n, n]; 12 | var total = 0; 13 | 14 | for (int i = 0; i < n; i++) 15 | { 16 | dp[i, i] = true; 17 | } 18 | 19 | var row = 0; 20 | 21 | for (int i = n - 1 - row; i >= 0; i--) 22 | { 23 | for (int j = i; j < n; j++) 24 | { 25 | if (j == i) 26 | { 27 | dp[i, j] = true; 28 | total++; 29 | } 30 | else if (s[i] == s[j] && (Math.Abs(i - j) == 1 || dp[i + 1, j - 1])) 31 | { 32 | dp[i, j] = true; 33 | total++; 34 | } 35 | } 36 | 37 | row++; 38 | } 39 | 40 | return total; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CountAndSay.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class CountAndSay 6 | { 7 | public string Solve(int n) 8 | { 9 | var actual = "1"; 10 | for (int i = 1; i < n; i++) 11 | { 12 | var next = new StringBuilder(); 13 | int j = 0; 14 | while (j < actual.Length) 15 | { 16 | var count = 1; 17 | var currentNumber = actual[j]; 18 | while (j + 1 < actual.Length && actual[j] == actual[j + 1]) 19 | { 20 | count++; 21 | j++; 22 | } 23 | next.Append($"{count}{currentNumber}"); 24 | j++; 25 | } 26 | actual = next.ToString(); 27 | } 28 | return actual; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/CountSquares.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class CountSquares 6 | { 7 | public int Solve(int[][] matrix) 8 | { 9 | var M = matrix.Length; 10 | var N = matrix[0].Length; 11 | var dp = new int[M + 1, N + 1]; 12 | var total = 0; 13 | 14 | for (int i = 1; i <= M; i++) 15 | { 16 | for (int j = 1; j <= N; j++) 17 | { 18 | if (matrix[i - 1][j - 1] == 1) 19 | { 20 | dp[i, j] = Math.Min( 21 | Math.Min(dp[i - 1, j - 1], dp[i - 1, j]), 22 | dp[i, j - 1]) + 1; 23 | 24 | total += dp[i, j]; 25 | } 26 | } 27 | } 28 | 29 | return total; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/DecodeWays.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class DecodeWays 6 | { 7 | public int NumDecodings(string s) 8 | { 9 | var cache = new Dictionary(); 10 | 11 | return NumDecodings(s, 0, cache); 12 | } 13 | 14 | private int NumDecodings(string s, int start, Dictionary cache) 15 | { 16 | if (start == s.Length) 17 | { 18 | return 1; 19 | } 20 | 21 | if (cache.ContainsKey(start)) 22 | { 23 | return cache[start]; 24 | } 25 | 26 | var count = 0; 27 | 28 | for (int i = 1; i <= 2 && i + start <= s.Length; i++) 29 | { 30 | var actual = int.Parse(s.Substring(start, i)); 31 | 32 | if (actual == 0 || actual > 26) 33 | { 34 | break; 35 | } 36 | 37 | count += NumDecodings(s, i + start, cache); 38 | } 39 | 40 | cache[start] = count; 41 | 42 | return count; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/DetectCycleII.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class DetectCycleII 6 | { 7 | public ListNode DetectCycle(ListNode head) 8 | { 9 | var fast = head; 10 | var slow = head; 11 | 12 | do 13 | { 14 | fast = fast?.next?.next; 15 | slow = slow?.next; 16 | } while (fast != null && fast?.next != null && fast != slow); 17 | 18 | 19 | if (fast == null || fast?.next == null) 20 | { 21 | return null; 22 | } 23 | 24 | slow = head; 25 | 26 | while (slow != fast) 27 | { 28 | slow = slow.next; 29 | fast = fast.next; 30 | } 31 | 32 | return fast; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/DistribuiteCoins.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class DistribuiteCoins 7 | { 8 | public int Solve(TreeNode root) 9 | { 10 | var moves = 0; 11 | 12 | DFS(root, ref moves); 13 | 14 | return moves; 15 | } 16 | 17 | public int DFS(TreeNode root, ref int moves) 18 | { 19 | if (root == null) 20 | { 21 | return 0; 22 | } 23 | 24 | var leftOverload = DFS(root.left, ref moves); 25 | var rightOverload = DFS(root.right, ref moves); 26 | 27 | moves += Math.Abs(leftOverload) + Math.Abs(rightOverload); 28 | 29 | return leftOverload + rightOverload + root.val - 1; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/EncodeAndDecodeTinyUrl.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class EncodeAndDecodeTinyUrl 6 | { 7 | 8 | private Dictionary dict = new Dictionary(); 9 | 10 | // Encodes a URL to a shortened URL 11 | public string encode(string longUrl) 12 | { 13 | var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(longUrl); 14 | var encode = System.Convert.ToBase64String(plainTextBytes); 15 | var tinyUrl = $"http://tinyurl.com/{encode}"; 16 | 17 | dict.Add(tinyUrl, longUrl); 18 | 19 | return tinyUrl; 20 | } 21 | 22 | // Decodes a shortened URL to its original URL. 23 | public string decode(string shortUrl) 24 | { 25 | return dict[shortUrl]; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/FindAllDuplicates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class FindAllDuplicates 7 | { 8 | public IList FindDuplicates(int[] nums) 9 | { 10 | var duplicates = new List(); 11 | 12 | for (int i = 0; i < nums.Length; i++) 13 | { 14 | var nextIndex = Math.Abs(nums[i]) - 1; 15 | 16 | if (nums[nextIndex] < 0) 17 | { 18 | duplicates.Add(nextIndex + 1); 19 | } 20 | else 21 | { 22 | nums[nextIndex] = nums[nextIndex] * -1; 23 | } 24 | } 25 | 26 | return duplicates; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/FindClosestElements.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace InterviewPreparation.MicrosoftExcercises.Medium 6 | { 7 | class FindClosestElements 8 | { 9 | public IList Solve(int[] arr, int k, int x) 10 | { 11 | // Initialize binary search bounds 12 | int low = 0; 13 | int high = arr.Length - k; 14 | 15 | while (low < high) 16 | { 17 | var mid = low + (high - low) / 2; 18 | 19 | if (x - arr[mid] > arr[mid + k] - x) 20 | { 21 | low = mid + 1; 22 | } 23 | else 24 | { 25 | high = mid; 26 | } 27 | } 28 | 29 | var result = new List(); 30 | 31 | for (int i = low; i < low + k; i++) 32 | { 33 | result.Add(arr[i]); 34 | } 35 | 36 | return result; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/FindPeakElement.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class FindPeakElement 4 | { 5 | public int Solve(int[] nums) 6 | { 7 | int left = 0; 8 | int right = nums.Length - 1; 9 | 10 | while (left < right) 11 | { 12 | var mid = left + (right - left) / 2; 13 | 14 | if (nums[mid] > nums[mid + 1]) 15 | { 16 | right = mid; 17 | } 18 | else 19 | { 20 | left = mid + 1; 21 | } 22 | } 23 | 24 | return left; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/FindTheDuplicateNumber.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class FindTheDuplicateNumber 4 | { 5 | public int FindDuplicate(int[] nums) 6 | { 7 | int fast = nums[0]; 8 | int slow = nums[0]; 9 | 10 | do 11 | { 12 | fast = nums[nums[fast]]; 13 | slow = nums[slow]; 14 | } while (slow != fast); 15 | 16 | slow = nums[0]; 17 | 18 | while (slow != fast) 19 | { 20 | slow = nums[slow]; 21 | fast = nums[fast]; 22 | } 23 | 24 | return slow; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/Flatten.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class Flatten 6 | { 7 | public void Solve(TreeNode root) 8 | { 9 | if (root == null) 10 | { 11 | return; 12 | } 13 | 14 | Preorder(root, null); 15 | } 16 | 17 | public TreeNode Preorder(TreeNode root, TreeNode prev) 18 | { 19 | if (root == null) 20 | { 21 | return prev; 22 | } 23 | 24 | var rightMost = Preorder(root.right, prev); 25 | var leftMost = Preorder(root.left, rightMost); 26 | 27 | root.left = null; 28 | root.right = leftMost; 29 | 30 | return root; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/GasStation.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class GasStation 4 | { 5 | public int CanCompleteCircuit(int[] gas, int[] cost) 6 | { 7 | var diff = 0; 8 | 9 | for (int i = 0; i < gas.Length; i++) 10 | { 11 | diff += gas[i] - cost[i]; 12 | } 13 | 14 | if (diff < 0) 15 | { 16 | return -1; 17 | } 18 | 19 | var index = 0; 20 | var tank = 0; 21 | 22 | for (int i = 0; i < gas.Length; i++) 23 | { 24 | tank += gas[i] - cost[i]; 25 | 26 | if (tank < 0) 27 | { 28 | tank = 0; 29 | index = i + 1; 30 | } 31 | } 32 | 33 | return index; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/GenerateBalancedParenthesis.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class GenerateBalancedParenthesis 7 | { 8 | public IList result; 9 | public IList GenerateParenthesis(int n) 10 | { 11 | result = new List(); 12 | Backtrack(n, 0, new StringBuilder(), 0, 0); 13 | return result; 14 | } 15 | public void Backtrack(int n, int total, StringBuilder actual, int open, int closed) 16 | { 17 | if (total == n * 2) 18 | { 19 | result.Add(actual.ToString()); 20 | return; 21 | } 22 | if (open < n) 23 | { 24 | actual.Append('('); 25 | Backtrack(n, total + 1, actual, open + 1, closed); 26 | actual.Remove(actual.Length - 1, 1); 27 | } 28 | if (closed < open) 29 | { 30 | actual.Append(')'); 31 | Backtrack(n, total + 1, actual, open, closed + 1); 32 | actual.Remove(actual.Length - 1, 1); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/HouseRobber.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class HouseRobber 6 | { 7 | public int Rob(int[] nums) 8 | { 9 | if (nums.Length == 1) 10 | { 11 | return nums[0]; 12 | } 13 | 14 | if (nums.Length == 2) 15 | { 16 | return Math.Max(nums[0], nums[1]); 17 | } 18 | 19 | var dp = new int[nums.Length]; 20 | 21 | dp[0] = nums[0]; 22 | dp[1] = Math.Max(dp[0], nums[1]); 23 | for (int i = 2; i < nums.Length; i++) 24 | { 25 | dp[i] = Math.Max(nums[i] + dp[i - 2], dp[i - 1]); 26 | } 27 | 28 | return dp[nums.Length - 1]; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/HtmlParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | public class HtmlParser 7 | { 8 | internal IEnumerable GetUrls(string actual) 9 | { 10 | throw new NotImplementedException(); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/InorderTraversal.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class InorderTraversal 7 | { 8 | public IList Solve(TreeNode root) 9 | { 10 | var stack = new Stack(); 11 | var list = new List(); 12 | var current = root; 13 | 14 | while (current != null || stack.Count > 0) 15 | { 16 | while (current != null) 17 | { 18 | stack.Push(current); 19 | current = current.left; 20 | } 21 | 22 | current = stack.Pop(); 23 | 24 | list.Add(current.val); 25 | 26 | current = current.right; 27 | } 28 | 29 | return list; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/InsertIntoBST.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class InsertIntoBST 6 | { 7 | public TreeNode Solve(TreeNode root, int val) 8 | { 9 | if (root == null) 10 | { 11 | return new TreeNode(val); 12 | } 13 | 14 | TreeNode prev = null; 15 | var current = root; 16 | bool isLeft = false; 17 | 18 | while (current != null) 19 | { 20 | prev = current; 21 | 22 | if (current.val > val) 23 | { 24 | current = current.left; 25 | isLeft = true; 26 | } 27 | else 28 | { 29 | current = current.right; 30 | isLeft = false; 31 | } 32 | } 33 | 34 | if (isLeft) 35 | { 36 | prev.left = new TreeNode(val); 37 | } 38 | else 39 | { 40 | prev.right = new TreeNode(val); 41 | } 42 | 43 | return root; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/KSmallestInMatrix.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class KSmallestInMatrix 6 | { 7 | // Kinda shitty sol 8 | public int KthSmallest(int[][] matrix, int k) 9 | { 10 | var sorted = new int[matrix.Length * matrix[0].Length]; 11 | var sortedIndex = 0; 12 | 13 | for (int i = 0; i < matrix.Length; i++) 14 | { 15 | for (int j = 0; j < matrix[i].Length; j++) 16 | { 17 | sorted[sortedIndex++] = matrix[i][j]; 18 | } 19 | } 20 | 21 | Array.Sort(sorted); 22 | 23 | return sorted[k - 1]; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/KthElementInBST.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class KthElementInBST 6 | { 7 | private int currentNumber; 8 | private int finalNumber; 9 | 10 | public int KthSmallest(TreeNode root, int k) 11 | { 12 | currentNumber = k; 13 | 14 | CreateInorderBSTList(root); 15 | 16 | return finalNumber; 17 | } 18 | 19 | public void CreateInorderBSTList(TreeNode root) 20 | { 21 | if (root != null && currentNumber != 0) 22 | { 23 | CreateInorderBSTList(root.left); 24 | currentNumber--; 25 | 26 | if (currentNumber == 0) 27 | { 28 | finalNumber = root.val; 29 | } 30 | 31 | CreateInorderBSTList(root.right); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/LCA3.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class LCA3 6 | { 7 | public Node LowestCommonAncestor(Node p, Node q) 8 | { 9 | var visited = new HashSet(); 10 | 11 | var current = p; 12 | 13 | while (current != null) 14 | { 15 | visited.Add(current); 16 | 17 | current = current.parent; 18 | } 19 | 20 | current = q; 21 | 22 | while (current != null) 23 | { 24 | if (visited.Contains(current)) 25 | { 26 | return current; 27 | } 28 | 29 | current = current.parent; 30 | } 31 | 32 | return null; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/LIS.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class LIS 6 | { 7 | public int LengthOfLIS(int[] nums) 8 | { 9 | var max = 1; 10 | var LIS = new int[nums.Length]; 11 | 12 | Array.Fill(LIS, 1); 13 | 14 | for (int i = 1; i < nums.Length; i++) 15 | { 16 | for (int j = 0; j < i; j++) 17 | { 18 | if (nums[j] < nums[i]) 19 | { 20 | LIS[i] = Math.Max(LIS[i], LIS[j] + 1); 21 | max = Math.Max(LIS[i], max); 22 | } 23 | } 24 | } 25 | 26 | return max; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/LongestIncreasingConsecutive.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class LongestIncreasingConsecutive 7 | { 8 | public int LongestConsecutive(int[] nums) 9 | { 10 | var hs = new HashSet(nums); 11 | var max = 0; 12 | foreach (var num in nums) 13 | { 14 | if (hs.Contains(num - 1)) 15 | { 16 | continue; 17 | } 18 | var currentStreak = 1; 19 | var current = num + 1; 20 | while (hs.Contains(current)) 21 | { 22 | current = current + 1; 23 | currentStreak++; 24 | } 25 | max = Math.Max(max, currentStreak); 26 | } 27 | return max; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/LongestPalindrome.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class LongestPalindrome 4 | { 5 | public string Solve(string s) 6 | { 7 | var maxPalindrome = ""; 8 | for (int i = 0; i < s.Length; i++) 9 | { 10 | var p1 = CalculatePalindrome(s, i, i); 11 | var p2 = CalculatePalindrome(s, i, i + 1); 12 | 13 | maxPalindrome = maxPalindrome.Length > p1.Length ? maxPalindrome : p1; 14 | maxPalindrome = maxPalindrome.Length > p2.Length ? maxPalindrome : p2; 15 | } 16 | return maxPalindrome; 17 | } 18 | public string CalculatePalindrome(string s, int i, int j) 19 | { 20 | while (i >= 0 && j < s.Length && s[i] == s[j]) 21 | { 22 | i--; 23 | j++; 24 | } 25 | return s.Substring(i + 1, j - i - 1); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/LowestCommonAncestorBinaryTree.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class LowestCommonAncestorBinaryTree 6 | { 7 | public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) 8 | { 9 | if (root == null) 10 | { 11 | return null; 12 | } 13 | 14 | if (p == root || q == root) 15 | { 16 | return root; 17 | } 18 | 19 | var left = LowestCommonAncestor(root.left, p, q); 20 | var right = LowestCommonAncestor(root.right, p, q); 21 | 22 | if (left != null && right != null) 23 | { 24 | return root; 25 | } 26 | 27 | return left ?? right; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/MaxProduct.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class MaxProduct 6 | { 7 | public int Solve(int[] nums) 8 | { 9 | var max = nums[0]; 10 | var min = nums[0]; 11 | int result = nums[0]; 12 | 13 | for (int i = 1; i < nums.Length; i++) 14 | { 15 | var actual1 = nums[i]; 16 | var actual2 = nums[i] * max; 17 | var actual3 = nums[i] * min; 18 | 19 | min = Math.Min(actual1, Math.Min(actual2, actual3)); 20 | max = Math.Max(actual1, Math.Max(actual2, actual3)); 21 | result = Math.Max(result, max); 22 | } 23 | 24 | return result; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/MaximalNetworkRank.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class MaximalNetworkRank 6 | { 7 | public int Solve(int n, int[][] roads) 8 | { 9 | var connected = new bool[n, n]; 10 | var outdegrees = new int[n]; 11 | var max = 0; 12 | 13 | foreach (var road in roads) 14 | { 15 | var u = road[0]; 16 | var v = road[1]; 17 | 18 | connected[u, v] = true; 19 | connected[v, u] = true; 20 | 21 | outdegrees[u]++; 22 | outdegrees[v]++; 23 | } 24 | 25 | for (int i = 0; i < n; i++) 26 | { 27 | for (int j = i + 1; j < n; j++) 28 | { 29 | if (connected[i, j]) 30 | { 31 | max = Math.Max(max, outdegrees[i] + outdegrees[j] - 1); 32 | } 33 | else 34 | { 35 | max = Math.Max(max, outdegrees[i] + outdegrees[j]); 36 | } 37 | } 38 | } 39 | 40 | return max; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/MergeIntervals.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class MergeIntervals 7 | { 8 | public int[][] Merge(int[][] intervals) 9 | { 10 | var result = new List(); 11 | 12 | Array.Sort(intervals, (x, y) => x[0].CompareTo(y[0])); 13 | 14 | int i = 0; 15 | 16 | while (i < intervals.Length) 17 | { 18 | var start = intervals[i][0]; 19 | var end = intervals[i][1]; 20 | var changed = false; 21 | 22 | while (i < intervals.Length && intervals[i][0] <= end) 23 | { 24 | end = Math.Max(end, intervals[i][1]); 25 | i++; 26 | changed = true; 27 | } 28 | 29 | result.Add(new int[] { start, end }); 30 | 31 | if (!changed) 32 | { 33 | i++; 34 | } 35 | } 36 | 37 | 38 | return result.ToArray(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/Min2Anagram.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class Min2Anagram 6 | { 7 | public class Solution 8 | { 9 | public int MinSteps(string s, string t) 10 | { 11 | var steps = 0; 12 | 13 | var freq1 = new int[26]; 14 | var freq2 = new int[26]; 15 | 16 | for (int i = 0; i < s.Length; i++) 17 | { 18 | freq1[t[i] - 'a']++; 19 | freq2[s[i] - 'a']++; 20 | } 21 | 22 | for (int i = 0; i < 26; i++) 23 | { 24 | steps += Math.Abs(freq1[i] - freq2[i]); 25 | } 26 | 27 | return steps / 2; 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/MinPathSum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class MinPathSum 6 | { 7 | public int Solve(int[][] grid) 8 | { 9 | var dp = new int[grid[0].Length]; 10 | 11 | Array.Fill(dp, int.MaxValue); 12 | 13 | dp[0] = 0; 14 | 15 | for (int i = 0; i < grid.Length; i++) 16 | { 17 | for (int j = 0; j < grid[i].Length; j++) 18 | { 19 | if (j > 0) 20 | { 21 | dp[j] = Math.Min(dp[j], dp[j - 1]) + grid[i][j]; 22 | } 23 | else 24 | { 25 | dp[j] = dp[j] + grid[i][j]; 26 | } 27 | } 28 | } 29 | 30 | return dp[dp.Length - 1]; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/MinimumInSorted.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class MinimumInSorted 4 | { 5 | public int FindMin(int[] nums) 6 | { 7 | var first = nums[0]; 8 | var last = nums[nums.Length - 1]; 9 | 10 | if (first <= last) 11 | { 12 | return first; 13 | } 14 | 15 | var low = 0; 16 | var high = nums.Length - 1; 17 | 18 | while (low < high) 19 | { 20 | var mid = low + (high - low) / 2; 21 | var actual = nums[mid]; 22 | 23 | if (actual < nums[high]) 24 | { 25 | high = mid; 26 | } 27 | else 28 | { 29 | low = mid + 1; 30 | } 31 | } 32 | 33 | return nums[low]; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/MinimumSubarraySumK.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class MinimumSubarraySumK 6 | { 7 | public int MinSubArrayLen(int target, int[] nums) 8 | { 9 | var min = int.MaxValue; 10 | var sum = 0; 11 | var left = 0; 12 | var right = 0; 13 | 14 | while (left < nums.Length && right < nums.Length) 15 | { 16 | while (right < nums.Length && sum < target) 17 | { 18 | sum += nums[right]; 19 | right++; 20 | } 21 | 22 | while (sum >= target) 23 | { 24 | min = Math.Min(min, right - left); 25 | sum -= nums[left]; 26 | left++; 27 | } 28 | } 29 | 30 | return min == int.MaxValue ? 0 : min; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/MyPow.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class SolveMyPow 4 | { 5 | public double Solve(double x, int n) 6 | { 7 | if (n < 0) return 1 / x * Solve(1 / x, -(n + 1)); 8 | if (n == 0) return 1; 9 | if (n == 1) return x; 10 | if (n % 2 == 0) return Solve(x * x, n / 2); 11 | return x * Solve(x * x, n / 2); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/OddEvenList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class OddEvenList 6 | { 7 | public ListNode Solve(ListNode head) 8 | { 9 | var odds = new ListNode(); 10 | var evens = new ListNode(); 11 | 12 | var actualOdd = odds; 13 | var actualEven = evens; 14 | 15 | var current = head; 16 | var isOdd = true; 17 | 18 | while (current != null) 19 | { 20 | if (isOdd) 21 | { 22 | actualOdd.next = current; 23 | actualOdd = actualOdd.next; 24 | } 25 | else 26 | { 27 | actualEven.next = current; 28 | actualEven = actualEven.next; 29 | } 30 | 31 | isOdd = !isOdd; 32 | current = current.next; 33 | } 34 | 35 | actualOdd.next = evens.next; 36 | actualEven.next = null; 37 | 38 | return odds.next; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/PartitionList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class PartitionList 6 | { 7 | public ListNode Partition(ListNode head, int x) 8 | { 9 | var dummyLesser = new ListNode(); 10 | var dummyGreaterOrEqual = new ListNode(); 11 | 12 | var currentLesser = dummyLesser; 13 | var currentGreaterOrEqual = dummyGreaterOrEqual; 14 | 15 | var current = head; 16 | 17 | while (current != null) 18 | { 19 | if (current.val < x) 20 | { 21 | currentLesser.next = current; 22 | currentLesser = currentLesser.next; 23 | } 24 | else 25 | { 26 | currentGreaterOrEqual.next = current; 27 | currentGreaterOrEqual = currentGreaterOrEqual.next; 28 | } 29 | 30 | current = current.next; 31 | } 32 | currentGreaterOrEqual.next = null; 33 | 34 | currentLesser.next = dummyGreaterOrEqual.next; 35 | 36 | return dummyLesser.next; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/PeekingIterator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class PeekingIterator 6 | { 7 | private IEnumerator _iterator; 8 | private bool hasNext; 9 | 10 | // iterators refers to the first element of the array. 11 | public PeekingIterator(IEnumerator iterator) 12 | { 13 | // initialize any member here. 14 | _iterator = iterator; 15 | hasNext = iterator.Current != null; 16 | } 17 | 18 | // Returns the next element in the iteration without advancing the iterator. 19 | public int Peek() 20 | { 21 | return _iterator.Current; 22 | } 23 | 24 | // Returns the next element in the iteration and advances the iterator. 25 | public int Next() 26 | { 27 | var current = _iterator.Current; 28 | 29 | hasNext = _iterator.MoveNext(); 30 | 31 | return current; 32 | } 33 | 34 | // Returns false if the iterator is refering to the end of the array of true otherwise. 35 | public bool HasNext() 36 | { 37 | return hasNext; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/Permutations.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class Permutations 7 | { 8 | public HashSet visited { get; set; } = new HashSet(); 9 | 10 | public IList> Permute(int[] nums) 11 | { 12 | var result = new List>(); 13 | 14 | foreach (var num in nums) 15 | { 16 | visited.Add(num); 17 | 18 | Backtrack(nums, result); 19 | 20 | visited.Remove(num); 21 | } 22 | 23 | return result; 24 | } 25 | 26 | public void Backtrack(int[] nums, IList> result) 27 | { 28 | if (visited.Count == nums.Length) 29 | { 30 | result.Add(visited.ToList()); 31 | return; 32 | } 33 | 34 | foreach (var num in nums) 35 | { 36 | if (!visited.Contains(num)) 37 | { 38 | visited.Add(num); 39 | 40 | Backtrack(nums, result); 41 | 42 | visited.Remove(num); 43 | } 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/ProductExcept.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | // Product of Array Except Self 4 | class ProductExcept 5 | { 6 | public int[] ProductExceptSelf(int[] nums) 7 | { 8 | var left = new int[nums.Length]; 9 | var right = new int[nums.Length]; 10 | 11 | left[0] = 1; 12 | 13 | for (int i = 1; i < left.Length; i++) 14 | { 15 | left[i] = left[i - 1] * nums[i - 1]; 16 | } 17 | 18 | right[right.Length - 1] = 1; 19 | 20 | for (int i = right.Length - 2; i >= 0; i--) 21 | { 22 | right[i] = right[i + 1] * nums[i + 1]; 23 | } 24 | 25 | var result = new int[nums.Length]; 26 | 27 | for (int i = 0; i < result.Length; i++) 28 | { 29 | result[i] = left[i] * right[i]; 30 | } 31 | 32 | return result; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/RangeSumQuery2D.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class RangeSumQuery2D 4 | { 5 | private int[,] prefixes; 6 | 7 | public void NumMatrix(int[][] matrix) 8 | { 9 | if (matrix.Length == 0 || matrix[0].Length == 0) return; 10 | 11 | prefixes = new int[matrix.Length + 1, matrix[0].Length + 1]; 12 | 13 | for (int i = 0; i < matrix.Length; i++) 14 | { 15 | for (int j = 0; j < matrix[i].Length; j++) 16 | { 17 | prefixes[i + 1, j + 1] = prefixes[i + 1, j] + prefixes[i, j + 1] 18 | + matrix[i][j] - prefixes[i, j]; 19 | } 20 | } 21 | } 22 | 23 | public int SumRegion(int row1, int col1, int row2, int col2) 24 | { 25 | return 26 | prefixes[row2 + 1, col2 + 1] 27 | - prefixes[row1, col2 + 1] 28 | + prefixes[row1, col1] 29 | - prefixes[row2 + 1, col1]; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/RemoveAllOcurrences.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class RemoveAllOcurrences 6 | { 7 | public string RemoveOccurrences(string s, string part) 8 | { 9 | var result = new StringBuilder(); 10 | 11 | for (int i = 0; i < s.Length; i++) 12 | { 13 | result.Append(s[i]); 14 | 15 | if (result.Length >= part.Length && 16 | result.ToString().Substring(result.Length - part.Length) == part) 17 | { 18 | result.Length -= part.Length; 19 | } 20 | } 21 | 22 | return result.ToString(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/RemoveNthElementList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class RemoveNthElementList 6 | { 7 | public ListNode RemoveNthFromEnd(ListNode head, int n) 8 | { 9 | var slow = head; 10 | var fast = head; 11 | ListNode prev = null; 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | fast = fast?.next; 16 | } 17 | 18 | while (fast != null) 19 | { 20 | prev = slow; 21 | slow = slow?.next; 22 | fast = fast?.next; 23 | } 24 | 25 | if (prev != null) 26 | { 27 | prev.next = slow.next; 28 | return head; 29 | } 30 | else 31 | { 32 | return slow?.next; 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/ReverseWords.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class ReverseWords 6 | { 7 | public string Solve(string s) 8 | { 9 | var sb = new StringBuilder(); 10 | int i = 0; 11 | 12 | while (i < s.Length) 13 | { 14 | while (i < s.Length && s[i] == ' ') 15 | { 16 | i++; 17 | } 18 | 19 | var actual = new StringBuilder(); 20 | 21 | while (i < s.Length && char.IsLetterOrDigit(s[i])) 22 | { 23 | actual.Append(s[i]); 24 | i++; 25 | } 26 | actual.Append(' '); 27 | 28 | sb.Insert(0, actual.ToString()); 29 | } 30 | 31 | return sb.ToString().Trim(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/RotateArray.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class RotateArray 4 | { 5 | public void Rotate(int[] nums, int k) 6 | { 7 | if (k % nums.Length != 0) 8 | { 9 | var shifted = 0; 10 | var i = 0; 11 | while (shifted < nums.Length) 12 | { 13 | var actualIndex = i; 14 | int? prev = null; 15 | 16 | do 17 | { 18 | var next = (i + k) % nums.Length; 19 | var tmp = nums[next]; 20 | 21 | nums[next] = prev ?? nums[i]; 22 | 23 | prev = tmp; 24 | i = next; 25 | shifted++; 26 | 27 | } while (i != actualIndex); 28 | 29 | i++; 30 | } 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/RotateList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class RotateList 6 | { 7 | public ListNode RotateRight(ListNode head, int k) 8 | { 9 | if (head == null) 10 | { 11 | return null; 12 | } 13 | 14 | var fast = head; 15 | var length = 1; 16 | 17 | while (fast.next != null) 18 | { 19 | fast = fast.next; 20 | length++; 21 | } 22 | 23 | k = k % length; 24 | 25 | if (k == 0) 26 | { 27 | return head; 28 | } 29 | 30 | fast.next = head; 31 | ListNode prev = null; 32 | var newHead = head; 33 | var i = 1; 34 | var lastIndex = length - k + 1; 35 | 36 | while (i < lastIndex) 37 | { 38 | prev = newHead; 39 | newHead = newHead.next; 40 | i++; 41 | } 42 | 43 | prev.next = null; 44 | 45 | return newHead; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/Search2DArray.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class Search2DArray 4 | { 5 | public bool SearchMatrix(int[][] matrix, int target) 6 | { 7 | var i = matrix.Length - 1; 8 | var j = 0; 9 | 10 | while (i >= 0 && j < matrix[i].Length) 11 | { 12 | var actual = matrix[i][j]; 13 | 14 | if (actual == target) 15 | { 16 | return true; 17 | } 18 | else if (actual > target) 19 | { 20 | i--; 21 | } 22 | else 23 | { 24 | j++; 25 | } 26 | } 27 | 28 | return false; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/SearchIn2dMatrix.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | public class SearchIn2dMatrix 4 | { 5 | public bool SearchMatrix(int[][] matrix, int target) 6 | { 7 | var m = matrix.Length; 8 | var n = matrix[0].Length; 9 | var low = 0; 10 | var high = (matrix.Length * matrix[0].Length) - 1; 11 | 12 | while (low <= high) 13 | { 14 | var mid = low + (high - low) / 2; 15 | var row = mid / n; 16 | var col = mid % n; 17 | 18 | if (matrix[row][col] == target) 19 | { 20 | return true; 21 | } 22 | 23 | if (matrix[row][col] > target) 24 | { 25 | high = mid - 1; 26 | } 27 | else 28 | { 29 | low = mid + 1; 30 | } 31 | } 32 | 33 | return false; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/SearchSuggestionsSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace InterviewPreparation.MicrosoftExcercises.Medium 6 | { 7 | class SearchSuggestionsSystem 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/SortCharactersByFrequency.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Text; 4 | 5 | namespace InterviewPreparation.MicrosoftExcercises.Medium 6 | { 7 | class SortCharactersByFrequency 8 | { 9 | public string FrequencySort(string str) 10 | { 11 | var frequencies = new Dictionary(); 12 | 13 | foreach (var character in str) 14 | { 15 | if (!frequencies.ContainsKey(character)) 16 | { 17 | frequencies.Add(character, 0); 18 | } 19 | 20 | frequencies[character]++; 21 | } 22 | 23 | var ordered = frequencies.OrderByDescending(tuple => tuple.Value); 24 | 25 | var sb = new StringBuilder(); 26 | 27 | foreach (var tuple in ordered) 28 | { 29 | for (int i = 0; i < tuple.Value; i++) 30 | { 31 | sb.Append(tuple.Key); 32 | } 33 | } 34 | 35 | return sb.ToString(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/SortedListToBST.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class SolveSortedListToBST 7 | { 8 | public TreeNode Solve(ListNode head) 9 | { 10 | var list = new List(); 11 | 12 | var current = head; 13 | 14 | while (current != null) 15 | { 16 | list.Add(current); 17 | 18 | current = current.next; 19 | } 20 | 21 | var array = list.ToArray(); 22 | 23 | var BST = CreateBST(array, 0, array.Length - 1); 24 | 25 | return BST; 26 | } 27 | 28 | public TreeNode CreateBST(ListNode[] array, int left, int right) 29 | { 30 | if (left > right) 31 | { 32 | return null; 33 | } 34 | 35 | var currentElement = left + (right - left) / 2; 36 | 37 | var newNode = new TreeNode(array[currentElement].val); 38 | 39 | newNode.left = CreateBST(array, left, currentElement - 1); 40 | newNode.right = CreateBST(array, currentElement + 1, right); 41 | 42 | return newNode; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/SubArraySum.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class SubArraySum 6 | { 7 | public int SubarraySum(int[] nums, int k) 8 | { 9 | var total = 0; 10 | var sum = 0; 11 | var accumulatorMap = new Dictionary(); 12 | accumulatorMap.Add(0, 1); 13 | for (int i = 0; i < nums.Length; i++) 14 | { 15 | sum += nums[i]; 16 | if (accumulatorMap.ContainsKey(sum - k)) 17 | { 18 | total += accumulatorMap[sum - k]; 19 | } 20 | if (!accumulatorMap.ContainsKey(sum)) 21 | { 22 | accumulatorMap.Add(sum, 0); 23 | } 24 | accumulatorMap[sum]++; 25 | } 26 | return total; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/Subsets.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class Subsets 7 | { 8 | public IList> Solve(int[] nums) 9 | { 10 | var subsets = new List>(); 11 | 12 | Backtrack(nums, subsets, 0, new HashSet()); 13 | 14 | return subsets; 15 | } 16 | 17 | public void Backtrack(int[] nums, IList> subsets, int start, HashSet subset) 18 | { 19 | subsets.Add(subset.ToList()); 20 | 21 | for (int i = start; i < nums.Length; i++) 22 | { 23 | var candidate = nums[i]; 24 | subset.Add(candidate); 25 | 26 | Backtrack(nums, subsets, i + 1, subset); 27 | 28 | subset.Remove(candidate); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/SwappingNodesInALinkedList.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class SwappingNodesInALinkedList 6 | { 7 | public ListNode SwapNodes(ListNode head, int k) 8 | { 9 | if (head == null) 10 | { 11 | return head; 12 | } 13 | 14 | var index = 1; 15 | var fast = head; 16 | 17 | while (index < k) 18 | { 19 | fast = fast.next; 20 | index++; 21 | } 22 | 23 | var firstSwap = fast; 24 | fast = fast.next; 25 | var slow = head; 26 | 27 | while (fast != null) 28 | { 29 | fast = fast.next; 30 | slow = slow.next; 31 | } 32 | 33 | var aux = slow.val; 34 | slow.val = firstSwap.val; 35 | firstSwap.val = aux; 36 | 37 | return head; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/TaskScheduler.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class TaskScheduler 6 | { 7 | public int LeastInterval(char[] tasks, int n) 8 | { 9 | var freq = new Dictionary(); 10 | int maxFreq = 0; 11 | int countMax = 0; 12 | 13 | foreach (var task in tasks) 14 | { 15 | if (!freq.ContainsKey(task)) 16 | { 17 | freq.Add(task, 0); 18 | } 19 | 20 | freq[task]++; 21 | 22 | if (freq[task] > maxFreq) 23 | { 24 | maxFreq = freq[task]; 25 | countMax = 0; 26 | } 27 | else if (freq[task] == maxFreq) 28 | { 29 | countMax++; 30 | } 31 | } 32 | 33 | var result = (maxFreq - 1) * n + maxFreq + countMax; 34 | 35 | if (result < tasks.Length) 36 | { 37 | result += tasks.Length - result; 38 | } 39 | 40 | return result; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/TopKFrequentWords.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class TopKFrequentWords 7 | { 8 | public IList TopKFrequent(string[] words, int k) 9 | { 10 | var frequencies = new Dictionary(); 11 | 12 | foreach (var word in words) 13 | { 14 | if (frequencies.ContainsKey(word)) 15 | { 16 | frequencies[word] += 1; 17 | } 18 | else 19 | { 20 | frequencies.Add(word, 1); 21 | } 22 | } 23 | 24 | return frequencies.OrderByDescending(key => key.Value) 25 | .ThenBy(key => key.Key) 26 | .Take(k) 27 | .Select(key => key.Key) 28 | .ToList(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/Triangle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Medium 5 | { 6 | class Triangle 7 | { 8 | public int MinimumTotal(IList> triangle) 9 | { 10 | var cache = new Dictionary(); 11 | return MinimumTotal(triangle, 0, 0, cache); 12 | } 13 | 14 | private int MinimumTotal(IList> triangle, int row, int col, Dictionary cache) 15 | { 16 | var key = $"{row}#{col}"; 17 | 18 | if (cache.ContainsKey(key)) 19 | { 20 | return cache[key]; 21 | } 22 | 23 | if (row == triangle.Count - 1) 24 | { 25 | return triangle[row][col]; 26 | } 27 | 28 | cache.Add(key, triangle[row][col] + Math.Min(MinimumTotal(triangle, row + 1, col, cache), 29 | MinimumTotal(triangle, row + 1, col + 1, cache))); 30 | 31 | return cache[key]; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/TriangleNumber.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class TriangleNumber 6 | { 7 | public int Solve(int[] nums) 8 | { 9 | var count = 0; 10 | Array.Sort(nums); 11 | 12 | for (int i = 0; i < nums.Length - 2; i++) 13 | { 14 | for (int j = i + 1; j < nums.Length - 1; j++) 15 | { 16 | var low = j + 1; 17 | var high = nums.Length - 1; 18 | var a = nums[i]; 19 | var b = nums[j]; 20 | 21 | while (low <= high) 22 | { 23 | var mid = low + (high - low) / 2; 24 | var c = nums[mid]; 25 | 26 | if (a + b <= c) 27 | { 28 | high = mid - 1; 29 | } 30 | else 31 | { 32 | low = mid + 1; 33 | } 34 | } 35 | 36 | count += low - j - 1; 37 | } 38 | } 39 | 40 | return count; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/TrimBST.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class TrimBST 6 | { 7 | public TreeNode Solve(TreeNode root, int low, int high) 8 | { 9 | return PostOrder(root, low, high); 10 | } 11 | 12 | public TreeNode PostOrder(TreeNode root, int low, int high) 13 | { 14 | if (root == null) 15 | { 16 | return root; 17 | } 18 | else 19 | { 20 | root.left = PostOrder(root.left, low, high); 21 | root.right = PostOrder(root.right, low, high); 22 | 23 | if (root.val >= low && root.val <= high) 24 | { 25 | return root; 26 | } 27 | else if (root.val < low) 28 | { 29 | return root.right; 30 | } 31 | else 32 | { 33 | return root.left; 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/UniquePaths.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class UniquePaths 4 | { 5 | public int Solve(int m, int n) 6 | { 7 | var dp = new int[m, n]; 8 | 9 | for (int j = 0; j < n; j++) 10 | { 11 | dp[0, j] = 1; 12 | } 13 | 14 | for (int i = 1; i < m; i++) 15 | { 16 | for (int j = 0; j < n; j++) 17 | { 18 | if (j == 0) 19 | { 20 | dp[i, j] = dp[i - 1, j]; 21 | } 22 | else 23 | { 24 | dp[i, j] = dp[i - 1, j] + dp[i, j - 1]; 25 | } 26 | } 27 | } 28 | 29 | return dp[m - 1, n - 1]; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/UniquePathsII.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Medium 2 | { 3 | class UniquePathsII 4 | { 5 | public int UniquePathsWithObstacles(int[][] grid) 6 | { 7 | var n = grid.Length; 8 | var m = grid[0].Length; 9 | 10 | if (n < 1 || m < 1 || grid[0][0] == 1) 11 | { 12 | return 0; 13 | } 14 | 15 | var dp = new int[n, m]; 16 | 17 | dp[0, 0] = 1; 18 | 19 | for (int j = 1; j < m; j++) 20 | { 21 | dp[0, j] = grid[0][j] == 1 ? 0 : dp[0, j - 1]; 22 | } 23 | 24 | for (int i = 1; i < n; i++) 25 | { 26 | for (int j = 0; j < m; j++) 27 | { 28 | if (j == 0) 29 | { 30 | dp[i, j] = grid[i][j] == 1 ? 0 : dp[i - 1, j]; 31 | } 32 | else 33 | { 34 | dp[i, j] = grid[i][j] == 1 ? 0 : dp[i - 1, j] + dp[i, j - 1]; 35 | } 36 | } 37 | } 38 | 39 | return dp[n - 1, m - 1]; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Medium/ValidateBST.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Medium 4 | { 5 | class ValidateBST 6 | { 7 | public bool IsValidBST(TreeNode root) 8 | { 9 | return IsValidBST(root, null, null); 10 | } 11 | 12 | private bool IsValidBST(TreeNode root, int? min, int? max) 13 | { 14 | if (root == null) 15 | { 16 | return true; 17 | } 18 | 19 | if ((min != null && root.val <= min) || 20 | (max != null && root.val >= max)) 21 | { 22 | return false; 23 | } 24 | 25 | return IsValidBST(root.left, min, root.val) && IsValidBST(root.right, root.val, max); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Node.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises 4 | { 5 | public class Node 6 | { 7 | public int val; 8 | public Node next; 9 | public Node left; 10 | public Node right; 11 | public Node random; 12 | public IList children = new List(); 13 | internal Node parent; 14 | 15 | public Node() 16 | { 17 | } 18 | 19 | public Node(int _val, Node _left) 20 | { 21 | val = _val; 22 | left = _left; 23 | } 24 | 25 | public Node(int _val) 26 | { 27 | val = _val; 28 | next = null; 29 | random = null; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/BSTToList.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Premium 2 | { 3 | class BSTToList 4 | { 5 | private Node last; 6 | private Node first; 7 | 8 | public Node TreeToDoublyList(Node root) 9 | { 10 | if (root == null) 11 | { 12 | return null; 13 | } 14 | 15 | buildInorder(root); 16 | 17 | first.left = last; 18 | last.right = first; 19 | 20 | return first; 21 | } 22 | 23 | private void buildInorder(Node root) 24 | { 25 | if (root == null) 26 | { 27 | return; 28 | } 29 | 30 | buildInorder(root.left); 31 | 32 | if (last == null) 33 | { 34 | first = root; 35 | } 36 | else 37 | { 38 | last.right = root; 39 | root.left = last; 40 | } 41 | 42 | last = root; 43 | 44 | buildInorder(root.right); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/ClosestBinaryTreeValue.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Premium 5 | { 6 | class ClosestBinaryTreeValue 7 | { 8 | public int ClosestValue(TreeNode root, double target) 9 | { 10 | int val, closest = root.val; 11 | while (root != null) 12 | { 13 | val = root.val; 14 | closest = Math.Abs(val - target) < Math.Abs(closest - target) ? val : closest; 15 | root = target < root.val ? root.left : root.right; 16 | } 17 | return closest; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/FindCelebrity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Premium 4 | { 5 | class FindCelebrity 6 | { 7 | public int Solve(int n) 8 | { 9 | var candidate = 0; 10 | 11 | for (int i = 1; i < n; i++) 12 | { 13 | if (Knows(candidate, i)) 14 | { 15 | candidate = i; 16 | } 17 | } 18 | 19 | return CheckCelebrity(n, candidate); 20 | } 21 | 22 | private bool Knows(int candidate, int i) 23 | { 24 | throw new NotImplementedException(); 25 | } 26 | 27 | private int CheckCelebrity(int totalCandidates, int candidate) 28 | { 29 | for (int i = 0; i < totalCandidates; i++) 30 | { 31 | if (i == candidate) 32 | { 33 | continue; 34 | } 35 | 36 | if (!Knows(i, candidate) || Knows(candidate, i)) 37 | { 38 | return -1; 39 | } 40 | } 41 | 42 | return candidate; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/LargestBST.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System; 3 | 4 | namespace InterviewPreparation.MicrosoftExcercises.Premium 5 | { 6 | class LargestBST 7 | { 8 | public int LargestBSTSubtree(TreeNode root) 9 | { 10 | return FindMax(root).count; 11 | } 12 | 13 | private (int min, int max, int count) FindMax(TreeNode root) 14 | { 15 | if (root == null) 16 | { 17 | return (int.MaxValue, int.MinValue, 0); 18 | } 19 | 20 | var left = FindMax(root.left); 21 | var right = FindMax(root.right); 22 | 23 | if (root.val > left.max && root.val < right.min) 24 | { 25 | return (Math.Min(root.val, left.min), Math.Max(root.val, right.max), left.count + right.count + 1); 26 | } 27 | 28 | return (int.MinValue, int.MaxValue, Math.Max(left.count, right.count)); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/MeetingRoomsII.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Premium 4 | { 5 | class MeetingRoomsII 6 | { 7 | public int MinMeetingRooms(int[][] intervals) 8 | { 9 | if (intervals.Length == 0) 10 | { 11 | return 0; 12 | } 13 | 14 | int[] start = new int[intervals.Length]; 15 | int[] end = new int[intervals.Length]; 16 | 17 | for (int i = 0; i < intervals.Length; i++) 18 | { 19 | start[i] = intervals[i][0]; 20 | end[i] = intervals[i][1]; 21 | } 22 | 23 | Array.Sort(start); 24 | Array.Sort(end); 25 | 26 | int startPointer = 0, endPointer = 0; 27 | int usedRooms = 0; 28 | 29 | while (startPointer < intervals.Length) 30 | { 31 | 32 | if (start[startPointer] >= end[endPointer]) 33 | { 34 | usedRooms -= 1; 35 | endPointer += 1; 36 | } 37 | 38 | usedRooms += 1; 39 | startPointer += 1; 40 | 41 | } 42 | 43 | return usedRooms; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/MovingAverage.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.MicrosoftExcercises.Premium 4 | { 5 | class MovingAverage 6 | { 7 | 8 | private LinkedList stream; 9 | private int maxSize; 10 | private double totalSum; 11 | 12 | /** Initialize your data structure here. */ 13 | public MovingAverage(int size) 14 | { 15 | stream = new LinkedList(); 16 | maxSize = size; 17 | totalSum = 0; 18 | } 19 | 20 | public double Next(int val) 21 | { 22 | if (stream.Count == maxSize) 23 | { 24 | totalSum -= stream.First.Value; 25 | stream.RemoveFirst(); 26 | } 27 | 28 | totalSum += val; 29 | 30 | stream.AddLast(val); 31 | 32 | return totalSum / (stream.Count * 1.0); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/ReverseWords3.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Premium 2 | { 3 | class ReverseWords3 4 | { 5 | public void ReverseWords(char[] s) 6 | { 7 | Reverse(s, 0, s.Length - 1); 8 | var index = 0; 9 | var left = 0; 10 | while (index <= s.Length) 11 | { 12 | while (index < s.Length && s[index] != ' ') 13 | { 14 | index++; 15 | } 16 | Reverse(s, left, index - 1); 17 | index++; 18 | left = index; 19 | } 20 | } 21 | private void Reverse(char[] s, int left, int right) 22 | { 23 | while (left < right) 24 | { 25 | var aux = s[left]; 26 | s[left] = s[right]; 27 | s[right] = aux; 28 | left++; 29 | right--; 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /InterviewPreparation/MicrosoftExcercises/Premium/WiggleSort.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.MicrosoftExcercises.Premium 2 | { 3 | class WiggleSort 4 | { 5 | public void Solve(int[] nums) 6 | { 7 | bool isLess = true; 8 | for (int i = 0; i < nums.Length - 1; i++) 9 | { 10 | if ((isLess && nums[i] > nums[i + 1]) || (!isLess && nums[i] < nums[i + 1])) 11 | { 12 | Swap(nums, i, i + 1); 13 | } 14 | 15 | isLess = !isLess; 16 | } 17 | } 18 | 19 | private void Swap(int[] nums, int i, int j) 20 | { 21 | var aux = nums[i]; 22 | nums[i] = nums[j]; 23 | nums[j] = aux; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /InterviewPreparation/Program.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | 3 | namespace InterviewPreparation 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | var nums = new int[] { 3, 2, 1, 5, 6, 4 }; 10 | 11 | 12 | var testing = new TopFrequentK().FindKthLargest(nums, 2); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /InterviewPreparation/Shared/GraphNode.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace InterviewPreparation.Shared 4 | { 5 | public class GraphNode 6 | { 7 | public int val; 8 | public IList neighbors; 9 | 10 | public GraphNode() 11 | { 12 | val = 0; 13 | neighbors = new List(); 14 | } 15 | 16 | public GraphNode(int _val) 17 | { 18 | val = _val; 19 | neighbors = new List(); 20 | } 21 | 22 | public GraphNode(int _val, List _neighbors) 23 | { 24 | val = _val; 25 | neighbors = _neighbors; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /InterviewPreparation/Theory/union-find/union-find-resource.md: -------------------------------------------------------------------------------- 1 | https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/DailyChallengesLC/DeepestLeavesSum.cs: -------------------------------------------------------------------------------- 1 | using InterviewPreparation.Exercises; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.TrainExercises.DailyChallengesLC 5 | { 6 | class DeepestLeavesSum 7 | { 8 | public int Solve(TreeNode root) 9 | { 10 | if (root == null) 11 | { 12 | return 0; 13 | } 14 | 15 | var sum = 0; 16 | 17 | var queue = new Queue(); 18 | 19 | queue.Enqueue(root); 20 | 21 | while (queue.Count > 0) 22 | { 23 | var queueSize = queue.Count; 24 | sum = 0; 25 | 26 | while (queueSize > 0) 27 | { 28 | var actual = queue.Dequeue(); 29 | queueSize--; 30 | sum += actual.val; 31 | 32 | if (actual.left != null) 33 | { 34 | queue.Enqueue(actual.left); 35 | } 36 | 37 | if (actual.right != null) 38 | { 39 | queue.Enqueue(actual.right); 40 | } 41 | } 42 | } 43 | 44 | return sum; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/BuyAndSellStocks.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.TrainExercises.Easy 2 | { 3 | public class BuyAndSellStocks 4 | { 5 | public int MaxProfit(int[] prices) 6 | { 7 | if (prices == null || prices.Length == 1) 8 | { 9 | return 0; 10 | } 11 | 12 | int buy = prices[0]; 13 | int sell = prices[0]; 14 | var profit = 0; 15 | 16 | for (int i = 1; i < prices.Length; i++) 17 | { 18 | if (prices[i] < sell) //time to sell 19 | { 20 | profit += sell - buy; 21 | buy = prices[i]; 22 | } 23 | 24 | sell = prices[i]; 25 | } 26 | 27 | if (buy < sell) 28 | { 29 | profit += sell - buy; 30 | } 31 | 32 | return profit; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/CountBinarySubStrings.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.TrainExercises.Easy 2 | { 3 | class CountBinarySubStrings 4 | { 5 | public int CountBinarySubstrings(string s) 6 | { 7 | var lastConsecutiveCount = 1; 8 | var consecutiveCount = 1; 9 | var total = 0; 10 | 11 | for (int i = 1; i < s.Length; i++) 12 | { 13 | if (s[i] != s[i - 1]) 14 | { 15 | lastConsecutiveCount = consecutiveCount; 16 | consecutiveCount = 1; 17 | total++; 18 | } 19 | else 20 | { 21 | consecutiveCount++; 22 | if (consecutiveCount <= lastConsecutiveCount) 23 | { 24 | total++; 25 | } 26 | } 27 | } 28 | 29 | return total; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/FindDiff.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.TrainExercises.Easy 2 | { 3 | class FindDiff 4 | { 5 | // https://leetcode.com/problems/find-the-difference/ 6 | public char FindTheDifference(string s, string t) 7 | { 8 | var bucket = new int[26]; 9 | 10 | foreach (var c in s) 11 | { 12 | bucket[c - 'a']++; 13 | } 14 | 15 | foreach (var c in t) 16 | { 17 | bucket[c - 'a']--; 18 | } 19 | 20 | for (int i = 0; i < bucket.Length; i++) 21 | { 22 | if (bucket[i] != 0) 23 | { 24 | return (char)(i + 'a'); 25 | } 26 | } 27 | 28 | return 'a'; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/LargestSubstringBetweenTwoEqualCharacters.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterviewPreparation.TrainExercises.Easy 5 | { 6 | class LargestSubstringBetweenTwoEqualCharacters 7 | { 8 | public int MaxLengthBetweenEqualCharacters(string s) 9 | { 10 | var max = -1; 11 | 12 | var dictionaryFirstPosition = new Dictionary(); 13 | 14 | for (int i = 0; i < s.Length; i++) 15 | { 16 | if (dictionaryFirstPosition.ContainsKey(s[i])) 17 | { 18 | max = Math.Max(max, i - dictionaryFirstPosition[s[i]] - 1); 19 | } 20 | else 21 | { 22 | dictionaryFirstPosition.Add(s[i], i); 23 | } 24 | } 25 | 26 | return max; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/MinimunChangesAlternatingBinaryString.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterviewPreparation.TrainExercises.Easy 4 | { 5 | // https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/ 6 | class MinimunChangesAlternatingBinaryString 7 | { 8 | public int MinOperations(string s) 9 | { 10 | var swapsOne = 0; 11 | var swapsTwo = 0; 12 | 13 | for (int i = 0; i < s.Length; i++) 14 | { 15 | if (i % 2 == 0) 16 | { 17 | if (s[i] == '1') 18 | { 19 | swapsOne++; 20 | } 21 | else 22 | { 23 | swapsTwo++; 24 | } 25 | } 26 | else 27 | { 28 | if (s[i] == '0') 29 | { 30 | swapsOne++; 31 | } 32 | else 33 | { 34 | swapsTwo++; 35 | } 36 | } 37 | } 38 | 39 | return Math.Min(swapsOne, swapsTwo); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/Shuffle.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.TrainExercises.Easy 2 | { 3 | public class Solution 4 | { 5 | public int[] Shuffle(int[] nums, int n) 6 | { 7 | int[] shuffle = new int[nums.Length]; 8 | 9 | var i = 0; 10 | var j = nums.Length / 2; 11 | int x = 0; 12 | 13 | while (x < nums.Length) 14 | { 15 | shuffle[x] = nums[i]; 16 | x++; 17 | i++; 18 | if (x < nums.Length) 19 | { 20 | shuffle[x] = nums[j]; 21 | x++; 22 | j++; 23 | } 24 | } 25 | 26 | return shuffle; 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/SumAllDigits.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.TrainExercises.Easy 2 | { 3 | class SumAllDigits 4 | { 5 | //https://leetcode.com/problems/add-digits/submissions/ 6 | public int AddDigits(int num) 7 | { 8 | var aux = num; 9 | 10 | while (aux > 9) 11 | { 12 | var tmp = aux; 13 | 14 | aux = 0; 15 | 16 | while (tmp > 0) 17 | { 18 | aux += tmp % 10; 19 | tmp = tmp / 10; 20 | } 21 | } 22 | 23 | return aux; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /InterviewPreparation/TrainExercises/Easy/ValidAnagrams.cs: -------------------------------------------------------------------------------- 1 | namespace InterviewPreparation.TrainExercises.Easy 2 | { 3 | class ValidAnagrams 4 | { 5 | public bool IsAnagram(string s, string t) 6 | { 7 | int[] bucket = new int[26]; 8 | 9 | foreach (var character in s) 10 | { 11 | bucket[character - 'a']++; 12 | } 13 | 14 | foreach (var character in t) 15 | { 16 | bucket[character - 'a']--; 17 | } 18 | 19 | foreach (var b in bucket) 20 | { 21 | if (b != 0) 22 | { 23 | return false; 24 | } 25 | } 26 | 27 | return true; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /OOD/OOD.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Account.cs: -------------------------------------------------------------------------------- 1 | namespace OOD.StackOverflow 2 | { 3 | public class Account 4 | { 5 | public long Id { get; set; } 6 | 7 | public string Username { get; set; } 8 | 9 | public string Name { get; set; } 10 | 11 | public string Email { get; set; } 12 | 13 | public string Password { get; set; } 14 | 15 | public int Reputation { get; set; } 16 | 17 | public bool ResetPassword(string newPassword) 18 | { 19 | Password = newPassword; 20 | 21 | return true; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Admin.cs: -------------------------------------------------------------------------------- 1 | namespace OOD.StackOverflow 2 | { 3 | public class Admin : Member 4 | { 5 | public bool BlockMember(Member member) 6 | { 7 | return false; 8 | } 9 | 10 | public bool UnblockMember(Member member) 11 | { 12 | return false; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Answer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OOD.StackOverflow 4 | { 5 | public class Answer 6 | { 7 | public string AnswerText { get; set; } 8 | 9 | public bool Accepted { get; set; } 10 | 11 | public int VoteCount { get; set; } 12 | 13 | public int FlagCount { get; set; } 14 | 15 | public DateTime Creation { get; set; } 16 | 17 | public Member Owner { get; set; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Badge.cs: -------------------------------------------------------------------------------- 1 | namespace OOD.StackOverflow 2 | { 3 | public class Badge 4 | { 5 | private string _name; 6 | 7 | private string _description; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Bounty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OOD.StackOverflow 4 | { 5 | public class Bounty 6 | { 7 | public int Reputation { get; set; } 8 | 9 | public DateTime ExpireIn { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /OOD/StackOverflow/Comment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace OOD.StackOverflow 6 | { 7 | public class Comment 8 | { 9 | public long Id { get; set; } 10 | 11 | public string Content { get; set; } 12 | 13 | public int Upvote { get; set; } 14 | 15 | public int FlagCount { get; set; } 16 | 17 | public Member Owner { get; set; } 18 | 19 | public DateTime CreationTime { get; set; } 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Enums.cs: -------------------------------------------------------------------------------- 1 | namespace OOD.StackOverflow 2 | { 3 | public enum QuestionStatus 4 | { 5 | OPEN, 6 | CLOSED, 7 | ON_HOLD, 8 | DELETED 9 | } 10 | 11 | public enum QuestionClosingRemark 12 | { 13 | DUPLICATE, 14 | OFF_TOPIC, 15 | TOO_BROAD, 16 | NOT_CONSTRUCTIVE, 17 | NOT_A_REAL_QUESTION, 18 | PRIMARILY_OPINION_BASED 19 | } 20 | 21 | public enum AccounStatus 22 | { 23 | ACTIVE, 24 | CLOSED, 25 | CANCELED, 26 | BLACKLISTED, 27 | BLOCKED 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Guest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace OOD.StackOverflow 4 | { 5 | class Guest 6 | { 7 | public int GuestId { get; set; } 8 | 9 | private ISearch _search; 10 | 11 | public Guest() 12 | { 13 | 14 | } 15 | 16 | public IEnumerable GetQuestions(string query) 17 | { 18 | return _search.Search(query); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /OOD/StackOverflow/ISearch.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace OOD.StackOverflow 4 | { 5 | interface ISearch 6 | { 7 | IEnumerable Search(string query); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Member.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace OOD.StackOverflow 4 | { 5 | public class Member 6 | { 7 | public Account Account { get; set; } 8 | 9 | public IEnumerable Badges { get; set; } 10 | 11 | public Member() 12 | { 13 | } 14 | 15 | public bool AddQuestion(Question question) 16 | { 17 | return false; 18 | } 19 | 20 | public bool AddTag(Tag question) 21 | { 22 | return false; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Moderator.cs: -------------------------------------------------------------------------------- 1 | namespace OOD.StackOverflow 2 | { 3 | public class Moderator : Member 4 | { 5 | public bool CloseQuestion(Question question) 6 | { 7 | return false; 8 | } 9 | 10 | public bool OpenQuestion(Question question) 11 | { 12 | return false; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Notification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace OOD.StackOverflow 6 | { 7 | class Notification 8 | { 9 | private int notificationId; 10 | 11 | private DateTime createdOn; 12 | private string content; 13 | 14 | public bool SendNotification() 15 | { 16 | return true; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Question.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace OOD.StackOverflow 4 | { 5 | public class Question 6 | { 7 | private string title; 8 | private string description; 9 | private int UpvoteCount; 10 | private int FlagCount; 11 | private int ViewCount; 12 | private Member Owner; 13 | private IEnumerable Answers; 14 | private QuestionClosingRemark questionClosingRemark; 15 | private QuestionStatus questionStatus; 16 | 17 | private Bounty bounty; 18 | private IEnumerable Comments { get; set; } 19 | 20 | 21 | public Question() 22 | { 23 | 24 | } 25 | 26 | public bool Close() 27 | { 28 | if (questionStatus != QuestionStatus.CLOSED) 29 | { 30 | return false; 31 | } 32 | 33 | questionStatus = QuestionStatus.CLOSED; 34 | 35 | return true; 36 | } 37 | 38 | public bool UnClose() 39 | { 40 | return false; 41 | } 42 | 43 | public void AddComment(Comment comment) 44 | { 45 | } 46 | 47 | public bool AddBounty(Bounty bounty) 48 | { 49 | return false; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /OOD/StackOverflow/Tag.cs: -------------------------------------------------------------------------------- 1 | namespace OOD.StackOverflow 2 | { 3 | public class Tag 4 | { 5 | private string name; 6 | private string description; 7 | private long freqDaily; 8 | private long freqWeekly; 9 | } 10 | } 11 | --------------------------------------------------------------------------------