The Forbidden Ceiling
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
- Raisin
Problem
Given a non-empty list of integers, return the maximum. You may not use max() or any sort/sorted helper. Runtime must be O(n).
Round up. But not the obvious way.
Example
Input:
nums = [3,1,4,1,5,9,2,6]
Output:
9
Worked solution and explanation
Why this problem exists in real interviews
This tests whether a candidate can implement a basic linear scan without relying on built-in functions. It probes understanding of comparison-based algorithms at the most fundamental level.
Break down the requirements
Step 1: Initialize with the first element
Use the first element as the initial maximum.
Step 2: Scan the rest of the list
Compare each element to the current maximum and update if larger.
The solution
def find_max(nums: list) -> int:
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 find both min and max in a single pass? (Tests tracking two variables simultaneously.)
- Can you find the max with fewer than n-1 comparisons? (Tests the tournament algorithm, which uses 3n/2 - 2 comparisons.)
- What if the list is extremely large and distributed? (Tests MapReduce pattern: find local maxima, then global max.)