The Peak Finder
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Athenahealth
Problem
Given a list of integers, return the maximum value without using max(). Return None if empty.
Largest number in the list. Max() is not an option.
Example 1
Input:
nums = [3,7,2,9,1]
Output:
9
Example 2
Input:
nums = [-5,-1,-8]
Output:
-1
Worked solution and explanation
Why this problem exists in real interviews
This tests the most basic linear scan accumulation pattern. By prohibiting max(), interviewers check whether candidates understand how maximum-finding actually works under the hood.
Break down the requirements
Step 1: Initialize with the first element
Set the current maximum to nums[0]. This handles the case where all numbers are negative.
Step 2: Scan the rest of the list
Compare each element to the current maximum. Update if the element is larger.
Step 3: Return the maximum
After scanning all elements, the tracked value is the largest.
The solution
def find_max(nums):
if not nums:
return None
largest = nums[0]
for i in range(1, len(nums)):
if nums[i] > largest:
largest = nums[i]
return largestCommon follow-up questions
- How would you also return the index of the maximum? (Tests tracking the index alongside the value.)
- What if you needed both the max and min in a single pass? (Tests tracking two variables simultaneously.)
- Can you find the max with fewer than n-1 comparisons? (Tests tournament-style comparison or proving the lower bound of n-1.)