These are the most popular coding interview questions, from easy to hard, in order.
The problems cover common patterns and algorithms in interviews:
- Binary search
- Two pointers
- Backtracking
- DFS
- BFS
- Dynamic programming
- Stack / Greedy / Heap
- Swap corresponding values
- Store one or more different values in the same pointer
- Trie / Map
Big O performance of common functions (Java).
List | Add | Remove | Get | Contains | Next | Data Structure |
---|---|---|---|---|---|---|
ArrayList | O(1) | O(n) | O(1) | O(n) | O(1) | Array |
LinkedList | O(1) | O(1) | O(n) | O(n) | O(1) | Linked List |
CopyOnWriteArrayList | O(n) | O(n) | O(1) | O(n) | O(1) | Array |
Set | Add | Remove | Contains | Next | Size | Data Structure |
---|---|---|---|---|---|---|
HashSet | O(1) | O(1) | O(1) | O(h/n) | O(1) | Hash Table |
LinkedHashSet | O(1) | O(1) | O(1) | O(1) | O(1) | Hash Table + Linked List |
EnumSet | O(1) | O(1) | O(1) | O(1) | O(1) | Bit Vector |
TreeSet | O(log n) | O(log n) | O(log n) | O(log n) | O(1) | Red-black tree |
CopyOnWriteArraySet | O(n) | O(n) | O(n) | O(1) | O(1) | Array |
ConcurrentSkipListSet | O(log n) | O(log n) | O(log n) | O(1) | O(n) | Skip List |
Queue | Offer | Peak | Poll | Remove | Size | Data Structure |
---|---|---|---|---|---|---|
PriorityQueue | O(log n) | O(1) | O(log n) | O(n) | O(1) | Priority Heap |
LinkedList | O(1) | O(1) | O(1) | O(1) | O(1) | Array |
ArrayDequeue | O(1) | O(1) | O(1) | O(n) | O(1) | Linked List |
ConcurrentLinkedQueue | O(1) | O(1) | O(1) | O(n) | O(n) | Linked List |
ArrayBlockingQueue | O(1) | O(1) | O(1) | O(n) | O(1) | Array |
PriorirityBlockingQueue | O(log n) | O(1) | O(log n) | O(n) | O(1) | Priority Heap |
SynchronousQueue | O(1) | O(1) | O(1) | O(n) | O(1) | None! |
DelayQueue | O(log n) | O(1) | O(log n) | O(n) | O(1) | Priority Heap |
LinkedBlockingQueue | O(1) | O(1) | O(1) | O(n) | O(1) | Linked List |
Map | Get | ContainsKey | Next | Data Structure |
---|---|---|---|---|
HashMap | O(1) | O(1) | O(h / n) | Hash Table |
LinkedHashMap | O(1) | O(1) | O(1) | Hash Table + Linked List |
IdentityHashMap | O(1) | O(1) | O(h / n) | Array |
WeakHashMap | O(1) | O(1) | O(h / n) | Hash Table |
EnumMap | O(1) | O(1) | O(1) | Array |
TreeMap | O(log n) | O(log n) | O(log n) | Red-black tree |
ConcurrentHashMap | O(1) | O(1) | O(h / n) | Hash Tables |
ConcurrentSkipListMap | O(log n) | O(log n) | O(1) | Skip List |
14 Patterns to Ace Any Coding Interview Question
- 1 Contains Duplicate | Solution
- 2 Missing Number | Solution
- 3 Find All Numbers Disappeared in an Array | Solution *
- 4 Single Number | Solution *
- 5 Climbing Stairs | Solution
- 6 Best Time to Buy and Sell Stock | Solution
- 7 Maximum Subarray subarray with the largest sum | Solution
- 8 Range Sum Query - Immutable | Solution
- 9 Linked List Cycle | Solution | Solution
- 10 Middle of the Linked List | Solution
- 11 Palindrome Linked List | Solution *
- 12 Remove Linked List Elements | Solution
- 13 Remove Duplicates from Sorted List | Solution
- 14 Reverse Linked List | Solution
- 15 Merge Two Sorted Lists | Solution
- 16 Meeting Rooms | Solution
- 17 Binary Search | Solution
- 18 Find Smallest Letter Greater Than Target | Solution *
- 19 Peak Index in a Mountain Array | Solution
- 20 Binary Tree Level Order Traversal II | Solution
- 21 Average of Levels in Binary Tree | Solution
- 22 Minimum Depth of Binary Tree | Solution
- 23 Same Tree | Solution
- 24 Path Sum | Solution
- 25 Diameter of Binary Tree | Solution
- 26 Merge Two Binary Trees | Solution *
- 27 Maximum Depth of Binary Tree | Solution
- 28 Lowest Common Ancestor of a Binary Search Tree | Solution
- 29 Subtree of Another Tree | Solution *
- 30 Invert Binary Tree | Solution
- 31 Two Sum | Solution
- 32 Squares of a Sorted Array | Solution
- 33 Backspace String Compare | Solution *
- 34 Longest Word in Dictionary | Solution (Trie)
- 35 Index Pairs of a String | Solution
- 36 Majority Element | Solution
- 37 Product of Array Except Self | Solution
- 38 Find the Duplicate Number | Solution
- 39 Find All Duplicates in an Array | Solution
- 40 Set Matrix Zeroes | Solution
- 41 Spiral Matrix | Solution
- 42 Rotate Image | Solution
- 43 Word Search | Solution
- 44 Letter Case Permutation | Solution
- 45 Subsets | Solution
- 46 Subsets II | Solution
- 47 Permutations | Solution
- 48 Permutations II | Solution
- 49 Combinations | Solution
- 50 Combination Sum | Solution
- 51 Combination Sum II | Solution
- 52 Combination Sum III | Solution
- 53 Generate Parentheses | Solution
- 54 Target Sum | Solution
- 55 Palindrome Partitioning | Solution
- 56 Letter Combinations of a Phone Number | Solution
- 57 Generalized Abbreviation | Solution
- 58 House Robber | Solution
- 59 House Robber II | Solution
- 60 Coin Change | Solution
- 61 Maximum Product Subarray | Solution
- 62 Longest Increasing Subsequence | Solution
- 63 Longest Palindromic Substring | Solution
- 64 Word Break | Solution
- 65 Combination Sum IV | Solution
- 66 Decode Ways | Solution
- 67 Unique Paths | Solution
- 68 Jump Game | Solution
- 69 Palindromic Substrings | Solution
- 70 Number of Longest Increasing Subsequence | Solution
- 71 Partition Equal Subset Sum | Solution
- 72 Partition to K Equal Sum Subsets | Solution
- 73 Best Time to Buy and Sell Stock with Cooldown | Solution
- 74 Counting Bits | Solution
- 75 Linked List Cycle II | Solution
- 76 Add Two Numbers | Solution
- 77 Remove Nth Node From End Of List | Solution
- 78 Sort List | Solution
- 79 Reorder List | Solution
- 80 Clone Graph | Solution
- 81 Pacific Atlantic Water Flow | Solution
- 82 Number of Islands | Solution
- 83 Graph Valid Tree | Solution
- 84 Number of Connected Components in an Undirected Graph | Solution
- 85 Reverse Linked List II | Solution
- 86 Rotate List | Solution
- 87 Swap Nodes in Pairs | Solution
- 88 Odd Even Linked List | Solution
- 89 Kth Smallest Element in a Sorted Matrix | Solution
- 90 Find K Pairs with Smallest Sums | Solution
- 91 Merge Intervals | Solution
- 92 Interval List Intersections | Solution
- 93 Non-overlapping Intervals | Solution
- 94 Meeting Rooms II | Solution
- 95 Task Scheduler | Solution
- 96 Minimum Number of Arrows to Burst Balloons | Solution
- 97 Find Minimum in Rotated Sorted Array | Solution
- 98 Find Peak Element | Solution
- 99 Search in Rotated Sorted Array | Solution
- 100 Search in Rotated Sorted Array II | Solution
- 101 Search a 2D Matrix | Solution
- 102 Search a 2D Matrix II | Solution
- 103 Find K Closest Elements | Solution
- 104 Minimum Size Subarray Sum | Solution(Sliding Window)
- 105 Fruit Into Baskets | Solution(Sliding Window)
- 106 Permutation in String / contains a permutation substring | Solution(Sliding Window)
- 107 Longest Repeating Character Replacement | Solution (Sliding Window)
- 108 Kth Smallest Element in a BST | Solution
- 109 K Closest Points to Origin | Solution
- 110 Top K Frequent Elements | Solution
- 111 Sort Characters By Frequency | Solution
- 112 Kth Largest Element in an Array | Solution
- 113 Reorganize String : adjacent characters are not the same | Solution
- 114 Course Schedule | Solution
- 115 Course Schedule II | Solution
- 116 Minimum Height Trees: root is the node with most child | Solution
- 117 Binary Tree Level Order Traversal | Solution
- 118 Binary Tree Zigzag Level Order Traversal | Solution
- 119 Populating Next Right Pointers in Each Node | Solution
- 120 Populating Next Right Pointers in Each Node II | Solution
- 121 Binary Tree Right Side View | Solution
- 122 All Nodes Distance K in Binary Tree | Solution
- 123 Path Sum II | Solution
- 124 Path Sum III | Solution
- 125 (LCA) Lowest Common Ancestor of a Binary Tree | Solution
- 126 Maximum Binary Tree | Solution
- 127 Maximum Width of Binary Tree | Solution
- 128 Construct Binary Tree from Preorder and Inorder Traversal | Solution
- 129 Validate Binary Search Tree | Solution
- 130 Implement Trie (Prefix Tree) | Solution
- 131 3 Sum | Solution
- 132 3 Sum Closest | Solution
- 133 Subarrays with Product Less than K | Solution
- 134 Sort Colours | Solution
- 135 Maximum XOR of Two Numbers in an Array | Solution
- 136 First Missing Positive
- 137 Longest Consecutive Sequence
- 138 Sudoku Solver
- 139 N-Queens
- 140 Reverse Nodes in k-Group
- 141 Merge k Sorted Lists
- 142 Smallest Range Covering Elements from K Lists
- 143 Insert Interval
- 144 Employee Free Time | Solution
- 145 Count of Range Sum
- 146 Sliding Window Maximum
- 147 Longest Substring Without Repeating Characters |
- 148 Minimum Number of K Consecutive Bit Flips
- 149 Count Unique Characters of All Substrings of a Given String
- 150 Minimum Window Substring
- 151 Substring with Concatenation of All Words | Solution
- 152 Rearrange String k Distance Apart | Solution
- 153 Course Schedule III
- 154 Maximum Frequency Stack
- 155 Alien Dictionary | Solution
- 156 Sequence Reconstruction | Solution
- 157 Binary Tree Maximum Path Sum
- 158 Serialize and Deserialize Binary Tree | Solution
- 159 Word Search II
- 160 Find Median from Data Stream | Solution
- 161 Sliding Window Median
- 162 Trapping Rain Water |Solution
- 163 Container With Most Water |Solution
- 164 Concatenated Words | Solution
- 165 Prefix and Suffix Search | Solution
- 166 Palindrome Pairs
- 167 Design Search Autocomplete System | Solution
- 168 Word Squares | Solution
- 169 Sort Items by Groups Respecting Dependencies |Solution
- 170 Median of Two Sorted Arrays
- SubArray Sum K | Solution
- Daily Temperatures | Solution
- Evaluate Division | Solution
- Random Pick with Weight | Solution (meta)
- Word Ladder | Solution
- Basin Calculator & Advance Calculator i,ii,iii Word Calculator | Solution Basic +-*/ | Solution +-*/and brackets() | Solution Word calculator : eval("(five plus negative four) times (three plus four)") == 7
- Nested Parser / Mini Parser / JSON | Solution
- DecodeExpand String | Solution
- Subdomain visit count | Solution
- Test justification
- Minimum Area Rectangle or Count of Unique Rectangle
- Log and process counter
- Common Course
- Maximum Mines in 2D plane
- Minimum Number of Refueling Stops | solution
- Max random index integer in O(1)
- Range Sum BST
- Insert Delete GetRandom O(1)
- Tax Slab
- Design Add and Search Words Data Structure
- Vertical Order Traversal of a Binary Tree
- Lowest Common Ancestor of a Binary Tree III or Intersection of two list | LCA Binary Tree | LCA BST
- Connecting Cities With Minimum Cost
- Valid Word Abbreviation
- Perimeter or Boundary of Tree
- Restore IP Address
- Travel Flight Plan Reconstruct Itinerary | Solution
- Account Merge Group Similar Email Contacts | solution
Graph | |||
---|---|---|---|
BFS | DFS | Topoligical Sort | Union Find |
Binary Tree Level Order Traversal II | Minimum Depth of Binary Tree | Course Schedule | Graph Valid Tree |
Average of Levels in Binary Tree | Same Tree | Course Schedule II | Number of Connected Components in an Undirected Graph |
Minimum Depth of Binary Tree | Path Sum | Minimum Height Trees | Number of Islands |
Clone Graph | Diameter of Binary Tree | Alien Dictionary | |
Pacific Atlantic Water Flow | Merge Two Binary Trees | Sequence Reconstruction | |
Number of Islands | Maximum Depth of Binary Tree | Sort Items by Groups Respecting Dependencies | |
Graph Valid Tree | Lowest Common Ancestor of a Binary Search Tree | ||
Number of Connected Components in an Undirected Graph | Subtree of Another Tree | ||
Course Schedule | Invert Binary Tree | ||
Course Schedule II | Target Sum | ||
Minimum Height Trees | Clone Graph | ||
Binary Tree Level Order Traversal | Pacific Atlantic Water Flow | ||
Binary Tree Zigzag Level Order Traversal | Number of Islands | ||
Populating Next Right Pointers in Each Node | Graph Valid Tree | ||
Populating Next Right Pointers in Each Node II | Number of Connected Components in an Undirected Graph | ||
Binary Tree Right Side View | Kth Smallest Element in a BST | ||
All Nodes Distance K in Binary Tree | Course Schedule | ||
Course Schedule II | |||
Binary Tree Right Side View | |||
All Nodes Distance K in Binary Tree | |||
Path Sum II | |||
Path Sum III | |||
Lowest Common Ancestor of a Binary Tree | |||
Maximum Binary Tree | |||
Maximum Width of Binary Tree | |||
Construct Binary Tree from Preorder and Inorder Traversal | |||
Validate Binary Search Tree | |||
Binary Tree Maximum Path Sum | |||
Word Search II | |||
Sort Items by Groups Respecting Dependencies |
Recursion | ||
---|---|---|
Backtracking | DP | Trie |
Word Search | Climbing Stairs | Longest Word in Dictionary |
Letter Case Permutation | Best Time to Buy and Sell Stock | Index Pairs of a String |
Subsets | Maximum Subarray | Implement Trie (Prefix Tree) |
Subsets II | Range Sum Query - Immutable | Maximum XOR of Two Numbers in an Array |
Permutations | Target Sum | Word Search II |
Permutations II | House Robber | Concatenated Words |
Combinations | House Robber II | Prefix and Suffix Search |
Combination Sum | Coin Change | Palindrome Pairs |
Combination Sum II | Maximum Product Subarray | Design Search Autocomplete System |
Combination Sum III | Longest Increasing Subsequence | Word Squares |
Generate Parentheses | Longest Palindromic Substring | |
Palindrome Partitioning | Word Break | |
Letter Combinations of a Phone Number | Combination Sum IV | |
Generalized Abbreviation | Decode Ways | |
Sudoku Solver | Unique Paths | |
N-Queens | Jump Game | |
Palindromic Substrings | ||
Number of Longest Increasing Subsequence | ||
Partition Equal Subset Sum | ||
Partition to K Equal Sum Subsets | ||
Best Time to Buy and Sell Stock with Cooldown | ||
Counting Bits |
Arrays | Binary Search | Linked List / Pointers | Sliding window |
---|---|---|---|
Contains Duplicate | Binary Search | Linked List Cycle | Minimum Size Subarray Sum |
Missing Number | Find Smallest Letter Greater Than Target | Middle of the Linked List | Fruit Into Baskets |
Find All Numbers Disappeared in an Array | Peak Index in a Mountain Array | Palindrome Linked List | Permutation in String |
Single Number | Find the Duplicate Number | Remove Linked List Elements | Longest Repeating Character Replacement |
Product of Array Except Self | Kth Smallest Element in a Sorted Matrix | Remove Duplicates from Sorted List | Longest Substring Without Repeating Characters |
Find the Duplicate Number | Find Minimum in Rotated Sorted Array | Linked List Cycle II | Sliding Window Maximum |
Find All Duplicates in an Array | Find Peak Element | Add Two Numbers | Minimum Number of K Consecutive Bit Flips |
Set Matrix Zeroes | Search in Rotated Sorted Array | Remove Nth Node From End Of List | Count Unique Characters of All Substrings of a Given String |
Spiral Matrix | Search in Rotated Sorted Array II | Sort List | Minimum Window Substring |
Rotate Image | Search a 2D Matrix | Reorder List | Substring with Concatenation of All Words |
First Missing Positive | Search a 2D Matrix II | Reverse Linked List | |
Longest Consecutive Sequence | Find K Closest Elements | Reverse Linked List II | |
Count of Range Sum | Rotate List | ||
Median of Two Sorted Arrays | Swap Nodes in Pairs | ||
Odd Even Linked List | |||
Reverse Nodes in k-Group |
Heap / PriorityQueue | Sliding Window | Two Pointers |
---|---|---|
Task Scheduler | Minimum Size Subarray Sum | Merge Two Sorted Lists |
Minimum Number of Arrows to Burst Balloons | Fruit Into Baskets | Two Sum |
Reorganize String | Permutation in String | Squares of a Sorted Array |
Employee Free Time | Longest Repeating Character Replacement | Backspace String Compare |
Rearrange String k Distance Apart | Longest Substring Without Repeating Characters | Find the Duplicate Number |
Course Schedule III | Sliding Window Maximum | 3 Sum |
Kth Smallest Element in a Sorted Matrix | Minimum Number of K Consecutive Bit Flips | 3 Sum Closest |
Find K Pairs with Smallest Sums | Count Unique Characters of All Substrings of a Given String | Subarrays with Product Less than K |
Meeting Rooms II | Minimum Window Substring | Sort Colours |
Task Scheduler | Substring with Concatenation of All Words | Container With Most Water |
K Closest Points to Origin | Trapping Rain Water | |
Top K Frequent Elements | ||
Sort Characters By Frequency | ||
Kth Largest Element in an Array | ||
Reorganize String | ||
Merge k Sorted Lists | ||
Smallest Range Covering Elements from K Lists | ||
Employee Free Time | ||
Rearrange String k Distance Apart | ||
Course Schedule III | ||
Maximum Frequency Stack | ||
Find Median from Data Stream | ||
Sliding Window Median | ||
Meeting Rooms | ||
Merge Intervals | ||
Interval List Intersections | ||
Non-overlapping Intervals | ||
Meeting Rooms II | ||
Insert Interval |
- Contains Duplicate
- Missing Number
- Find All Numbers Disappeared in an Array
- Single Number
- Product of Array Except Self
- Find the Duplicate Number
- Find All Duplicates in an Array
- Set Matrix Zeroes
- Spiral Matrix
- Rotate Image
- First Missing Positive
- Longest Consecutive Sequence
- Binary Search
- Find Smallest Letter Greater Than Target
- Peak Index in a Mountain Array
- Find the Duplicate Number
- Kth Smallest Element in a Sorted Matrix
- Find Minimum in Rotated Sorted Array
- Find Peak Element
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array II
- Search a 2D Matrix
- Search a 2D Matrix II
- Find K Closest Elements
- Count of Range Sum
- Median of Two Sorted Arrays
- Word Search
- Letter Case Permutation
- Subsets
- Subsets II
- Permutations
- Find Smallest Letter Greater Than Target
- Peak Index in a Mountain Array
- Find the Duplicate Number
- Kth Smallest Element in a Sorted Matrix
- Find Minimum in Rotated Sorted Array
- Find Peak Element
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array II
- Search a 2D Matrix
- Search a 2D Matrix II
- Find K Closest Elements
- Count of Range Sum
- Median of Two Sorted Arrays
- Permutations II
- Combinations
- Combination Sum
- Combination Sum II
- Combination Sum III
- Generate Parentheses
- Palindrome Partitioning
- Letter Combinations of a Phone Number
- Generalized Abbreviation
- Sudoku Solver
- N-Queens
- Binary Tree Level Order Traversal II
- Average of Levels in Binary Tree
- Minimum Depth of Binary Tree
- Same Tree
- Path Sum
- Clone Graph
- Merge Two Binary Trees
- Pacific Atlantic Water Flow
- Number of Islands
- Graph Valid Tree
- Maximum Depth of Binary Tree
- Lowest Common Ancestor of a Binary Search Tree
- Subtree of Another Tree
- Invert Binary Tree
- Target Sum
- Clone Graph
- Pacific Atlantic Water Flow
- Number of Islands
- Number of Connected Components in an Undirected Graph
- Kth Smallest Element in a BST
- Binary Tree Right Side View
- All Nodes Distance K in Binary Tree
- Path Sum II
- Path Sum III
- Lowest Common Ancestor of a Binary Tree
- Number of Connected Components in an Undirected Graph
- Course Schedule
- Course Schedule II
- Minimum Height Trees
- Binary Tree Level Order Traversal
- Binary Tree Zigzag Level Order Traversal
- Validate Binary Search Tree
- Maximum Width of Binary Tree
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node II
- Binary Tree Right Side View
- Binary Tree Maximum Path Sum
- Construct Binary Tree from Preorder and Inorder Traversal
- All Nodes Distance K in Binary Tree
- Word Search II
- Sort Items by Groups Respecting Dependencies
- Longest Word in Dictionary
- Index Pairs of a String
- Implement Trie (Prefix Tree)
- Maximum XOR of Two Numbers in an Array
- Word Search II
- Concatenated Words
- Prefix and Suffix Search
- Palindrome Pairs
- Design Search Autocomplete System
- Word Squares
TreeMap and Interval Tree
- Base edge condition like array size 0 etc
- Variable naming, errors in the code and not to focus solely on the algorithm itself.
- clarified requirements about potential inputs, nulls expected, single threaded vs multi threaded etc
- checked if boundary conditions were handled correctly
- suggested better naming of variables
- suggested better data structures to store data // Set instead of a List
- added Nullable and Nonnull annotations
- code reusability
- wrote down time and space complexity
- test cases. I shared a few edge cases that we should look for.
- meaningful variable, method names, modular code. no method is too complex, code reusability. Also make sure to ask for test cases to cover most of the code path.
- readability of code, proper unit tests, code structure, short functions (max 1 screen size for one function), efficiency