LearnThatStack Ace your next interview
Sorting, Searching & Recursion · question
Question 8 of 61

What is a base case, and what happens when a recursion lacks one?

beginner
← All Sorting, Searching & Recursion questions
Re-explain

A base case is the stopping condition that returns an answer directly, without another recursive call. It is the point where the problem is small enough to solve outright, like an empty list or zero.

Without one, the function keeps calling itself forever, because the problem never shrinks to something answerable. Each call reserves a frame on the call stack for its local state. The frames pile up until the stack runs out of room, and the program crashes with a stack overflow.

A wrong base case fails the same way. If your condition never matches the values you reach, the recursion skips the exit and never stops. Getting the base case exact is the first thing to check.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Sorting, Searching & Recursion One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Can a recursion with a correct base case still overflow the stack? Yes, when the depth is large. Tie depth to input size, like recursing down a long linked list, and note that a base case does not limit depth.
  • Your function has a base case but never stops on some inputs. How do you find out why? Check that every call shrinks the input toward the base case on every branch. Watch for steps that jump past it, and for negative or empty inputs.
  • Why do some recursive functions need more than one base case? Link base cases to how the input shrinks. Fibonacci needs both 0 and 1 because the n - 2 step can skip over a single base case.
  • If a valid input is too deep for the stack, how would you fix the function? Rewrite it as a loop with an explicit stack, or use tail recursion where the language removes tail calls. Raising the stack limit only moves the cliff.

What you can say

  1. A base case is the stopping condition, the point where the input is small enough, like an empty list or zero, that the function returns an answer without calling itself again.
  2. Every other call works on a smaller piece of the problem, so the base case is where the chain of calls finally ends.
  3. Without one, the function keeps calling itself forever, because the problem never shrinks to something it can answer.
  4. Each call reserves a frame on the call stack for its local state, so the frames pile up until the stack runs out of room and the program crashes with a stack overflow.
  5. A wrong base case fails the same way, because if the condition never matches the values you actually reach, the recursion skips right past the exit.
  6. So when a recursion misbehaves, the base case is the first thing I check, against every value the calls can really reach.

Weak answers to avoid

  • Says it just loops forever In most languages each call reserves a stack frame, so it does not spin harmlessly. Say the frames pile up until the stack overflows and the program crashes.
  • Defines the base case, skips the failure The question has two halves. Explain the failure too. The calls never end, each one takes a stack frame, and the stack overflows.
  • Thinks any base case is enough A base case the recursion never reaches fails the same way. Say you check that every path hits it, like an n - 2 step that skips past n == 0.
  • Gives no concrete base case A vague 'when it is done' sounds memorized. Name a real one, like returning 0 for the sum of an empty list or 1 for factorial of 0.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Sorting, Searching & Recursion cheatsheet.

← Back to all Sorting, Searching & Recursion questions
Pro · $10/mo

51 of 61 Sorting, Searching & Recursion answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime