By Hardik B Vanza
This guide organizes 130 popular LeetCode questions into 14 essential patterns to help you master coding interviews. Each pattern includes a brief explanation for better understanding, and a list of questions with their topics, company frequencies, and direct LeetCode links. Questions that don’t fit into these patterns are listed under "Other Techniques." Let’s dive in!
Explanation:
The Sliding Window pattern processes a specific "window" of elements in an array or string. The window slides right, performing operations like finding the longest/shortest subarray or substring. The window size can be fixed or dynamic.
When to Use?
1. Contains Duplicate II (Array)
Companies: Amazon - 6, Facebook - 5, Google - 2, Microsoft - 2, Bloomberg - 3
2. Longest Substring Without Repeating Characters (String)
Companies: Amazon - 58, Microsoft - 30, Bloomberg - 22, Facebook - 20, Google - 19
3. Longest Repeating Character Replacement (String)
Companies: Google - 9, Uber - 7, Amazon - 5, Adobe - 2, Facebook - 4
4. Minimum Window Substring (String)
Companies: Facebook - 18, Amazon - 16, LinkedIn - 9, Lyft - 9, Microsoft - 8
5. Sliding Window Maximum (Heap)
Companies: Amazon - 34, Google - 13, Apple - 7, Facebook - 5, Microsoft - 4
Explanation:
The Two Pointers pattern uses two pointers moving through a data structure, often in sorted arrays or linked lists, to optimize solutions. It reduces time complexity from O(n²) to O(n) or O(n log n).
When to Use?
1. Two Sum II (Array)
Companies: Amazon - 10, Apple - 4, Google - 2, Facebook - 2
2. 3Sum (Array)
Companies: Amazon - 40, Microsoft - 24, Facebook - 21, Apple - 16, Google - 8
3. Container With Most Water (Array)
Companies: Amazon - 31, Microsoft - 8, Facebook - 8, Apple - 5, Google - 5
4. Remove Duplicates from Sorted Array(Array)
Companies: Amazon - 3, Apple - 2, Microsoft - 6, Google - 4, Facebook - 4
5. Trapping Rain Water (Array)
Companies: Amazon - 51, Goldman Sachs - 44, Bloomberg - 20, Facebook - 19, Microsoft - 12
6. Merge Sorted Array (Sorting)
Companies: Facebook - 44, Microsoft - 13, Amazon - 9, Apple - 6, LinkedIn - 4
7. Sort Colors (Sorting)
Companies: Amazon - 10, Microsoft - 7, Apple - 7, Facebook - 3, Google - 2
8. Valid Palindrome (String)
Companies: Facebook - 74, Amazon - 10, Microsoft - 9, Apple - 6, Bloomberg - 4
9. Longest Palindromic Substring String)
Companies: Amazon - 40, Microsoft - 19, Google - 15, Adobe - 10, Apple - 10
Explanation:
The Fast and Slow Pointers (Hare & Tortoise) pattern uses two pointers moving at different speeds (fast at 2x, slow at 1x). It’s ideal for detecting loops in linked lists or arrays.
When to Use?
1. Detect Cycle in a Linked List (Linked List)
Companies: Amazon - 9, Spotify - 4, Apple - 2, Microsoft - 2, Google - 2
2. Remove Nth Node From End Of List (Linked List)
Companies: Facebook - 18, Amazon - 9, Google - 4, Microsoft - 4, Apple - 3
3. Middle of the Linked List (Linked List)
Companies: Apple - 4, Amazon - 3, Facebook - 2, Google - 2, Microsoft - 3
4. Palindrome Linked List (Linked List)
Companies: Amazon - 16, Facebook - 7, Microsoft - 7, Apple - 7, Google - 5
Explanation:
The Merge Intervals pattern handles overlapping intervals by merging or checking them. Understanding six cases of interval relations is key.
When to Use?
1. Insert Interval (Interval)
Companies: Google - 15, LinkedIn - 8, Facebook - 6, Amazon - 4, Microsoft - 2
2. Merge Intervals (Interval)
Companies: Facebook - 87, Amazon - 57, Google - 31, Apple - 22, Microsoft - 17
3. Non-overlapping Intervals (Interval)
Companies: Facebook - 6, Amazon - 5, Microsoft - 3, Apple - 5, Google - 5
4. Meeting Rooms (Interval)
Companies: Google - 3, Amazon - 2, Microsoft - 6, Facebook - 2, Bloomberg - 2
5. Meeting Rooms II (Interval)
Companies: Amazon - 58, Google - 22, Facebook - 20, Bloomberg - 19, Microsoft - 18
6. Employee Free Time (Interval)
Companies: Google - 10, Uber - 4, Apple - 3, Microsoft - 2, Amazon - 3
Explanation:
Cyclic Sort is used for arrays with numbers in a specific range. It swaps each number to its correct index, achieving O(n) complexity. Ideal for finding missing or duplicate numbers.
When to Use?
1. First Missing Positive (Hashing)
Companies: Amazon - 18, Microsoft - 10, Adobe - 5, Google - 3, Apple - 3
2. Missing Number (Binary-Bit Manipulation)
Companies: Microsoft - 13, Amazon - 12, Apple - 4, Adobe - 3, Facebook - 3
Explanation:
This pattern reverses linked list node links in-place without extra memory. A current pointer starts at the head, and a previous pointer reverses links step-by-step.
When to Use?
1. Reverse a Linked List (Linked List)
Companies: Amazon - 15, Apple - 8, Microsoft - 7, Google - 4, Facebook - 3
2. Reorder List (Linked List)
Companies: Amazon - 9, Microsoft - 8, Facebook - 3, Google - 3, Uber - 3
3. Reverse Nodes in k-Group (Linked List)
Companies: Microsoft - 15, Amazon - 13, Google - 3, Apple - 3, Facebook - 2
Explanation:
Tree BFS (Breadth First Search) traverses a tree level-by-level using a queue. Start with the root in the queue, process each level, and add children to the queue.
When to Use?
1. Binary Tree Level Order Traversal (Tree)
Companies: Amazon - 11, LinkedIn - 11, Microsoft - 9, Facebook - 5, Google - 2
2. Binary Tree Zigzag Level Order Traversal (Tree)
Companies: Facebook - 8, Bloomberg - 8, Amazon - 3, Microsoft - 2, Apple - 2
3. Find Leaves of Binary Tree (Tree)
Companies: Google - 52, LinkedIn - 15, Amazon - 8, Microsoft - 4, Facebook - 2
Explanation:
Tree DFS (Depth First Search) traverses a tree depth-first (pre-order, in-order, or post-order) using recursion or a stack.
When to Use?
1. Maximum Depth of Binary Tree (Tree)
Companies: LinkedIn - 8, Amazon - 5, Google - 5, Spotify - 4, Microsoft - 4
2. Same Tree (Tree)
Companies: LinkedIn - 8, Amazon - 4, Google - 2, Bloomberg - 2, Apple - 2
3. Symmetric Tree (Tree)
Companies: Amazon - 9, LinkedIn - 5, Google - 5, Facebook - 4, Apple - 3
4. Invert/Flip Binary Tree (Tree)
Companies: Amazon - 5, Facebook - 5, Google - 3, Microsoft - 2, Apple - 2
5. Binary Tree Maximum Path Sum (Tree)
Companies: Amazon - 20, DoorDash - 18, Microsoft - 12, Facebook - 9, Google - 6
6. Serialize and Deserialize Binary Tree (Tree)
Companies: Amazon - 26, Microsoft - 16, LinkedIn - 15, Uber - 6, Google - 4
7. Subtree of Another Tree (Tree)
Companies: Amazon - 7, Microsoft - 2, Google - 2, Facebook - 9, Apple - 2
8. Construct Binary Tree from Preorder and Inorder Traversal (Tree)
Companies: Microsoft - 6, Amazon - 3, Bloomberg - 2, Google - 2, Apple - 2
9. Validate Binary Search Tree (Tree)
Companies: Amazon - 25, Microsoft - 12, Bloomberg - 12, Facebook - 5, Uber - 3
10. Kth Smallest Element in a BST (Tree)
Companies: Uber - 17, Amazon - 8, Microsoft - 3, Facebook - 2, Apple - 2
11. Lowest Common Ancestor of BST (Tree)
Companies: LinkedIn - 12, Facebook - 5, Amazon - 3, Apple - 3, Google - 2
Explanation:
The Two Heaps pattern uses a Min Heap and a Max Heap to balance or find medians efficiently. The first half is stored in a Max Heap (largest element), and the second half in a Min Heap (smallest element).
When to Use?
1. Find Median from Data Stream (Heap)
Companies: Amazon - 26, Microsoft - 14, Facebook - 6, Apple - 5, Google - 4
Explanation:
The Subsets pattern generates permutations and combinations using BFS or recursion. Start with an empty set and add elements to create new subsets.
When to Use?
1. Combination Sum (Dynamic Programming)
Companies: Amazon - 5, Google - 3, Microsoft - 2, Facebook - 3, Apple - 4
2. Generate Parentheses (Dynamic Programming)
Companies: Amazon - 38, Facebook - 13, Microsoft - 9, Apple - 9, Bloomberg - 7
3. Letter Combinations of a Phone Number (String)
Companies: Amazon - 38, Microsoft - 27, Facebook - 10, Apple - 9, Google - 8
Explanation:
Modified Binary Search adapts binary search for rotated arrays or other variations. It calculates the middle index and narrows the search range based on the key.
When to Use?
1. Find Minimum in Rotated Sorted Array (Array)
Companies: Amazon - 8, Microsoft - 7, Facebook - 5, Uber - 3, Apple - 2
2. Search in Rotated Sorted Array (Array)
Companies: Amazon - 27, Facebook - 22, Microsoft - 22, LinkedIn - 21, Apple - 5
3. Find First and Last Position of Element in Sorted Array (Array)
Companies: Facebook - 23, Amazon - 21, Microsoft - 8, Bloomberg - 6, Google - 6
4. Median of Two Sorted Arrays (Array)
Companies: Amazon - 39, Apple - 20, Microsoft - 18, Google - 15, Bloomberg - 9
Explanation:
The Top K Elements pattern finds the top/smallest/frequent K elements using heaps or sorting.
When to Use?
1. High Five (Heap)
Companies: Goldman Sachs - 17, Amazon - 5
2. Top K Frequent Elements (Heap)
Companies: Facebook - 73, Amazon - 30, Microsoft - 8, Google - 7, Uber - 7
3. Kth Largest Element in an Array (Heap)
Companies: Facebook - 64, Amazon - 19, LinkedIn - 14, Microsoft - 9, Google - 6
Explanation:
The K-way Merge pattern combines K sorted lists or arrays using a min-heap. Insert the smallest element from each list into the heap, remove the smallest, and add the next element.
When to Use?
1. Merge K Sorted Lists (Linked List)
Companies: Amazon - 54, Facebook - 42, Microsoft - 17, Apple - 10, Google - 8
Explanation:
Topological Sort arranges vertices of a directed acyclic graph (DAG) in a linear order respecting dependencies. Use in-degrees to find sources and process them with a queue.
When to Use?
1. Course Schedule (Graph)
Companies: Amazon - 34, Google - 9, TikTok - 9, Microsoft - 8, Facebook - 7
2. Course Schedule II (Graph)
Companies: Amazon - 40, Google - 14, Microsoft - 13, Apple - 6, TikTok - 4
3. Alien Dictionary (Graph)
Companies: Airbnb - 18, Amazon - 12, Facebook - 9, Google - 6, Microsoft - 5
These questions don’t fit directly into the 14 patterns but are still important for coding interviews. They can be solved using other common techniques like Dynamic Programming, Graph DFS/BFS, Stack, Hashing, etc.
1. Two Sum (Array)
Companies: Amazon - 108, Apple - 43, Google - 39, Facebook - 22, Bloomberg - 13
2. Best Time to Buy and Sell Stock (Array)
Companies: Amazon - 62, Microsoft - 19, Bloomberg - 15, Facebook - 12, Google - 12
3. Contains Duplicate (Array)
Companies: Amazon - 17, Apple - 15, Google - 8, Microsoft - 6, Facebook - 5
4. Product of Array Except Self (Array)
Companies: Amazon - 34, Facebook - 13, Microsoft - 13, Apple - 8, Google - 2
5. Maximum Subarray (Array)
Companies: LinkedIn - 39, Amazon - 32, Apple - 19, Microsoft - 16, Google - 15
6. Maximum Product Subarray (Array)
Companies: LinkedIn - 26, Amazon - 21, Microsoft - 4, Google - 3, Infosys - 2
7. Verifying an Alien Dictionary (Array)
Companies: Facebook - 10, Uber - 4, Amazon - 2, LinkedIn - 3, Apple - 2
8. Next Permutation (Array)
Companies: Facebook - 33, Amazon - 19, Google - 12, Apple - 10, Microsoft - 8
9. Climbing Stairs (Dynamic Programming)
Companies: Amazon - 16, Microsoft - 5, Uber - 3, Google - 2, Apple - 2
10. Coin Change (Dynamic Programming)
Companies: Amazon - 28, Microsoft - 13, Adobe - 6, Apple - 6, Google - 3
11. Longest Increasing Subsequence (Dynamic Programming)
Companies: Google - 12, Microsoft - 10, Amazon - 6, TikTok - 3, Facebook - 3
12. Longest Common Subsequence (Dynamic Programming)
Companies: DoorDash - 7, Amazon - 6, Google - 4, Bloomberg - 3, Microsoft - 2
13. Word Break Problem (Dynamic Programming)
Companies: Amazon - 35, Facebook - 20, Apple - 6, Microsoft - 6, Google - 5
14. House Robber (Dynamic Programming)
Companies: Amazon - 25, Apple - 11, Google - 5, Microsoft - 5, Bloomberg - 3
15. House Robber II (Dynamic Programming)
Companies: Google - 11, TikTok - 2, Amazon - 6, Microsoft - 2, Facebook - 2
16. Decode Ways (Dynamic Programming)
Companies: Amazon - 9, TikTok - 7, Google - 6, Microsoft - 6, Lyft - 5
17. Unique Paths (Dynamic Programming)
Companies: Google - 17, Amazon - 10, Microsoft - 9, Facebook - 8, Apple - 6
18. Pascal's Triangle (Dynamic Programming)
Companies: Amazon - 13, Microsoft - 4, Google - 3, Apple - 2, Facebook - 2
19. Jump Game (Dynamic Programming)
Companies: Amazon - 21, Facebook - 5, Apple - 4, Google - 3, DoorDash - 2
20. Jump Game II (Dynamic Programming)
Companies: Amazon - 12, DoorDash - 4, Bloomberg - 3, Google - 3, Microsoft - 3
21. Regular Expression Matching (Dynamic Programming)
Companies: Microsoft - 18, Amazon - 8, Facebook - 6, Google - 6, Bloomberg - 3
22. Race Car (Dynamic Programming)
Companies: Google - 92, Amazon - 12, Microsoft - 2, Apple - 2
23. Clone Graph (Graph)
Companies: Facebook - 28, Bloomberg - 8, Amazon - 7, Google - 5, Microsoft - 3
24. Pacific Atlantic Water Flow (Graph)
Companies: Amazon - 6, Google - 6, Microsoft - 4, Uber - 3, Facebook - 2
25. Number of Islands (Graph)
Companies: Amazon - 115, Microsoft - 44, Bloomberg - 29, Facebook - 25, Google - 16
26. Longest Consecutive Sequence (Graph)
Companies: Amazon - 21, Google - 12, Microsoft - 9, Bloomberg - 6, Apple - 6
27. Graph Valid Tree (Graph)
Companies: LinkedIn - 9, Google - 3, Microsoft - 3, Amazon - 2, Bloomberg - 2
28. Number of Provinces (Graph)
Companies: Amazon - 22, DoorDash - 8, Apple - 2, Google - 2, Bloomberg - 3
29. Find the Celebrity (Graph)
Companies: Amazon - 5, Microsoft - 4, LinkedIn - 4, Facebook - 5, Apple - 3
30. Number of Connected Components in an Undirected Graph (Graph)
Companies: Amazon - 7, Facebook - 3, Google - 2, Microsoft - 2, Pinterest - 2
31. Roman to Integer (Hashing)
Companies: Amazon - 46, Adobe - 32, Apple - 27, Google - 25, Facebook - 11
32. Copy List with Random Pointer (Hashing)
Companies: Facebook - 31, Amazon - 30, Microsoft - 7, Bloomberg - 5, Apple - 2
32. Word Ladder (Graph)
Companies: Amazon - 53, Microsoft - 9, LinkedIn - 7, Facebook - 6, Google - 2
33. Remove Interval (Interval)
Companies: Google - 5, Amazon - 3, Microsoft - 1
34. Add Two Numbers (Linked List)
Companies: Amazon - 36, Apple - 17, Microsoft - 11, Facebook - 11, Google - 6
35. Merge Two Sorted Lists (Linked List)
Companies: Amazon - 21, Microsoft - 13, Facebook - 12, Apple - 4, Bloomberg - 4
36. Flatten Binary Tree to Linked List (Linked List)
Companies: Amazon - 9, Microsoft - 5, Facebook - 3, Apple - 2, Google - 2
37. Set Matrix Zeroes (Matrix)
Companies: Microsoft - 8, Amazon - 5, Bloomberg - 4, Facebook - 4, Apple - 2
38. Spiral Matrix (Matrix)
Companies: Microsoft - 20, Amazon - 10, Apple - 8, Facebook - 8, Google - 7