Call stack behavior - Time & Space Complexity
When a program runs functions, it uses a call stack to keep track of where it is. Understanding how this stack grows helps us see how time and work increase.
We want to know how the number of function calls affects the total work done.
Analyze the time complexity of the following code snippet.
void countdown(int n) {
if (n == 0) return;
countdown(n - 1);
}
int main() {
countdown(5);
return 0;
}
This code calls a function that calls itself repeatedly, counting down from n to 0.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive function calls to countdown.
- How many times: The function calls itself n times before stopping.
Each time we increase n by 1, the function calls itself one more time, adding one more step.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls |
| 100 | 100 calls |
| 1000 | 1000 calls |
Pattern observation: The work grows directly with n, like climbing stairs one step at a time.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size n.
[X] Wrong: "Recursive calls happen all at once, so time stays the same no matter n."
[OK] Correct: Each call waits for the next one to finish, so calls add up one after another, increasing total time.
Understanding how the call stack grows with recursion helps you explain why some functions take longer as input grows. This skill shows you can think about program behavior clearly.
"What if the countdown function called itself twice each time? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of the call stack
The call stack stores information about active function calls and their local variables during program execution.Step 2: Differentiate from other memory areas
Global variables and dynamic memory are stored elsewhere, not on the call stack.Final Answer:
Keeps track of function calls and local variables -> Option DQuick Check:
Call stack = function calls + locals [OK]
- Confusing call stack with heap memory
- Thinking global variables are on the stack
- Assuming call stack manages I/O
Solution
Step 1: Identify correct C function syntax
In C, functions are declared with a return type, name, and parentheses, e.g.,void myFunction().Step 2: Recognize incorrect syntax
Keywords likefunction,def, orfuncare not valid in C.Final Answer:
void myFunction() { /* code */ } -> Option AQuick Check:
C function declaration uses return type + name [OK]
- Using Python or JavaScript keywords in C
- Omitting return type in function declaration
- Using incorrect braces or parentheses
void func(int n) {
if (n == 0) return;
printf("%d ", n);
func(n - 1);
}
int main() {
func(3);
return 0;
}Solution
Step 1: Trace the recursive calls
The function prints the current value of n, then calls itself with n-1 until n is 0.Step 2: Determine printed values
It prints 3, then 2, then 1, then stops before printing 0.Final Answer:
3 2 1 -> Option AQuick Check:
Print before recursion = descending order [OK]
- Assuming print happens after recursion
- Including zero in output incorrectly
- Confusing order of prints
void foo() {
int x = 5;
bar();
}
void bar() {
printf("%d", x);
}
int main() {
foo();
return 0;
}What is the error here?
Solution
Step 1: Check variable scope
Variable x is declared inside foo(), so it is local to foo() and not visible in bar().Step 2: Identify the error cause
bar() tries to print x, but x is undefined in bar(), causing a compile error.Final Answer:
Variable x is not declared in bar()'s scope -> Option CQuick Check:
Local variables are not shared across functions [OK]
- Thinking local variables are global
- Ignoring variable scope rules
- Assuming implicit variable sharing
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
int result = factorial(4);
printf("%d", result);
return 0;
}How many call stack frames are active at the deepest point during execution?
Solution
Step 1: Count recursive calls for factorial(4)
Calls happen for n=4, 3, 2, and 1 (base case triggers at n=1).Step 2: Determine deepest call stack frames
At n=1, the deepest call, there are 4 active frames: factorial(4), factorial(3), factorial(2), and factorial(1).Final Answer:
4 -> Option BQuick Check:
Number of calls = 4 frames [OK]
- Counting main() as a call stack frame for this question
- Off-by-one error in counting frames
- Assuming base case does not add a frame
Ready to prove it?
Pass the test and this pattern moves unknown
