AmoghaKS / interview-prep-cpp

My solutions to coding interview problems on Leetcode, Algoexpert, Codewars and other interview preparation websites

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interview Preparation

My solutions to coding interview problems on LeetCode, AlgoExpert, Codewars, EPI book, Pramp, Codility and other interview preparation resources. Part of my #100 Days of Interview Preparation challenge. I will be adding my solutions to this repository every day starting September 15, 2019.

Completed: 38/49 days

Difficulty Levels solved

βšͺ Easy: 40
πŸ”΅ Medium: 67
πŸ”΄ Hard: 18
⚫ Very Hard: 3

Total: 128

Day 0

Adding solutions to all problems I have already completed

LeetCode

  1. πŸ”΅ 5-longest-palindromic-substring.cpp
  2. πŸ”΅ 19-remove-nth-node-from-end-of-list.cpp
  3. βšͺ 21-merge-two-sorted-lists.cpp
  4. βšͺ 101-symmetric-tree.cpp
  5. βšͺ 104-maximum-depth-of-binary-tree.cpp
  6. βšͺ 110-balanced-binary-tree.cpp
  7. βšͺ 118-pascals-triangle.cpp
  8. βšͺ 206-reverse-linked-list.cpp
  9. βšͺ 226-invert-binary-tree.cpp
  10. βšͺ 572-subtree-of-another-tree.cpp
  11. βšͺ 617-merge-two-binary-trees.cpp
  12. βšͺ 917-reverse-only-letters.cpp

AlgoExpert

  1. βšͺ bubble-sort.cpp
  2. βšͺ caesar-cipher-encryptor.cpp
  3. πŸ”΅ longest-palindromic-substring.cpp
  4. βšͺ palindrome-check.cpp
  5. πŸ”΅ remove-kth-node-from-end.cpp
  6. πŸ”΄ reverse-linked-list.cpp
  7. πŸ”΅ smallest-difference.cpp
  8. βšͺ two-number-sum.cpp

Day 1: September 15, 2019

Focus for today: Heaps/Priority Queues

LeetCode

  1. πŸ”΅ 347-top-k-frequent-elements.cpp
  2. πŸ”΅ 692-top-k-frequent-words.cpp

AlgoExpert

  1. πŸ”΅ min-heap-construction.cpp

Day 2: September 16, 2019

Focus for today: Heaps/Priority Queues

LeetCode

  1. πŸ”΄ 23-merge-k-sorted-lists.cpp Hint: No need to add entire lists to heaps at first!

AlgoExpert

  1. πŸ”΅ invert-binary-tree.cpp Hint: You can't invert right if you've already invert left.
  2. βšͺ binary-search.cpp Hint: Helper helps!

Day 3: September 17, 2019

Focus for today: Heaps/Priority Queues/Mock Interview Practice

LeetCode

  1. βšͺ 796-is-rotation.cpp Hint: Don't try anything fancy! Think about how strings wrap around.
  2. πŸ”΅ 229-majority-element-ii.cpp Hint: How many elements can be in the solution if majority is defined by having floor(n/3) elements?

AlgoExpert

  1. πŸ”΄ contiunous-median.cpp Hint: Remember to rebalance the heaps!

Day 4: September 18, 2019

Focus for today: Heaps/Priority Queues, Common LeetCode questions

LeetCode

  1. βšͺ 1-two-sum.cpp Hint: Think about what data structure to use. Don't try regular iteration!
  2. πŸ”΅ 2-add-two-numbers.cpp Hint: Recall elementary math! Also don't forget about the carry!

Day 5: September 19, 2019

Focus for today: Common LeetCode questions, Heaps/Priority Queues

LeetCode

  1. βšͺ 20-valid-parentheses.cpp Hint: Compare chars instead of strings!
  2. βšͺ 937-reorder-data-in-log-files.cpp Hint: find_first_of method can be used to find first space in a string!
  3. βšͺ 973-k-closest-points-to-origin.cpp Hint: Heap is your friend!

AlgoExpert

  1. πŸ”΅ bst-traversal.cpp Hint: Don't append to the array, replace it!

Elements of Programming Interviews

  1. πŸ”΅ 10-4-k-closest-stars.cpp Hint: Don't load everything into memory, use interators to point from beginning to end of stars list.

Day 6: September 20, 2019

Focus for today: Pramp interview

Pramp

  1. πŸ”΅ decode-variations.cpp Hint: Iterate backwards, don't actually create the variations.
  2. πŸ”΅ busiest-time-in-mall.cpp Hint: Time stamps can be repeated! What happens at the last one?.

Day 7: September 21, 2019

Focus for today: Heaps, Graphs

LeetCode

  1. πŸ”΅ 215-kth-largest-element-in-array.cpp Hint: Use a heap, but see if you can find a way to NOT include all elements in a heap.

AlgoExpert

  1. βšͺ depth-first-search.cpp Hint: Use recursion!
  2. πŸ”΅ has-single-cycle.cpp Hint: Make sure index is already corrected at the end of the for loop!
  3. πŸ”΅ breadth-first-search.cpp Hint: Iterate and think about what data structure to use.
  4. πŸ”΅ river-sizes.cpp Hint: Use DFS/BFS as soon as you find a river. Keep track of already visited ones.

Day 8: September 22, 2019

Focus for today: Graphs

LeetCode

  1. πŸ”΅ 200-number-of-islands.cpp Hint: Recursive DFS is much faster!

Day 9: September 23, 2019

Focus for today: Graphs

AlgoExpert

  1. πŸ”΅ youngest-common-ancestor.cpp Hint: Get to the same depth!
  2. πŸ”΄ lowest-common-manager.cpp Hint: Don't forget about the edgecase where one of the reports could be the manager!

Day 10: September 24, 2019

Focus for today: Graphs

LeetCode

  1. βšͺ 661-image-smoother.cpp Hint: Dont' over complicate it.

AlgoExpert

  1. ⚫ rectangle-mania.cpp Hint: What do diagonals tell you? Can a datastructure give you better lookup than the vector?

Day 11: September 25, 2019

Focus for today: Arrays and Strings

LeetCode

  1. πŸ”΅ 362-design-hit-counter.cpp Hint: What data structure helps you to remove older values easily?.

Pramp

  1. πŸ”΅ getting-a-different-number.cpp Hint: There are 3 ways to do it! O(N) is possible but not intuitive.

Day 12: September 26, 2019

Practiced but did not have any answers completed.

Day 13: September 27, 2019

Focus for today: Arrays and Strings

LeetCode

  1. πŸ”΅ 785-is-graph-bipartite.cpp Hint: Any neighbor of a node cannot have the same color as itself.
  2. πŸ”΅ 364-nested-list-weight-sum-ii.cpp Hint: Iterate through, add an integer as many times as there are levels below it.

Pramp

  1. πŸ”΄ word-count-engine.cpp Hint: You cannot do sorting and counting at the same time.

Day 14: September 28, 2019

Focus for today: Arrays and Strings

LeetCode

  1. βšͺ 268-missing-number.cpp Hint: What was gauss's formula? Use it!
  2. πŸ”΅ 15-3sum.cpp Hint: Remember to skip the repeated ones!

AlgoExpert

  1. πŸ”΅ three-number-sum.cpp Hint: Two loops is okay.
  2. πŸ”΄ four-number-sum.cpp Hint: Can you divide this into a two sum problem with 2 loops? Only store the tuple of values AFTER you have visited the first.
  3. πŸ”΄ subarray-sort.cpp Hint: Find the smallest and largest out of order numbers. Where will you place them now?.

Day 15: September 29, 2019

Focus for today: Arrays and Strings, Graphs

AlgoExpert

  1. πŸ”΄ largest-range.cpp Hint: Store everything in a data structure, and search forwards and backwards. Mark numbers you've already seen before.

60a. πŸ”΄ min-rewards.cpp (Naive) Hint: While going backwards, make sure to keep track of previous reward for that student! Choose the larger.

60b. πŸ”΄ min-rewards.cpp (Peaks and Valleys) Hint: Find valleys and iterate outwards incrementing rewards as you go. Don't forget to choose the larger between the previous and the new reward!

60c. πŸ”΄ min-rewards.cpp (Two sweep) Hint: Sweep once from either direction, increment rewards only on positive slope! Don't forget to choose the larger between the previous and the new reward!

LeetCode

  1. πŸ”΅ 994-rotting-oranges.cpp Hint: Use a datastructure to store depth for each newly rotten orange.

  2. βšͺ 121-best-time-to-buy-and-sell-stock.cpp Hint: Store the minimum so far and keep maximum difference at every time step.

  3. βšͺ 88-merge-sorted-array.cpp Hint: Do the merge step from mergesort from the back!.

  4. βšͺ 283-move-zeroes.cpp Hint: Use the fast/slow pointer approach! Keep track of where the last non-zero element was seen.

  5. πŸ”΅ 11-container-with-most-water.cpp Hint: Traverse from both ends.

Day 16: September 30, 2019

Focus for today: Arrays, Pramp interview

AlgoExpert

  1. πŸ”΄ zigzag-traverse.cpp Hint: Pay attention to the conditions when you need to NOT go diagonally. Keep track of the direction.

Day 17: October 1, 2019

Focus for today: Arrays

LeetCode

  1. πŸ”΅ 59-spiral-matrix-ii.cpp Hint: Keep track of beggining and ending indices of rows and columns as you rotate around the matrix. Increment or decrement as you finish any row or column.`
  2. πŸ”΅ 54-spiral-matrix.cpp Hint: Keep track of beggining and ending indices of rows and columns as you rotate around the matrix. Increment or decrement as you finish any row or column. Have you violated the constraints while inside the loop?`

Day 18: October 2, 2019

Focus for today: Arrays

LeetCode

  1. πŸ”΅ 161-one-edit-distance.cpp Hint: Iterate over both and keep account of differences.

Day 19: October 3, 2019

Focus for today: Arrays

LeetCode

  1. πŸ”΅ 48-rotate-image.cpp Hint: Keep track of row and column limits and go around in a circle. Make the circle smaller once all are done.

Day 20: October 4, 2019

Skipped, other priorities for the day.

Day 21: October 5, 2019

Focus for today: Dynamic Programming

AlgoExpert

  1. πŸ”΅ maximum-subset-sum-with-no-adjacent-elements.cpp Hint: Iterate looking backwards. The current max is the max of either the max at previous index, or sum of current index and the max and previous to previous index.

  2. πŸ”΅ number-of-ways-to-make-change.cpp Hint: For d in denominations, check each amount 0-n and add ways possible.

  3. πŸ”΅ min-number-of-coins-for-change.cpp Hint: Make a list from 0-n, and at each index find the min number of coins for change. Choose min of current value, and 1 + min ways to make change of the remainder after using a denomination

LeetCode

  1. βšͺ 53-maximum-subarray.cpp Hint: Maximum subarray at i is max of current number or sum of previous max and current number.

Day 22: October 6, 2019

Focus for today: Dynamic Programming, Mock interview

LeetCode

  1. πŸ”΅ 91-decode-ways.cpp Hint: Iterate backwards and count number of ways to decode if you combine two adjacent characters. Add up to index 0 and that's your answer.

  2. βšͺ 561-array-partition-i.cpp Hint: What does sorting tell you?

  3. βšͺ 7-reverse-integer.cpp Hint: Remember that current digit comes from modulo operator. How do you check whether your current answer has overflowed, either in positive or negative directions?

Day 23: October 7, 2019

Focus for today: Dynamic Programming, Mock interview

LeetCode

  1. πŸ”΅ 322-coin-change.cpp Hint: Make a list from 0-n, and at each index find the min number of coins for change. Choose min of current value, and 1 + min ways to make change of the remainder after using a denomination.

AlgoExpert

  1. πŸ”΅ levenshtein-distance.cpp Hint: Remember to consider empty spaces at the beginning of strings. Initialize the first row and column. If current character in str1 is not equal to that in str2, add 1 to min of surrounding three. Else, copy the diagonal value as if the new character did not exist.

Day 24: October 8, 2019

Focus for today: Graphs and review

AlgoExpert

  1. πŸ”΄ topological-sort.cpp Hint: Do recursive DFS and add each node after all of its pre-requisites are already in the list. To deal with cycles, keep track of nodes that are currently in the DFS stack.

  2. πŸ”΅ kadanes-algorithm.cpp Hint: Maximum subarray at i is max of current number or sum of previous max and current number.

LeetCode

  1. βšͺ 541-reverse-string-ii.cpp Hint: Swap from 0-k, then jump to index 2k.

  2. ⚫ 1192-critical-connections-in-a-network.cpp Hint: (Tarjan's) Do DFS, and convert undirected edges to directed in the order of DFS. Keep track of node ids and lowest id traversable from a node. Bridge occurs when id of current node is less than lowest node that can be found by the neighbor.

  3. πŸ”΅ 207-course-schedule.cpp Hint: Topological Sort. Keep track of cycles in pre-requisites by checking if the node you're currently checking is in the stack already. Remember to visit the node AFTER visiting all its neighbors.

Day 25: October 9, 2019

Focus for today: Coding challenge from a Company

Day 26: October 10, 2019

Skipped. Midterm at school.

Day 27: October 11, 2019

Focus for today: Dynamic Programming

AlgoExpert

  1. πŸ”΄ max-sum-increasing-subsequence.cpp Hint: At i, iterate from 0 to i-1 and check what the max sum increasing subsequence up until that point is. To build array, keep track of the previous index.

  2. πŸ”΄ water-area.cpp Hint: At every index, find the left max pillar and the right max pillar, then calculating area at that level is easy.

LeetCode

  1. πŸ”΄ 42-trapping-rain-water.cpp Hint: Extension of water-area.cpp Use only one array for holding the min if left max pillar and right max pillar.

Day 28: October 12, 2019

Focus for today: Dynamic Programming

  1. πŸ”΄ knapsack-problem.cpp Hint: Use a 2D array tracking maxValue possible with given items and given capacity size. Current max value is comes from either using the previous max value, or adding the current value with the max value obtained from adding the remainder into the knapsack. Build resulting array by backtracking from max value.

Day 29: October 13, 2019

Skipped. Preparing for midterm.

Day 30: October 14, 2019

Skipped. Preparing for midterm.

Day 31: October 15, 2019

Focus for today: Dynamic Programming

AlgoExpert

  1. πŸ”΄ longest-common-subsequence.cpp Hint: Create a 3D array to keep track of common subsequences between arrays ending at each index. If current characters match, use the diagonal value and append current character to it. Else, use the longer of top or left.

LeetCode

  1. βšͺ 70-climbing-stairs.cpp Hint: To get to the current step, you can either take a single 1-step from the previous one, or a single 2-step from the previous to previous one.

  2. βšͺ 746-min-cost-climbing-stairs.cpp Hint: Cost of climbing to current step is sum of cost of being on this step and minimum of either the cost of coming from previous step or previous to previous step.

Day 32: October 16, 2019

Focus for today: Dynamic Programming

AlgoExpert

  1. ⚫ max-profit-with-k-transactions.cpp Hint: Come back to this problem! Max profit at current transaction and day is obtained by either selling the stock or by not selling the stock and keeping the previous maximum. Keep track of max profit obtained thus far for each transaction.

Pramp

  1. πŸ”΅ matrix-spiral-copy.cpp Hint: Keep track of left, right, top and bottom boundaries and increment/decrement them as you go along.

Day 33: October 17, 2019

Focus for today: Dynamic Programming and Graphs

LeetCode

  1. πŸ”΅ 1042-flower-planting-with-no-adjacent.cpp Hint: Do not overthink this one! Look at neighbors and assign yourself the first color available.

  2. πŸ”΅ 1079-letter-tile-possibilities.cpp Hint: First create a map with character counts. Then do recursive DFS and count by removing one character at a time and one length at a time.

Day 34: October 18, 2019

Focus for today: Common Google problems

LeetCode

  1. πŸ”΅ 394-decode-string.cpp Hint: Use two stacks, one for string and the other for numbers. Do the necessary things when you encounter a number, either of the brackets or just a character.

  2. πŸ”΅ 139-word-break.cpp Hint: Think of it in terms of segmentation. A string can be segmented into words from dictionary if a part of it can be segmented, and the remaining directly exists in the dictionary.

Day 35: October 19, 2019

Focus for today: Common Google problems

LeetCode

  1. πŸ”΅ 743-network-delay-time.cpp Hint: Remember to check if the delay at any node can be updated to a smaller one!

  2. βšͺ 551-student-attendance-record-i.cpp Hint: Don't need one.

  3. βšͺ 359-logger-rate-limiter.cpp Hint: Just what it sounds like.

  4. πŸ”΅ 1055-shortest-way-to-form-string.cpp Hint: Use a pointer each for both strings, compare characters and move accordingly. Keep track of how many characters you skip, if you skipped all of source string, return -1.

Day 36: October 20, 2019

Focus for today: Common Google problems

LeetCode

  1. πŸ”΅ 253-meeting-rooms-ii.cpp Hint: How can I use a min-heap for this? Use ending times in the heap to check what the earliest end time is.

  2. πŸ”΅ 221-maximal-square.cpp Hint: Brute force: When you find one, assume square starts at top-left, keep expanding diagonally and record how many levels are valid (i.e., don't have a 0). Dynamic Programming: For the current index to be part of a square, which previous conditions need to be true?

  3. πŸ”΅ 56-merge-intervals.cpp Hint: Be careful in selecting what to compare the next index in the array with!

  4. πŸ”΅ 939-minimum-area-rectangle.cpp Hint: Brute force: Create a map from string to Point and for each point, search for points in top-right and attempt to create a rectangle. If it forms, calculate its area.

  5. πŸ”΅ 1007-minimum-domino-rotations-for-equal-row.cpp Hint: Choose first values and iterate through the lists, first checking how many rotations would be needed for making row A equal to A[0] and then row B equal to A[0]. If not possible, try with B[0].

  6. βšͺ 867-transpose-matrix.cpp Hint: Copy from [r][c] to [c][r].

AlgoExpert

  1. πŸ”΅ powerset.cpp Hint: Iteratively append the current element to all previously added subsets in the result.

Day 37: October 21, 2019

Focus for today: Common Google problems

LeetCode

  1. βšͺ 299-bulls-and-cows.cpp Hint: You can do it without O(N) additional space! Can you mark the already chosen ones?

Day 38: October 22, 2019

Skipped, preparing for project assignment.

Day 39: October 23, 2019

Skipped, preparing for project assignment.

Day 40: October 24, 2019

Focus for today: Mock interview

  1. πŸ”΅ 281-zigzag-iterator.cpp Hint: You don't need two pointers.

Day 41: October 25, 2019

Focus for today: Common questions

AlgoExpert

  1. βšͺ find-largest-three-numbers.cpp Hint: Use a min-heap to keep track of largest three.

  2. πŸ”΅ search-in-sorted-matrix.cpp Hint: At each iteration you can ignore either a row or column if it is not equal to target.

Day 42: October 26, 2019

Skipped: Attending CalHacks

Day 43: October 27, 2019

Skipped: Attending CalHacks

Day 44: October 28, 2019

Skipped: Attending CalHacks

Day 45: October 29, 2019

LeetCode

  1. πŸ”΅ 240-search-a-2d-matrix-ii.cpp Hint: Which corner do you want to start at? A corner that tells you more about which row or column you can ignore.

  2. πŸ”΅ 74-search-a-2d-matrix.cpp Hint: Find the correct row to search!

  3. πŸ”΅ 531-lonely-pixel-i.cpp Hint: Two passes are fine!

  4. βšͺ 141-linked-list-cycle.cpp Hint: Make two pointers race.

  5. βšͺ 198-house-robber.cpp Hint: What is the max profit at second position?

  6. πŸ”΄ 124-binary-tree-maximum-path-sum.cpp Hint: Keep a global max.

AlgoExpert

  1. πŸ”΅ balanced-brackets.cpp Hint: What are the cases when brackets are not balanced?

Day 46: October 30, 2019

LeetCode

  1. πŸ”΅ 79-word-search.cpp Hint: What happens if you go down the wrong path and need to reset back to the valid path? Backtracking!

  2. πŸ”΄ 308-range-sum-query-2d-mutable.cpp Hint: There is a dynamic programming way to do later.

  3. βšͺ 26-remove-duplicates-from-sorted-array.cpp Hint: Use two pointers and keep the slow pointer as the end of the array you return.

Day 47: October 31, 2019

Pramp

  1. πŸ”΅ sales-path.cpp Hint: Same as finding min cost path.

LeetCode

  1. πŸ”΅ 105-construct-binary-tree-from-preorder-and-inorder-traversal.cpp Hint: Identify which nodes belong to the right and left subtree in both traversals.

Day 48: November 1, 2019

Skipped, working on assignment.

Day 49: November 2, 2019

Pramp

  1. πŸ”΅ array-of-array-products.cpp Hint: Forwards and Backwards pass.

LeetCode

  1. πŸ”΅ 238-product-of-array-except-self.cpp Hint: Forwards and backwards pass.

AlgoExpert

  1. πŸ”΄ find-loop.cpp Hint: Do the math! Find the relation between where you are and where the intersection is. Then move one of the pointers to the head and traverse both at the same speed.

  2. πŸ”΄ shifted-binarySearch.cpp Hint: Add an extra condition to choose where to perform binary search.

About

My solutions to coding interview problems on Leetcode, Algoexpert, Codewars and other interview preparation websites


Languages

Language:C++ 100.0%