Problem No 2
Find the maximum and minimum in an array
Easy≈ 7 minute session
Lesson guide
What this Python exercise practices
Find the maximum and minimum in an array is a beginner practice lesson that focuses on dsa, problem patterns, edge cases. It is designed to be solved in about 7 minutes with examples, starter code, and test feedback.
Prerequisites
- Python functions
- Loops
- Lists
- Basic edge cases
Difficulty and time
- Level
- Beginner
- Estimated time
- 7 minutes
Related public exercises
Summary
Return both the minimum and maximum values from an array.
Problem statement
Given a list of comparable values (integers), return a tuple (minimum, maximum). If the list is empty, return (None, None). You should traverse the list only once (O(n) time) and O(1) extra space.
Task
Implement a function that scans an array once to determine both min and max values, handling single-element and empty arrays explicitly.
Examples
Basic example
Input
[3, 1, 4, 2]
Output
(1, 4)
Explanation
Minimum is 1 and maximum is 4.
Input format
A single argument: a list of integers, e.g. [3, 1, 4].
Output format
A tuple (min_value, max_value). For an empty list return (None, None).
Constraints
List length 0..10^6. Values are integers. Time O(n), space O(1).
Samples
Sample input 0
[7]
Sample output 0
(7, 7)
Explanation 0
Single element: both min and max are 7.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.