What decides whether a piece of data lives on the stack or the heap, and why does it matter for performance?
technical-conceptual · Mid level · software-engineering
What the interviewer is really asking
Tests whether the candidate understands the stack/heap distinction in terms of lifetime and allocation mechanics — not just 'stack is fast' — and can connect it to real failure modes like stack overflow and heap fragmentation.
What to say
- Tie placement to lifetime and size: data whose lifetime is bounded by the current function call and whose size is known goes on the stack; data that must outlive the call or whose size is dynamic goes on the heap.
- Explain the cost difference: a stack allocation is just moving the stack pointer, while a heap allocation searches for a free block, so the heap is slower and adds bookkeeping.
- Name the failure modes: deep or unbounded recursion blows the fixed-size stack (stack overflow), while long-running heap churn causes fragmentation and pressure on the allocator or garbage collector.
What to avoid
- Don't reduce it to 'stack is fast, heap is slow' with no mention of lifetime or size constraints.
- Don't claim the stack is unlimited; it's a bounded region, which is exactly why runaway recursion crashes.
- Don't ignore language nuance — in managed languages the heap is GC-managed, and escape analysis can keep some objects on the stack.
Example answers
Strong: Placement follows lifetime and size. A local with a known size that dies at function return goes on the stack — allocation is just bumping the stack pointer, so it's near-free and freed automatically. Anything that must outlive the call or whose size is only known at runtime goes on the heap, which costs an allocator lookup and explicit or GC-driven cleanup. The stack is bounded, so deep recursion overflows it; the heap fragments under long-running churn. In a managed runtime, escape analysis can even promote a heap object to the stack.
Weak: The stack is for small fast stuff and the heap is for big objects. The stack is faster, so you want to keep things on the stack when you can, and the heap is where dynamic memory goes.