Q1
Why does computing mid = (left + right) / 2 fail in binary search on huge arrays, and how do you fix it?
When left + right exceeds INT_MAX (~2.14 billion), signed 32-bit integer overflow wraps mid negative and causes out-of-bounds crashes. Always compute mid = left + (right - left) / 2.
Q2
Why can QuickSort degrade to O(n^2) on already-sorted arrays, and how does randomized/median-of-three pivot fix it?
Picking the first or last element on sorted/nearly-sorted inputs splits partitions 0 vs n-1 every recursion level. Random pivot selection or median-of-three guarantees expected O(n log n) performance.
Q3
When does Dijkstra's shortest-path algorithm fail silently—even when you don't have a negative cycle?
Dijkstra greedily finalizes the closest unvisited vertex assuming path weights never decrease later. Even ONE negative-weight edge breaks this greedy invariant; use Bellman-Ford when negative weights exist.
Q4
Why is BFS preferred over DFS for unweighted shortest paths in graphs and grids?
BFS explores vertices strictly in increasing order of edge distance (level 1, 2, 3...), so the first time a node is visited guarantees the minimum hop count—whereas DFS can wander down deep detours first.
Q5
How do Union-Find (Disjoint Set Union) optimizations reduce cycle-detection time to nearly O(1)?
Combining Path Compression (flattening parent pointers during find()) with Union by Rank/Size bounds amortized operation complexity to inverse Ackermann O(alpha(n)), virtually constant time.
Q6
Why does topological sorting require a DAG—and how does Kahn's algorithm detect cycles automatically?
Cycles create mutual circular dependencies with no valid starting task. Kahn's BFS tracks node in-degrees: if the output list contains fewer than V vertices once the zero-in-degree queue empties, a cycle exists.
Q7
In dynamic programming, when can you optimize a 2D dp[i][j] table down to 1D dp[j]—and which loop direction flips for 0/1 vs Unbounded Knapsack?
When dp[i][w] only depends on row i-1, collapse to 1D. Iterate capacity w backwards (W -> wt) for 0/1 Knapsack so each item is used at most once, and forwards (wt -> W) for Unbounded Knapsack to allow reuse.
Q8
Why can recursive DFS cause a StackOverflowError at depth 20,000 even when space complexity is 'only' O(V)?
Call stacks in JVM/OS threads default to ~1 MB (~5,000–10,000 frames). Deep skewed trees or long graph paths exhaust the stack limit; convert deep traversals to an explicit heap Stack/Deque.
Q9
How does a Monotonic Stack solve 'Next Greater Element' or 'Largest Rectangle in Histogram' in O(n) instead of O(n^2)?
Each element is pushed onto the stack once and popped at most once when a constraint breaks, guaranteeing linear 2n total operations instead of nested inner scans.
Q10
Why is a Trie faster than a HashSet for autocomplete prefix queries (startsWith), despite higher memory use?
HashSets hash full strings and must scan all entries for prefix matching (O(N * L)). A Trie navigates shared character nodes directly by prefix length O(P) regardless of how many million words are stored.