Trace recursive calls as frames on a stack.
Each open call waits for one smaller call, and the base case returns at once, so answers are built as frames return, newest first.
Use it when a question about an input can be answered from the same question about a smaller input. Signal words: the same question on a smaller input, a function that calls itself, a base case.
Base case: the smallest input, answered at once. Write it first.
Recursive case: call the same function on a smaller input, and trust that it returns the right answer. Combine that answer with this call's own part.
Progress: every call must move toward the base case.
Cost: one frame for each open call. The time is the number of calls, and the extra space is the recursion depth.
def chain_length(replies, start):
# [1] Base case: a comment with no reply ends the chain, so it counts for 1.
if replies[start] == -1:
return 1
# [2] Recursive case: ask the same question about the reply, which is one comment closer to the end.
below = chain_length(replies, replies[start])
# [3] Return: this comment plus everything below it.
return 1 + belowpublic int chainLength(int[] replies, int start) {
// [1] Base case: a comment with no reply ends the chain, so it counts for 1.
if (replies[start] == -1) {
return 1;
}
// [2] Recursive case: ask the same question about the reply, which is one comment closer to the end.
int below = chainLength(replies, replies[start]);
// [3] Return: this comment plus everything below it.
return 1 + below;
}int chainLength(vector<int>& replies, int start) {
// [1] Base case: a comment with no reply ends the chain, so it counts for 1.
if (replies[start] == -1) {
return 1;
}
// [2] Recursive case: ask the same question about the reply, which is one comment closer to the end.
int below = chainLength(replies, replies[start]);
// [3] Return: this comment plus everything below it.
return 1 + below;
}function chainLength(replies, start) {
// [1] Base case: a comment with no reply ends the chain, so it counts for 1.
if (replies[start] === -1) {
return 1;
}
// [2] Recursive case: ask the same question about the reply, which is one comment closer to the end.
const below = chainLength(replies, replies[start]);
// [3] Return: this comment plus everything below it.
return 1 + below;
}def chain_length_loop(replies, start):
# [1] The comment being looked at, and the count so far: the start comment counts for 1.
current = start
total = 1
# [2] While the comment has a reply, move to it. This is the recursive case, with no new frame.
while replies[current] != -1:
current = replies[current]
# [3] Each reply adds one comment to the count.
total += 1
# [4] No reply is left: this is the base case, and the count is the length of the chain.
return totalpublic int chainLengthLoop(int[] replies, int start) {
// [1] The comment being looked at, and the count so far: the start comment counts for 1.
int current = start;
int total = 1;
// [2] While the comment has a reply, move to it. This is the recursive case, with no new frame.
while (replies[current] != -1) {
current = replies[current];
// [3] Each reply adds one comment to the count.
total++;
}
// [4] No reply is left: this is the base case, and the count is the length of the chain.
return total;
}int chainLengthLoop(vector<int>& replies, int start) {
// [1] The comment being looked at, and the count so far: the start comment counts for 1.
int current = start;
int total = 1;
// [2] While the comment has a reply, move to it. This is the recursive case, with no new frame.
while (replies[current] != -1) {
current = replies[current];
// [3] Each reply adds one comment to the count.
total++;
}
// [4] No reply is left: this is the base case, and the count is the length of the chain.
return total;
}function chainLengthLoop(replies, start) {
// [1] The comment being looked at, and the count so far: the start comment counts for 1.
let current = start;
let total = 1;
// [2] While the comment has a reply, move to it. This is the recursive case, with no new frame.
while (replies[current] !== -1) {
current = replies[current];
// [3] Each reply adds one comment to the count.
total++;
}
// [4] No reply is left: this is the base case, and the count is the length of the chain.
return total;
}This version trusts that the data always reaches a comment with no reply. The base case is correct, but the chain never reaches it.
This version makes the recursive call and stores the answer in below, but it never returns 1 + below.
This version returns 0 in the base case, to count the links between comments, and not the comments.
Your team lead asks for the length of a chain of 50,000 comments in Python. The recursion needs 50,000 frames, and the default limit is about 1,000, so it stops with a RecursionError.
systemdesignmastery.dev/dsa/cheatsheets/call-stack