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
- 1Contains Duplicate | Solution
- 2Missing Number | Solution
- 3Find All Numbers Disappeared in an Array | Solution *
- 4Single Number | Solution *
- 5Climbing Stairs | Solution
- 6Best Time to Buy and Sell Stock | Solution
- 7Maximum Subarray subarray with the largest sum | Solution
- 8Range Sum Query - Immutable | Solution
- 9Linked List Cycle | Solution | Solution
- 10Middle of the Linked List | Solution
- 11Palindrome Linked List | Solution *
- 12Remove Linked List Elements | Solution
- 13Remove Duplicates from Sorted List | Solution
- 14Reverse Linked List | Solution
- 15Merge Two Sorted Lists | Solution
- 16Meeting Rooms | Solution
- 17Binary Search | Solution
- 18Find Smallest Letter Greater Than Target | Solution *
- 19Peak Index in a Mountain Array | Solution
- 20Binary Tree Level Order Traversal II | Solution
- 21Average of Levels in Binary Tree | Solution
- 22Minimum Depth of Binary Tree | Solution
- 23Same Tree | Solution
- 24Path Sum | Solution
- 25Diameter of Binary Tree | Solution
- 26Merge Two Binary Trees | Solution *
- 27Maximum Depth of Binary Tree | Solution
- 28Lowest Common Ancestor of a Binary Search Tree | Solution
- 29Subtree of Another Tree | Solution *
- 30Invert Binary Tree | Solution
- 31Two Sum | Solution
- 32Squares of a Sorted Array | Solution
- 33Backspace String Compare | Solution *
- 34Longest Word in Dictionary | Solution (Trie)
- 35Index Pairs of a String | Solution
- 36Majority Element | Solution
- 37Product of Array Except Self | Solution
- 38Find the Duplicate Number | Solution
- 39Find All Duplicates in an Array | Solution
- 40Set Matrix Zeroes | Solution
- 41Spiral Matrix | Solution
- 42Rotate Image | Solution
- 43Word Search | Solution
- 44Letter Case Permutation | Solution
- 45Subsets | Solution
- 46Subsets II | Solution
- 47Permutations | Solution
- 48Permutations II | Solution
- 49Combinations | Solution
- 50Combination Sum | Solution
- 51Combination Sum II | Solution
- 52Combination Sum III | Solution
- 53Generate Parentheses | Solution
- 54Target Sum | Solution
- 55Palindrome Partitioning | Solution
- 56Letter Combinations of a Phone Number | Solution
- 57Generalized Abbreviation | Solution
- 58House Robber | Solution
- 59House Robber II | Solution
- 60Coin Change | Solution
- 61Maximum Product Subarray | Solution
- 62Longest Increasing Subsequence | Solution
- 63Longest Palindromic Substring | Solution
- 64Word Break | Solution
- 65Combination Sum IV | Solution
- 66Decode Ways | Solution
- 67Unique Paths | Solution
- 68Jump Game | Solution
- 69Palindromic Substrings | Solution
- 70Number of Longest Increasing Subsequence | Solution
- 71Partition Equal Subset Sum | Solution
- 72Partition to K Equal Sum Subsets | Solution
- 73Best Time to Buy and Sell Stock with Cooldown | Solution
- 74Counting Bits | Solution
- 75Linked List Cycle II | Solution
- 76Add Two Numbers | Solution
- 77Remove Nth Node From End Of List | Solution
- 78Sort List | Solution
- 79Reorder List | Solution
- 80Clone Graph | Solution
- 81Pacific Atlantic Water Flow | Solution
- 82Number of Islands | Solution
- 83Graph Valid Tree | Solution
- 84Number of Connected Components in an Undirected Graph | Solution
- 85Reverse Linked List II | Solution
- 86Rotate List | Solution
- 87Swap Nodes in Pairs | Solution
- 88Odd Even Linked List | Solution
- 89Kth Smallest Element in a Sorted Matrix | Solution
- 90Find K Pairs with Smallest Sums | Solution
- 91Merge Intervals | Solution
- 92Interval List Intersections | Solution
- 93Non-overlapping Intervals | Solution
- 94Meeting Rooms II | Solution
- 95Task Scheduler | Solution
- 96Minimum Number of Arrows to Burst Balloons | Solution
- 97Find Minimum in Rotated Sorted Array | Solution
- 98Find Peak Element | Solution
- 99Search in Rotated Sorted Array | Solution
- 100Search in Rotated Sorted Array II | Solution
- 101Search a 2D Matrix | Solution
- 102Search a 2D Matrix II | Solution
- 103Find K Closest Elements | Solution
- 104Minimum Size Subarray Sum | Solution(Sliding Window)
- 105Fruit Into Baskets | Solution(Sliding Window)
- 106Permutation in String / contains a permutation substring | Solution(Sliding Window)
- 107Longest Repeating Character Replacement | Solution (Sliding Window)
- 108Kth Smallest Element in a BST | Solution
- 109K Closest Points to Origin | Solution
- 110Top K Frequent Elements | Solution
- 111Sort Characters By Frequency | Solution
- 112Kth Largest Element in an Array | Solution
- 113Reorganize String : adjacent characters are not the same | Solution
- 114Course Schedule | Solution
- 115Course Schedule II | Solution
- 116Minimum Height Trees: root is the node with most child | Solution
- 117Binary Tree Level Order Traversal | Solution
- 118Binary Tree Zigzag Level Order Traversal | Solution
- 119Populating Next Right Pointers in Each Node | Solution
- 120Populating Next Right Pointers in Each Node II | Solution
- 121Binary Tree Right Side View | Solution
- 122All Nodes Distance K in Binary Tree | Solution
- 123Path Sum II | Solution
- 124Path Sum III | Solution
- 125(LCA) Lowest Common Ancestor of a Binary Tree | Solution
- 126Maximum Binary Tree | Solution
- 127Maximum Width of Binary Tree | Solution
- 128Construct Binary Tree from Preorder and Inorder Traversal | Solution
- 129Validate Binary Search Tree | Solution
- 130Implement Trie (Prefix Tree) | Solution
- 1313 Sum | Solution
- 1323 Sum Closest | Solution
- 133Subarrays with Product Less than K | Solution
- 134Sort Colours | Solution
- 135Maximum XOR of Two Numbers in an Array | Solution
- 136First Missing Positive
- 137Longest Consecutive Sequence
- 138Sudoku Solver
- 139N-Queens
- 140Reverse Nodes in k-Group
- 141Merge k Sorted Lists
- 142Smallest Range Covering Elements from K Lists
- 143Insert Interval
- 144Employee Free Time | Solution
- 145Count of Range Sum
- 146Sliding Window Maximum
- 147Longest Substring Without Repeating Characters |
- 148Minimum Number of K Consecutive Bit Flips
- 149Count Unique Characters of All Substrings of a Given String
- 150Minimum Window Substring
- 151Substring with Concatenation of All Words | Solution
- 152Rearrange String k Distance Apart | Solution
- 153Course Schedule III
- 154Maximum Frequency Stack
- 155Alien Dictionary | Solution
- 156Sequence Reconstruction | Solution
- 157Binary Tree Maximum Path Sum
- 158Serialize and Deserialize Binary Tree | Solution
- 159Word Search II
- 160Find Median from Data Stream | Solution
- 161Sliding Window Median
- 162Trapping Rain Water |Solution
- 163Container With Most Water|Solution
- 164Concatenated Words| Solution
- 165Prefix and Suffix Search | Solution
- 166Palindrome Pairs
- 167Design Search Autocomplete System | Solution
- 168Word Squares | Solution
- 169Sort Items by Groups Respecting Dependencies |Solution
- 170Median 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