This repo is intended for any individual wanting to improve their problem solving skills for Software Engineering interviews. Problems are grouped under their respective subtopic, in order to focus on repeatedly applying common patterns rather than randomly tackling questions.
All questions are available on leetcode.com with some requiring leetcode premium.
It is highly recommended to read chapters 1, 2, 3, 4, 8, and 10 of Cracking The Coding Interview to familiarize yourself with the following data structures and their operations:
- Arrays
- Maps
- Linked Lists
- Queues
- Heaps
- Stacks
- Trees
- Graphs
In addition, you should have a good grasp on common algorithms such as:
- Breadth-first search
- Depth-first search
- Binary search
- Recursion
This pdf contains useful information for the built-in data structures in Java.
Other useful methods that will help include substring()
, toCharArray()
, Math.max()
,
Math.min()
, and Arrays.fill()
.
Arrays
- Contains Duplicate: https://leetcode.com/problems/contains-duplicate/
- Missing Number: https://leetcode.com/problems/missing-number/
- Find All Numbers Disappeared in an Array: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
- Single Number: https://leetcode.com/problems/single-number/
- Product of Array Except Self: https://leetcode.com/problems/product-of-array-except-self/
- Find the Duplicate Number: https://leetcode.com/problems/find-the-duplicate-number/
- Find All Duplicates in an Array: https://leetcode.com/problems/find-all-duplicates-in-an-array/
- First Missing Positive: https://leetcode.com/problems/first-missing-positive/
- Longest Consecutive Sequence: https://leetcode.com/problems/longest-consecutive-sequence/
Backtracking
The backtracking technique can be found under 10. Subsets
here.
- Letter Case Permutation: https://leetcode.com/problems/letter-case-permutation/
- Subsets: https://leetcode.com/problems/subsets/
- Subsets II: https://leetcode.com/problems/subsets-ii/
- Permutations: https://leetcode.com/problems/permutations/
- Permutations II: https://leetcode.com/problems/permutations-ii/
- Combinations: https://leetcode.com/problems/combinations/
- Combination Sum: https://leetcode.com/problems/combination-sum/
- Combination Sum II: https://leetcode.com/problems/combination-sum-ii/
- Combination Sum III: https://leetcode.com/problems/combination-sum-iii/
- Generate Parentheses: https://leetcode.com/problems/generate-parentheses/
- Target Sum: https://leetcode.com/problems/target-sum/
- Palindrome Partitioning: https://leetcode.com/problems/palindrome-partitioning/
- Partition to K Equal Sum Subsets: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
- Letter Combinations of a Phone Number: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
- Generalized Abbreviation: https://leetcode.com/problems/generalized-abbreviation/
- Sudoku Solver: https://leetcode.com/problems/sudoku-solver/
- N-Queens: https://leetcode.com/problems/n-queens/
Dynamic Programming
Dynamic programming guides can be found on topcoder and the Back To Back SWE YouTube channel.
Dynamic Programming Patterns: https://leetcode.com/discuss/general-discussion/458695/Dynamic-Programming-Patterns
- Climbing Stairs: https://leetcode.com/problems/climbing-stairs/
- House Robber: https://leetcode.com/problems/house-robber/
- Best Time to Buy and Sell Stock: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
- Maximum Subarray: https://leetcode.com/problems/maximum-subarray/
- Range Sum Query - Immutable: https://leetcode.com/problems/range-sum-query-immutable/
- House Robber II: https://leetcode.com/problems/house-robber-ii/
- Coin Change: https://leetcode.com/problems/coin-change/
- Maximum Product Subarray: https://leetcode.com/problems/maximum-product-subarray/
- Longest Increasing Subsequence: https://leetcode.com/problems/longest-increasing-subsequence/
- Longest Palindromic Substring: https://leetcode.com/problems/longest-palindromic-substring/
- Word Break: https://leetcode.com/problems/word-break/
- Combination Sum: https://leetcode.com/problems/combination-sum-iv/
- Decode Ways: https://leetcode.com/problems/decode-ways/
- Unique Paths: https://leetcode.com/problems/unique-paths/
- Palindromic Substrings: https://leetcode.com/problems/palindromic-substrings/
- Number of Longest Increasing Subsequence: https://leetcode.com/problems/number-of-longest-increasing-subsequence/
- Partition Equal Subset Sum: https://leetcode.com/problems/partition-equal-subset-sum/
- Best Time to Buy and Sell Stock with Cooldown: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
- Counting Bits: https://leetcode.com/problems/counting-bits/
- Longest Valid Parentheses: https://leetcode.com/problems/longest-valid-parentheses/
Fast & Slow Pointers
The fast & slow pointer approach can be found under 3. Fast and Slow pointers
here.
- Linked List Cycle: https://leetcode.com/problems/linked-list-cycle/
- Middle of the Linked List: https://leetcode.com/problems/middle-of-the-linked-list/
- Palindrome Linked List: https://leetcode.com/problems/palindrome-linked-list/
- Remove Linked List Elements: https://leetcode.com/problems/remove-linked-list-elements/
- Remove Duplicates from Sorted List: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
- Linked List Cycle II: https://leetcode.com/problems/linked-list-cycle-ii/
- Add Two Numbers: https://leetcode.com/problems/add-two-numbers/
- Remove Nth Node From End Of List: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
- Sort List: https://leetcode.com/problems/sort-list/
- Reorder List: https://leetcode.com/problems/reorder-list/
Graph Traversals
- Clone Graph: https://leetcode.com/problems/clone-graph/
- Course Schedule: https://leetcode.com/problems/course-schedule/
- Pacific Atlantic Water Flow: https://leetcode.com/problems/pacific-atlantic-water-flow/
- Number of Islands: https://leetcode.com/problems/number-of-islands/
- Graph Valid Tree: https://leetcode.com/problems/graph-valid-tree/
- Number of Connected Components in an Undirected Graph: https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/
In-place Reversal of a Linked List
The in-place reveral technique can be found under 6. In-place reversal of linked list
here.
- Reverse Linked List: https://leetcode.com/problems/reverse-linked-list/
- Reverse Linked List II: https://leetcode.com/problems/reverse-linked-list-ii/
- Rotate List: https://leetcode.com/problems/rotate-list/
- Swap Nodes in Pairs: https://leetcode.com/problems/swap-nodes-in-pairs/
- Odd Even Linked List: https://leetcode.com/problems/odd-even-linked-list/
- Reverse Nodes in k-Group: https://leetcode.com/problems/reverse-nodes-in-k-group/
K-Way Merge
The k-way merge technique can be found under 13. K-way Merge
here.
- Merge Two Sorted Lists: https://leetcode.com/problems/merge-two-sorted-lists/
- Merge k Sorted Lists: https://leetcode.com/problems/merge-k-sorted-lists/
Matrices
- Set Matrix Zeroes: https://leetcode.com/problems/set-matrix-zeroes/
- Spiral Matrix: https://leetcode.com/problems/spiral-matrix/
- Rotate Image: https://leetcode.com/problems/rotate-image/
- Word Search: https://leetcode.com/problems/word-search/
- Kth Smallest Element in a Sorted Matrix: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
Intervals
The merge interval approach can be found under 4. Merge Intervals
here.
- Meeting Rooms: https://leetcode.com/problems/meeting-rooms
- Merge Intervals: https://leetcode.com/problems/merge-intervals/
- Interval List Intersections: https://leetcode.com/problems/interval-list-intersections/
- Non-overlapping Intervals: https://leetcode.com/problems/non-overlapping-intervals/
- Meeting Rooms II: https://leetcode.com/problems/meeting-rooms-ii/
- Task Scheduler: https://leetcode.com/problems/task-scheduler/
- Minimum Number of Arrows to Burst Balloons: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
- Insert Interval: https://leetcode.com/problems/insert-interval/
- Employee Free Time: https://leetcode.com/problems/employee-free-time/
Modified Binary Search
The modified binary search algorithm can be found under 11. Modified binary search
here.
- Binary Search: https://leetcode.com/problems/binary-search/
- Find Smallest Letter Greater Than Target: https://leetcode.com/problems/find-smallest-letter-greater-than-target/
- Peak Index in a Mountain Array: https://leetcode.com/problems/peak-index-in-a-mountain-array/
- Find Minimum in Rotated Sorted Array: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
- Find Peak Element: https://leetcode.com/problems/find-peak-element/
- Search in Rotated Sorted Array: https://leetcode.com/problems/search-in-rotated-sorted-array/
- Search in Rotated Sorted Array II: https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
- Search a 2D Matrix: https://leetcode.com/problems/search-a-2d-matrix/
- Search a 2D Matrix II: https://leetcode.com/problems/search-a-2d-matrix-ii/
- Count of Range Sum: https://leetcode.com/problems/count-of-range-sum/
Sliding Window
The sliding window approach can be found under 1. Sliding Window
here.
- Minimum Size Subarray Sum: https://leetcode.com/problems/minimum-size-subarray-sum/
- Fruit Into Baskets: https://leetcode.com/problems/fruit-into-baskets/
- Permutation in String: https://leetcode.com/problems/permutation-in-string/
- Longest Repeating Character Replacement: https://leetcode.com/problems/longest-repeating-character-replacement/
- Longest Substring Without Repeating Characters: https://leetcode.com/problems/longest-substring-without-repeating-characters/
- Sliding Window Maximum: https://leetcode.com/problems/sliding-window-maximum/
- Minimum Number of K Consecutive Bit Flips: https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/
- Unique Letter String: https://leetcode.com/problems/unique-letter-string/
- Substring with Concatenation of All Words: https://leetcode.com/problems/substring-with-concatenation-of-all-words/
Top 'K' Elements
The top K element technique can be found under 12. Top K elements
here.
- Kth Smallest Element in a BST: https://leetcode.com/problems/kth-smallest-element-in-a-bst/
- K Closest Points to Origin: https://leetcode.com/problems/k-closest-points-to-origin/
- Top K Frequent Elements: https://leetcode.com/problems/top-k-frequent-elements/
- Sort Characters By Frequency: https://leetcode.com/problems/sort-characters-by-frequency/
- Kth Largest Element in an Array: https://leetcode.com/problems/kth-largest-element-in-an-array/
- Find K Closest Elements: https://leetcode.com/problems/find-k-closest-elements/
- Reorganize String: https://leetcode.com/problems/reorganize-string/
- Rearrange String k Distance Apart: https://leetcode.com/problems/rearrange-string-k-distance-apart
- Course Schedule III: https://leetcode.com/problems/course-schedule-iii/
- Maximum Frequency Stack: https://leetcode.com/problems/maximum-frequency-stack/
Topological Sort
The topological sort algorithm can be found under 14. Topological sort
here.
- Course Schedule: https://leetcode.com/problems/course-schedule/
- Course Schedule II: https://leetcode.com/problems/course-schedule-ii/
- Minimum Height Trees: https://leetcode.com/problems/minimum-height-trees/
- Alien Dictionary: https://leetcode.com/problems/alien-dictionary
- Sequence Reconstruction: https://leetcode.com/problems/sequence-reconstruction
Tree Breadth First Search
The tree BFS technique can be found under 7. Tree BFS
here.
- Binary Tree Level Order Traversal II: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
- Average of Levels in Binary Tree: https://leetcode.com/problems/average-of-levels-in-binary-tree/
- Minimum Depth of Binary Tree: https://leetcode.com/problems/minimum-depth-of-binary-tree/
- Binary Tree Level Order Traversal: https://leetcode.com/problems/binary-tree-level-order-traversal/
- Binary Tree Zigzag Level Order Traversal: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
- Populating Next Right Pointers in Each Node: https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
- Populating Next Right Pointers in Each Node II: https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
- Binary Tree Right Side View: https://leetcode.com/problems/binary-tree-right-side-view/
- All Nodes Distance K in Binary Tree: https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
- Boundary of Binary Tree: https://leetcode.com/problems/boundary-of-binary-tree
Tree Depth First Search
The tree DFS technique can be found under 8. Tree DFS
here.
- Same Tree: https://leetcode.com/problems/same-tree/
- Path Sum: https://leetcode.com/problems/path-sum/
- Diameter of Binary Tree: https://leetcode.com/problems/diameter-of-binary-tree/
- Merge Two Binary Trees: https://leetcode.com/problems/merge-two-binary-trees/
- Maximum Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree/
- Lowest Common Ancestor of a Binary Search Tree: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
- Subtree of Another Tree: https://leetcode.com/problems/subtree-of-another-tree/
- Invert Binary Tree: https://leetcode.com/problems/invert-binary-tree/
- Path Sum II: https://leetcode.com/problems/path-sum-ii/
- Path Sum III: https://leetcode.com/problems/path-sum-iii/
- Lowest Common Ancestor of a Binary Tree: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
- Maximum Binary Tree: https://leetcode.com/problems/maximum-binary-tree/
- Maximum Width of Binary Tree: https://leetcode.com/problems/maximum-width-of-binary-tree/
- Construct Binary Tree from Preorder and Inorder Traversal: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
- Validate Binary Search Tree: https://leetcode.com/problems/validate-binary-search-tree/
- Kth Smallest Element in a BST: https://leetcode.com/problems/kth-smallest-element-in-a-bst/
- Implement Trie (Prefix Tree): https://leetcode.com/problems/implement-trie-prefix-tree/
- Binary Tree Maximum Path Sum: https://leetcode.com/problems/binary-tree-maximum-path-sum/
- Serialize and Deserialize Binary Tree: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/
- Word Search II: https://leetcode.com/problems/word-search-ii/
Two Heaps
The two heaps approach can be found under 9. Two heaps
here.
- Find Median from Data Stream: https://leetcode.com/problems/find-median-from-data-stream/
- Sliding Window Median: https://leetcode.com/problems/sliding-window-median/
- IPO: https://leetcode.com/problems/ipo/
Two Pointers
The two pointer approach can be found under 2. Two Pointers or Iterators
here.
- Two Sum II - Input array is sorted: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
- Remove Duplicates from Sorted List: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
- Squares of a Sorted Array: https://leetcode.com/problems/squares-of-a-sorted-array/
- Backspace String Compare: https://leetcode.com/problems/backspace-string-compare
- 3 Sum: https://leetcode.com/problems/3sum/
- 3 Sum Closest: https://leetcode.com/problems/3sum-closest/
- Subarrays with Product Less than K: https://leetcode.com/problems/subarray-product-less-than-k/
- Sort Colours: https://leetcode.com/problems/sort-colors/
- Minimum Window Substring: https://leetcode.com/problems/minimum-window-substring/
- Trapping Rain Water: https://leetcode.com/problems/trapping-rain-water/
- Container With Most Water: https://leetcode.com/problems/container-with-most-water/
If input array is sorted then
- Binary search
- Two pointers
If asked for all permutations/subsets then
- Backtracking
If given a tree then
- DFS
- BFS
If given a graph then
- DFS
- BFS
If given a linked list then
- Two pointers
If recursion is banned then
- Stack
If asked for maximum/minumum subarray/subset/options then
- Dynamic programming
If asked for top/least K items then
- Heap
If asked for common strings then
- Map
- Trie
Else
- Map/Set for O(1) time & O(n) space
- Sort input for O(nlogn) time and O(1) space
This list is heavily inspired from Grokking the Coding Interview with additional problems extracted from the Blind 75 list and this medium article on 14 patterns to ace any coding interview question.