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.
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.
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.
Every other call works on a smaller piece of the problem, so the base case is where the chain of calls finally ends.
Without one, the function keeps calling itself forever, because the problem never shrinks to something it can answer.
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.
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.
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 foreverIn 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 failureThe 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 enoughA 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 caseA 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.