Find the largest element of a Python list with a single loop instead of max(), and handle an empty list. Your first O(n) scan, run live in the browser.
The problem
Return the largest number in nums without using max() or sorting. Return None for an empty list.
Examples
Example 1
Input
find_max([3, 9, 2, 7])
Expected output
9
Example 2
Input
find_max([-5, -2, -9])
Expected output
-2
+ 3 hidden tests on Submit.
Edge cases to ask about
- All negative
- Empty list
- Single element
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Track the best so far | O(n) | O(1) |
Walkthrough of the optimal approach (try it yourself first)
Start best at nums[0] and replace it whenever a bigger value appears. Starting at 0 is the classic bug: it returns 0 for an all-negative list.
You can't do better than O(n): an element you skip could be the maximum.
Complexity: O(n) time, O(1) space. Every element must be seen once — any unseen element could be the largest.
Reveal the reference solution
def find_max(nums): if not nums: return None best = nums[0] for x in nums[1:]: if x > best: best = x return best
Follow-ups interviewers ask
- Return the index of the largest instead.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Find the Largest Number Without max() in Python?
The optimal solution runs in O(n) time and O(1) auxiliary space. Every element must be seen once — any unseen element could be the largest.
What follow-up questions do interviewers ask about Find the Largest Number Without max()?
Return the index of the largest instead.
