Definition
Smallest value
Return the least element in a list of numbers.
Finding the minimum in an unsorted list is a single left-to-right scan: start with arr[0], then update whenever a smaller value appears. This tutorial covers the pattern, empty-list handling, negatives, a live preview, worked Python examples, edge cases, and complexity.
Smallest value
Return the least element in a list of numbers.
One pass
Track a running minimum while walking the list.
arr[0] first
Seed min_val with the first element, then compare the rest.
No min
Reject empty lists before scanning.
Sample → 2
Run the classic sample list in the browser.
O(1) space
One pass, constant extra memory — faster than sorting.
Finding the minimum in an unsorted list means returning the smallest value. Start with the first element as the current minimum; whenever you see a smaller number, update it.
After one pass, the running minimum is the answer. The same idea works for all-negative lists — the minimum is simply the most negative value.
It mirrors finding the maximum with one flipped comparison — a classic interview twin pair for loops and empty-input thinking.
Update whenever x < min_val.
Avoid sentinel values that break negatives.
Raise or validate before indexing.
Do not sort when you only need the min.
In short: if the list is non-empty, set min_val = arr[0], then for each later value update when smaller.
Given a non-empty list of integers, return the smallest value using one linear scan.
# [12, 5, 7, 3, 2, 8, 10] -> 2
# min_val starts at 12, then updates to 5, 3, 2 | Item | Type | Description |
|---|---|---|
arr | list[int] | Non-empty list of numbers. |
| Return / print | int / text | The smallest value in the list. |
function find_min(arr):
if arr is empty:
error
min_val <- arr[0]
for each value from second element:
if value < min_val:
min_val <- value
return min_val | Method | Idea | Notes |
|---|---|---|
| Linear scan | Running minimum | Interview default — O(n), O(1) space |
Built-in min | min(arr) | Fine in apps; show loops in interviews |
| Sort then take first | Sort ascending | Slower — avoid when only min is needed |
| Goal | Pattern |
|---|---|
| Seed | min_val = arr[0] |
| Update | if arr[i] < min_val: min_val = arr[i] |
| Empty guard | if not arr: raise ValueError(...) |
| Slice loop | for x in arr[1:]: |
| Track index | Keep min_i when updating |
| Built-in | min(arr) (after explaining the scan) |
Same answer — different costs and interview signals.
running minThis page — clear O(n) interview style
min(arr)Production shortcut after you can explain it
sorted(arr)[0]Overkill — usually O(n log n)
mention emptyDefine behavior for [] up front
Reach for a running minimum whenever you need the smallest value in one pass.
Loops, comparisons, and empty-list discussion.
Same pattern; only the comparison flips.
Lowest score, coldest reading, cheapest price.
Practice less-than updates and negatives.
If you need order of all elements, sort instead.
Key benefit: one short loop that locks in running state, empty guards, and O(n) reasoning — the twin of array maximum.
Uses the same sample list as Example 1: 12, 5, 7, 3, 2, 8, 10.
Three complete Python programs — classic scan to 2, all-negative list, and min-with-index tracking. Click View Output to reveal sample console results.
A reusable helper and a mixed positive sample.
Same sample values as the reference flow; output is 2.
def find_min(arr: list[int]) -> int:
if not arr:
raise ValueError("Array must contain at least one element.")
min_val = arr[0]
for i in range(1, len(arr)):
if arr[i] < min_val:
min_val = arr[i]
return min_val
def main() -> None:
array = [12, 5, 7, 3, 2, 8, 10]
min_value = find_min(array)
print(f"Minimum value in the array: {min_value}")
if __name__ == "__main__":
main() The function raises a clear error for empty input and uses one simple pass for normal lists. Running min starts at 12, then updates to 5, 3, and finally 2.
The minimum is the smallest value — even when every element is negative.
Smallest means most negative, so the answer is -9 here.
def find_min(arr: list[int]) -> int:
if not arr:
raise ValueError("Array must contain at least one element.")
min_val = arr[0]
for x in arr[1:]:
if x < min_val:
min_val = x
return min_val
def main() -> None:
negatives = [-9, -3, -1, -7]
print(f"Minimum (most negative): {find_min(negatives)}")
if __name__ == "__main__":
main() Seeding with arr[0] (not a huge positive sentinel) keeps the logic simple. Here -9 is smaller than -7, -3, and -1.
Common follow-up: return both the minimum and its first index.
Updates both value and index whenever a strictly smaller element appears.
def find_min_with_index(arr: list[int]) -> tuple[int, int]:
if not arr:
raise ValueError("Array must contain at least one element.")
min_val = arr[0]
min_i = 0
for i in range(1, len(arr)):
if arr[i] < min_val:
min_val = arr[i]
min_i = i
return min_val, min_i
array = [12, 5, 7, 3, 2, 8, 10]
value, index = find_min_with_index(array)
print(f"Minimum {value} found at index {index}") Strict < keeps the first occurrence if duplicates exist. Use <= only if you intentionally want the last index of the minimum.
If the list is empty, raise an error — no minimum exists.
Set min_val = arr[0] as the first candidate.
For each later element, replace min_val when smaller.
Return the final running minimum.
Trace the running minimum for [12, 5, 7, 3, 2, 8, 10].
| Step | See | min_val |
|---|---|---|
| Start | 12 | 12 |
| Next | 5 | 5 |
| Next | 7 | 5 (unchanged) |
| Next | 3 | 3 |
| Next | 2 | 2 |
| Next | 8, 10 | 2 (unchanged) |
Final answer: 2 — matching Example 1.
Where finding a list minimum shows up beyond the interview prompt.
One-pass loops and empty-list talk.
Example: write find_min(arr).
Same scan; flip > to <.
Example: twin of find_max.
Lowest score or coldest reading in a series.
Example: min temperature today.
Show most-negative is the minimum.
Example: min of [-9, -1].
State O(n) vs sorting for interviews.
Example: one pass is enough.
Return position of the minimum too.
Example: Example 3 pattern.
Pro Tip: open with “empty check, seed arr[0], one pass with <” before writing code.
Why the linear-scan approach works well in interviews.
O(n) time and O(1) extra space for unsorted input.
Dry-run a short list and watch min_val update.
Seeding with arr[0] works for all-negative lists.
Track index, or flip to maximum with one change.
Pro Tip: lead with the manual scan; mention min(arr) only as a production aside.
Small habits that keep min-finding solutions interview-ready.
Raise before reading arr[0].
Do not initialize to a huge constant blindly.
Keeps the first index if you also track position.
Sorting is slower when you only need the min.
One pass, constant extra memory.
Pro Tip: dry-run 12 → 5 → 3 → 2 aloud — if that matches, your update logic is correct.
Mistakes that commonly break min-finding solutions.
Reading arr[0] when the list is empty.
→ Guard with if not arr first.
Copying max code but forgetting to flip > to <.
→ Minimum uses less-than updates.
Sorting just to take the first element.
→ Use one O(n) scan.
Starting the loop at 0 incorrectly.
→ Loop from index 1 (or use arr[1:]).
Returning the least negative when asked for maximum.
→ Clarify: min is farthest left on the number line.
Handle these before claiming the scan is complete.
There is no minimum; validate before reading first element.
The minimum is that one value.
Minimum is the most negative value (e.g. -9).
Value is unchanged; index depends on < vs <=.
Same scan — no special case needed.
Same comparison logic works for floats.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
O(n) time and O(1) extra space.min(arr), but manual scan is often required in interviews. Large systems can combine partial minima in parallel.Quick Takeaway: guard empty, seed with arr[0], update on <, return in O(n) time.
| Approach | Time | Extra space |
|---|---|---|
| Single scan | O(n) | O(1) |
Built-in min | O(n) | O(1) |
| Sort then take first | O(n log n) | depends on sort |
For unsorted input, a single scan is the standard interview answer.
Finding the minimum is a one-pass running minimum: guard empty lists, seed with arr[0], and update on smaller values. The same pattern works for negatives and extends cleanly to tracking the index.
Practice the three examples above, then continue to displaying a multiplication table.
Empty check first, then min_val = arr[0], update when x < min_val.
arr[0]Find the smallest value the interview-friendly way.
Running min
PatternStart at arr[0]
InitGuard first
SafetyMost negative wins
EdgeO(n) / O(1)
AnalysisFinding the smallest value in an unsorted list takes a single left-to-right pass: O(n) time and O(1) extra memory. In the worst case, any unseen element could still be the new minimum.
Learn how to use nested loops to display a multiplication table in Python.
8 people found this page helpful