Interview questions
How does recursion work under the hood?
How I'd answer: "Each method call, recursive or not, pushes a stack frame containing its parameters, local variables and return address onto the thread's call stack. A recursive method calls itself with a smaller input, so frames accumulate — each with its own copy of the variables — until a base case returns without recursing. Then the frames unwind in reverse order: each paused call resumes with the returned value, finishes its work and returns to its caller. So the time cost is the number of calls times the work per call, and the space cost is the maximum depth times the frame size. Java doesn't do tail-call optimisation, and the default thread stack allows only tens of thousands of frames, so very deep linear recursion causes a StackOverflowError."
Combines: Ch 2, 5, 8.
Generate all subsets of a set." (LeetCode 78 — a classic first backtracking question)
How I'd answer: "At each index I have two choices: exclude the element or include it. I recurse with an index and a current path: if the index reaches the end, I add a copy of the path to the results. Otherwise I recurse without the element, then append it, recurse again, and remove it — choose, explore, unchoose — so the path is restored for the caller. There are 2ⁿ subsets and copying each costs up to n, so it's O(n · 2ⁿ) time; the recursion depth is n, so O(n) extra space besides the output. The key details are copying the path when recording it, since the path object is shared, and undoing the append."
Combines: Ch 6, 9.
What's the difference between recursion and iteration? When would you use each?
How I'd answer:
"Anything recursive can be written iteratively, using an explicit stack if needed. Recursion is clearest when the problem or data is self-similar — trees, nested structures,
divide and conquer, backtracking — because the code mirrors the structure. Its costs are a stack frame per call and a depth limit. Iteration has no depth limit and less
overhead, and is clearer for flat, linear processes. In interviews I'll write a recursive tree traversal or backtracking solution directly, count its depth in the space
complexity, and if the input could make the recursion very deep — like DFS on a path-shaped graph with 10⁵ nodes — I'd convert it to an explicit ArrayDeque stack."
Combines: Ch 8.
Climbing stairs: you can take 1 or 2 steps. How many ways to reach step n? Optimise it.
How I'd answer: "The last move to step n came from n−1 or n−2, so ways(n) = ways(n−1) + ways(n−2), with ways(0) = ways(1) = 1 — the Fibonacci recurrence. Written directly it's exponential, because the same steps are recomputed; for n = 40 that's hundreds of millions of calls. Memoizing each ways(i) in an array makes it O(n) time and O(n) space — about 80 calls for n = 40. Since each value only needs the previous two, I can go bottom-up with two variables for O(n) time and O(1) space. That progression — recurrence, memoize, bottom-up, optimise space — is the standard dynamic programming recipe."
Combines: Ch 3, 7.
Why might this recursive solution get Time Limit Exceeded, and how would you check?
How I'd answer: "Three common reasons. One: overlapping subproblems without memoization — the recursion tree is exponential; I'd check whether the same arguments repeat and cache them. Two: hidden work per call, like building substrings or copying arrays at each level, which turns O(n) into O(n²); I'd pass indexes instead. Three: the branching factor is simply too high for the constraints — for example 2ⁿ with n = 40 — which means the approach needs to change, perhaps to DP or pruning. To check, I'd reason about the recursion tree: calls equals branching factor to the power of depth, and compare that with about 10⁸ operations per second."
Combines: Ch 3, 5, 7; Big-O and Complexity Ch 8, 10.