Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 4

Find Maximum Value

Easy

6 minute session

Summary

Find the largest number in a list of integers. Returns None for an empty list.

Problem statement

Write a function find_max(nums) that takes a list of integers nums and returns the maximum value in the list. If nums is empty, return None. Do not use the built-in max() function; iterate through the list and track the largest value manually.

Task

Practice iterating through a list to identify the maximum value and handle empty inputs.

Examples

Basic example

Input

find_max([3, 1, 4, 2])

Output

4

Explanation

Among the numbers 3, 1, 4, 2 the largest is 4.

Input format

A single list of integers, e.g. [1, 2, 3].

Output format

An integer (the maximum) or None for an empty list.

Constraints

You must not use the built-in max() or sorted(). Aim for O(n) time and O(1) extra space.

Samples

Sample input 0

find_max([-5, -2, -3])

Sample output 0

-2

Explanation 0

The maximum among the negatives is -2.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

02:26 AM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.