Skip to main content

Interview Tips

Coding Interview Tips — How to Solve LeetCode Problems by Pattern

Based on problem constraints, use these heuristics to identify possible approaches when unsure.

Arrays & Strings

Condition Approach
Input is sorted Binary search or two pointers
Need O(1) lookup or existence check Hash table (set or map)
Problem requires in-place modification Two pointers or index swap
Common prefix or string matching Map counting or Trie
XOR, toggling, or bit flags Bit manipulation

Subarrays & Sequences

Condition Approach
Max or min subarray sum or length Dynamic programming or sliding window
Sliding window max or min Monotonic deque
Next greater or smaller element Monotonic stack
Range sum query Prefix sum, Binary Indexed Tree, or segment tree

Trees & Graphs

Condition Approach
Tree traversal or path problem DFS (recursion or stack) or BFS (queue)
Graph traversal or connected components DFS / BFS / Union-Find
2D grid connectivity or shortest path BFS or DFS or DP on grid
Grouping or merging disjoint sets Union-Find or DFS
Task ordering with dependencies Topological sort

Linked Lists & Stacks

Condition Approach
Cycle detection or finding midpoint in a linked list Fast and slow pointers
Need to simulate recursion iteratively Explicit stack

Sorting & Intervals

Condition Approach
Top K or least K elements Min/max heap, Quickselect, or bucket sort
Merging multiple sorted sequences Merge sort pattern or min-heap
Overlapping interval handling Sort by start time, then sweep line
Processing a continuous data stream Heap or custom data structure

Optimization

Condition Approach
All permutations, combinations, or subsets Backtracking
Count ways or divide problem optimally Greedy (if provable) or Dynamic Programming

General

Condition Approach
None of the above fit Hash map or set — O(1) time, O(n) space
Want to avoid extra space Sort the input first — O(n log n) time, O(1) space

Jump to section

Buy me a coffee