Default arguments in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using default arguments in functions, it's important to see how they affect the work done each time the function runs.
We want to know if having default values changes how long the function takes as input grows.
Analyze the time complexity of the following code snippet.
def greet(name, times=1):
for _ in range(times):
print(f"Hello, {name}!")
# Calling the function
# greet("Alice") # uses default times=1
# greet("Bob", 3) # times=3
This function prints a greeting message a number of times, using a default value if no number is given.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints the greeting.
- How many times: It runs exactly
timestimes, which can be the default or a given number.
Each time the function runs, the number of print operations grows with the times value.
| Input Size (times) | Approx. Operations |
|---|---|
| 1 | 1 print |
| 10 | 10 prints |
| 100 | 100 prints |
Pattern observation: The work grows directly with the number of times the loop runs.
Time Complexity: O(times)
This means the time taken grows in a straight line with how many times we want to print the greeting.
[X] Wrong: "Using default arguments makes the function run faster or slower depending on the default value."
[OK] Correct: The default value just sets a starting number; the function's time depends on the actual number used when called, not on whether it was default or given.
Understanding how default arguments affect loops helps you explain how your code behaves with different inputs, a skill that shows clear thinking in interviews.
"What if the function called another function inside the loop? How would that change the time complexity?"
Practice
Solution
Step 1: Understand default arguments
Default arguments allow a function to have parameters with preset values if the caller does not provide them.Step 2: Identify the purpose
This makes the function easier to call without needing all arguments every time.Final Answer:
To provide preset values for parameters if no argument is given -> Option BQuick Check:
Default arguments = preset values [OK]
- Thinking default arguments speed up code
- Believing default arguments force all inputs
- Confusing default arguments with variable creation
Solution
Step 1: Check default argument placement
Parameters with default values must come after parameters without defaults.Step 2: Analyze each option
def greet(name='Friend', age): places a default before a non-default parameter, which is invalid syntax. Options C and D have syntax errors. def greet(name, age=30): correctly places the default argument after a required parameter.Final Answer:
def greet(name, age=30): -> Option CQuick Check:
Defaults after required params = correct syntax [OK]
- Placing default arguments before required ones
- Leaving default values empty
- Using invalid syntax for defaults
def multiply(a, b=2):
return a * b
print(multiply(4))
print(multiply(4, 3))Solution
Step 1: Understand default argument usage
When multiply(4) is called, b uses its default value 2, so 4 * 2 = 8.Step 2: Evaluate second call
When multiply(4, 3) is called, b is 3, so 4 * 3 = 12.Final Answer:
8 and 12 -> Option DQuick Check:
Default used first call, explicit second call = 8 and 12 [OK]
- Assuming default is ignored when argument is given
- Confusing order of arguments
- Expecting error when default is used
def add_numbers(x=5, y):
return x + ySolution
Step 1: Check parameter order
Parameters with default values must come after parameters without defaults in Python.Step 2: Identify error
Here, x has a default but y does not, so this causes a SyntaxError.Final Answer:
Default argument before non-default argument causes SyntaxError -> Option AQuick Check:
Default before required param = SyntaxError [OK]
- Ignoring parameter order rules
- Thinking missing return causes error here
- Assuming function name is invalid
greet('Alice', 'Hi') and "Hello, Bob!" when called as greet('Bob')?Solution
Step 1: Check parameter order and defaults
Parameters with defaults must come after those without. def greet(name, greeting='Hello'): print(f"{greeting}, {name}!") has name first (no default), greeting second (default 'Hello').Step 2: Verify behavior
Calling greet('Alice', 'Hi') prints "Hi, Alice!". Calling greet('Bob') uses default greeting "Hello" and prints "Hello, Bob!".Final Answer:
def greet(name, greeting='Hello'): print(f"{greeting}, {name}!") -> Option AQuick Check:
Defaults after required params, works as expected [OK]
- Placing default param before required param
- Not providing default for optional greeting
- Using default for name causing unexpected calls
