An engineer wants to convert a deep recursive algorithm to an iterative one using an explicit stack because production traffic is hitting stack-overflow crashes. What does that conversion actually buy you, and where do people get it wrong?

technical-conceptual · Senior level · software-engineering

What the interviewer is really asking

Assesses understanding of how an explicit stack replaces the call stack to trade limited stack memory for heap memory, the subtlety of preserving execution state across the conversion, and when the conversion is the right fix versus addressing the depth itself.

What to say

What to avoid

Example answers

Strong: The win is memory location and graceful failure: the call stack is a small fixed region, so deep recursion blows it, whereas an explicit stack lives on the heap and can grow far larger — and I can check its size and return an error instead of crashing. The trap is state: a call frame holds locals, the child being processed, and where to resume, so for something like post-order traversal I have to push markers or a visited flag, not just nodes, or I'll double-process. Before doing the conversion I'd ask whether I can cut the depth itself — tail-call style or a genuinely iterative formulation — since the conversion relocates memory rather than removing it.

Weak: I'd swap the recursion for a while loop with a stack — push the nodes, pop them, process each one. That gets rid of the stack overflow because we're not recursing anymore.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.