Function call flow in Rust - Time & Space Complexity
When functions call other functions, the total work depends on how many times each function runs.
We want to know how the total steps grow as the input gets bigger.
Analyze the time complexity of the following code snippet.
fn print_numbers(n: u32) {
for i in 0..n {
print_number(i);
}
}
fn print_number(x: u32) {
println!("Number: {}", x);
}
This code calls print_number inside a loop that runs n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop in
print_numbersthat callsprint_number. - How many times: The loop runs
ntimes, soprint_numberis calledntimes.
Each time n grows, the number of calls to print_number grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to print_number |
| 100 | 100 calls to print_number |
| 1000 | 1000 calls to print_number |
Pattern observation: The work grows directly with n. Double n, double the calls.
Time Complexity: O(n)
This means the total steps grow in a straight line with the input size.
[X] Wrong: "Because print_number is a separate function, it runs only once."
[OK] Correct: The function is called inside a loop, so it runs every time the loop runs, not just once.
Understanding how function calls add up helps you explain how your code behaves as inputs grow, a key skill in coding interviews.
What if print_number itself had a loop that ran m times? How would the time complexity change?
Practice
Solution
Step 1: Understand function call behavior
When a function is called, the program jumps to the function's code and executes it.Step 2: Return to the call point
After the function finishes, the program returns to the line after the call and continues running.Final Answer:
The program runs the function's code and then returns to the call point. -> Option AQuick Check:
Function call flow = run code then return [OK]
- Thinking function code is skipped
- Believing program stops after call
- Assuming function runs only if called twice
greet in Rust?Solution
Step 1: Recall Rust function call syntax
Functions are called by writing their name followed by parentheses, even if no arguments are passed.Step 2: Match the correct syntax
Onlygreet();matches this pattern correctly.Final Answer:
greet(); -> Option BQuick Check:
Function call syntax = name + () [OK]
- Omitting parentheses when calling functions
- Using brackets [] instead of parentheses
- Using keywords like 'call' or 'function' incorrectly
fn add_one(x: i32) -> i32 {
x + 1
}
fn main() {
let result = add_one(5);
println!("{}", result);
}Solution
Step 1: Understand the function behavior
The functionadd_onetakes an integer and returns that integer plus one.Step 2: Trace the function call and output
Callingadd_one(5)returns 6, which is printed byprintln!.Final Answer:
6 -> Option CQuick Check:
5 + 1 = 6 [OK]
- Confusing input and output values
- Thinking semicolon causes error here
- Assuming function prints instead of returns
fn multiply(x: i32, y: i32) -> i32 {
x * y
}
fn main() {
let result = multiply(4);
println!("{}", result);
}Solution
Step 1: Check function definition parameters
The functionmultiplyrequires two arguments:xandy.Step 2: Check function call arguments
The callmultiply(4)provides only one argument, causing an error.Final Answer:
multiply is called with wrong number of arguments. -> Option DQuick Check:
Function call args must match definition [OK]
- Ignoring argument count mismatch
- Thinking missing return causes error here
- Assuming println! usage is wrong
fn first() {
println!("Start");
second();
println!("End");
}
fn second() {
println!("Middle");
}
fn main() {
first();
}What is the order of printed lines when
main runs?Solution
Step 1: Trace the call flow from main
Main callsfirst(), which prints "Start" then callssecond().Step 2: Follow nested function calls
second()prints "Middle" and returns. Thenfirst()prints "End".Final Answer:
Start\nMiddle\nEnd -> Option AQuick Check:
Call flow order = Start, Middle, End [OK]
- Ignoring nested function call order
- Assuming prints happen after all calls
- Mixing up print sequence
Ready to prove it?
Pass the test and this pattern moves unknown
