Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: A grid of R × C = 10³ × 10³ heights; find the minimum possible 'maximum step height' along any path from top-left to bottom-right. Which approaches are correct and efficient?
- A. Dijkstra where a path's cost is its maximum edge (minimax), or binary search on the limit with BFS, or Kruskal-style union-find – each about O(RC log RC)
- B. Plain BFS for the fewest steps
- C. DP moving only right/down
- D. DFS returning the first path found
Correct answer: A. Dijkstra where a path's cost is its maximum edge (minimax), or binary search on the limit with BFS, or Kruskal-style union-find – each about O(RC log RC)
The bottleneck path minimises the largest edge; Dijkstra works with max instead of sum, binary search over the threshold with a BFS feasibility check also works, and so does adding edges in increasing order until the corners connect. DP restricted to right/down misses paths that go up or left.
More Coding Interview Problems questions
- Problem: Count distinct values in every window of size k over n = 10⁶ values. What is the optimal complexity?
- Problem: n = 10⁵ cities connected by roads that are added one at a time; after each addition report the number of connected components…
- Problem: Find the longest substring that appears at least twice in a string of length 10⁵. Which approach is efficient?
- Problem: A recursive solution for 'number of ways to partition n into parts' is memoised on (n, largest part allowed). With n ≤ 1000, how…
- Problem: A function has T(n) = T(n − 1) + T(n − 2) + O(1) without memoisation. What is its growth?
- Problem: Dijkstra is run with a binary heap but WITHOUT the 'skip stale entry' check (if d > dist[u]: continue). What happens?