
2D Array vs 1D Array: When to Flatten a Matrix and When to Keep It 2D
2D array vs 1D array for grid problems: cache behavior, the r*cols+c index formula, when to flatten for Union-Find, and when to keep it 2D for DP.
Page 10 of 61 · Ideas for curious minds in tech.
Back to the archive
2D array vs 1D array for grid problems: cache behavior, the r*cols+c index formula, when to flatten for Union-Find, and when to keep it 2D for DP.

Eager evaluation computes immediately; lazy evaluation defers until needed. Learn the O(k) vs O(n) tradeoff, when each wins, and what interviewers test.

Heap vs quickselect for top-K elements: O(n log K) vs O(n), streams vs static arrays, and when each wins in coding interviews.
Greedy vs backtracking: one question decides it. If the locally optimal choice is always globally optimal, use greedy. Otherwise, backtrack and explore.

DP vs memoized recursion compared: same time complexity, different stack risk and space options. Learn exactly when each approach wins.

Linear probing vs separate chaining: understand clustering, tombstones, load factor limits, and cache behavior to pick the right collision strategy.

Hash table vs balanced BST: O(1) wins point lookups, but range queries, floor/ceiling, and sorted iteration need the BST. Full comparison with code.

Adjacency matrix vs edge list: one question about edge lookup vs iteration decides which graph representation you need in a coding interview.

Recursive vs iterative tree traversal: when depth is O(n), the call stack overflows. Learn the decision framework and how to fix it with an explicit stack.
Trie vs sorting for prefix search: O(m) queries vs O(m log n). When tries beat sorted arrays, when they don't, and LeetCode 1268 solved both ways.

Segment tree vs prefix sum: know which to reach for in your coding interview. Static arrays want O(1) queries; mutable arrays need O(log n).

Union-Find vs DFS for graph connectivity: when to pick each, the complexity tradeoff, and the interview signal your choice sends.

BFS vs Dijkstra: edge weights are the one decision point. Learn why BFS breaks on weighted graphs, when Dijkstra is required, and the 0-1 BFS trick.

Sliding window vs prefix sum: one question decides which to use. Negatives, counting subarrays, repeated queries. Here's when each pattern wins.

Binary search vs two pointers: one finds a single position, the other a pair relationship. The one question that decides which to use, with examples.

When to use a min-heap vs sorting for top-K elements. O(n log K) vs O(n log n) with Python examples, LeetCode walkthroughs, and streaming data.

Set vs map explained for coding interviews: one signal decides it every time. LeetCode examples, language reference, and when to use TreeSet vs TreeMap.

Learn when to use a HashMap vs array for counting in coding interviews. Array beats HashMap for bounded keys. Here's the one question that settles it.

Memoization vs caching: learn the key differences in scope, invalidation, and complexity impact, and how to decide which one your solution actually needs.
Topological sort vs DFS: one traverses, one orders. Kahn's algorithm, three-color cycle detection, LeetCode 210, and when to reach for each in interviews.

Master the DFS vs Dijkstra decision: what each algorithm does, when each breaks, and how to pick the right one in coding interviews.

Recursion vs dynamic programming: learn the two DP requirements, why subproblem overlap matters, and the 3-step decision framework for coding interviews.

Master binary search for coding interviews with 10 problems that build real intuition: boundary search, rotated arrays, answer-space search, and more.

15 LeetCode problems that teach complexity analysis from first principles. Each isolates one Big-O lesson: space-time tradeoffs, DP, amortized O(1), and more.