max() and min() in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use max() or min() on a list, we want to find the biggest or smallest item quickly.
We ask: How does the time to find these values grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
numbers = [3, 7, 2, 9, 4]
maximum = max(numbers)
minimum = min(numbers)
print(f"Max: {maximum}, Min: {minimum}")
This code finds the largest and smallest numbers in a list.
- Primary operation: Checking each item in the list once to compare values.
- How many times: Each element is checked exactly one time for max and one time for min.
As the list gets bigger, the number of checks grows in a straight line with the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 checks (10 for max, 10 for min) |
| 100 | About 200 checks |
| 1000 | About 2000 checks |
Pattern observation: The work doubles when the list size doubles because max and min each look at every item once.
Time Complexity: O(n)
This means the time to find max and min grows directly with the size of the list.
[X] Wrong: "max() and min() find the answer instantly, no matter the list size."
[OK] Correct: They must look at every item to be sure which is biggest or smallest, so time grows with list size.
Understanding how max() and min() work helps you explain how searching through data scales, a key skill in many coding tasks.
"What if we used max() and min() on a sorted list? How would the time complexity change?"
Practice
max() function do in Python?Solution
Step 1: Understand the purpose of max()
Themax()function is used to find the biggest or largest item from a list or multiple values.Step 2: Compare with other options
Options A, C, and D describe different functions or actions, not whatmax()does.Final Answer:
Finds the largest item in a list or among values -> Option AQuick Check:
max() = largest item [OK]
- Confusing max() with min()
- Thinking max() sorts the list
- Assuming max() removes duplicates
nums?Solution
Step 1: Recall the syntax of min()
The correct syntax to find the smallest value in a list ismin(list_name). So,min(nums)is correct.Step 2: Check other options for errors
minimum(nums) uses a non-existent functionminimum(). nums.min() tries to callmin()as a method on the list, which is invalid. min = nums assignsminto the list, which is incorrect usage.Final Answer:
min(nums) -> Option BQuick Check:
min(list) = smallest value [OK]
- Using min as a method like nums.min()
- Trying to assign min to a variable incorrectly
- Using a wrong function name like minimum()
numbers = [3, 7, 2, 9, 5] print(max(numbers)) print(min(numbers))
Solution
Step 1: Identify the list values
The listnumberscontains [3, 7, 2, 9, 5].Step 2: Find max and min values
The largest value is 9 and the smallest value is 2.Final Answer:
9 and 2 -> Option AQuick Check:
max = 9, min = 2 [OK]
- Mixing up max and min results
- Assuming print shows the whole list
- Syntax errors from missing parentheses
values = [10, 20, 30] print(max(values, key=abs))
Solution
Step 1: Understand max() with key parameter
Thekeyparameter lets max() compare items by a function result. Here,absreturns absolute values, which are the same as the original values since all are positive.Step 2: Check if code runs correctly
Sinceabsis a built-in function and the list is valid, the code runs without error and returns the largest value, 30.Final Answer:
The code runs without error and prints 30 -> Option CQuick Check:
max(values, key=abs) = 30 [OK]
- Thinking abs() is undefined
- Believing max() can't use key parameter
- Assuming code causes error
words = ['apple', 'banana', 'grape', 'kiwi'], which code finds the word with the shortest length?Solution
Step 1: Understand the key parameter with len()
Usingkey=lentellsmin()to compare words by their length, not alphabetically.Step 2: Identify shortest word
Among the words, 'kiwi' has the shortest length (4 letters). Somin(words, key=len)returns 'kiwi'.Final Answer:
min(words, key=len) -> Option DQuick Check:
min by length = 'kiwi' [OK]
- Using max() instead of min() for shortest
- Not using key=len and getting alphabetical result
- Assuming min(words) finds shortest word
