Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Initialise `min` to `nums[0]` and scan, keeping the smaller of (current min, element).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you see min/max as one fold and seed correctly.
Where candidates slip
Seeding with 0 so all-positive inputs return 0.
How to stand out
State that min and max can be tracked together in a single pass.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Running minimum
1Hint 1Explore hintHide hint
This is array-maximum with one operator changed — which one?
2Hint 2Explore hintHide hint
Seed the running min with nums[0], not 0.
3Hint 3Explore hintHide hint
Keep the smaller value at each step in a single pass.
A · Core idea
The minimum is the maximum's mirror image: carry a running smallest, seeded with nums[0], and keep the lesser value at each step. Identical fold, flipped comparison.
Why seed with `nums[0]`. The all-positive array is the dual trap: seed min = 0 and run [5, 1, 8] and you'd return 0, which isn't present. Seeding with the first element keeps the answer a real element regardless of sign.
Why it's still one pass. Same argument as the max — you must inspect every element, since any unseen one could be the smallest. O(n) is optimal.
Recognising min and max as the same loop with < swapped for > is the transferable insight; later you'll track both at once (sum-of-min-and-max).
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [5, 1, 8]seed
min = nums[0]
min = 51C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one comparison per element, O(1) state, and every element must be seen.
E · Edge cases and pitfalls4nums = [5, 1, 8] — Returns 1; seeding with 0 would wrongly return 0.Explore edge casesHide edge cases
Edge cases handled
nums = [5, 1, 8]Returns 1; seeding with 0 would wrongly return 0.nums = [-2]Single element is the min.
Pitfalls
- Seeding min with 0 — wrong for all-positive arrays.
- Accidentally reusing a `>` (max) comparison.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int minOf(int[] nums) { int min = nums[0]; for (int x : nums) { if (x < min) min = x; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Seeding min with 0 — wrong for all-positive arrays.
- Reusing a max comparison by mistake.
Revise
Carry the pattern into the interview
Pattern to remember
Running minimum
Code pattern to memorize
min = nums[0]; for x in nums: if x < min: min = xRules to remember
- 1Seed with nums[0].
- 2Keep the smaller value.
- 3O(n) one pass.
Use this pattern when
- Smallest in a sequence
- Pairs naturally with a running max
Interview takeaway
Min and max are the same loop with the comparison flipped.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Running minimum. Master it on small inputs and it transfers to harder problems.