Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Seed both `min` and `max` to `nums[0]`, update both as you scan, and return `min + max`. One pass instead of two.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you fuse two reductions into one pass and seed correctly.
Where candidates slip
0-seeding, or two separate scans.
How to stand out
Note both extremes are independent reductions that can share a traversal.
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
Track min and max together
1Hint 1Explore hintHide hint
Do you need two passes, or can min and max share one?
2Hint 2Explore hintHide hint
Two separate ifs per element: maybe-lower-min, maybe-raise-max.
3Hint 3Explore hintHide hint
Seed both extremes with nums[0], not 0.
A · Core idea
Track the running minimum and maximum together in one pass, then add them. Two independent ifs per element — one can lower the min, the other can raise the max — avoid a second scan.
Why one pass suffices. Min and max are independent reductions over the same data, so they can share a single traversal. Scanning twice is correct but wasteful; combining them halves the passes.
Why seed both with `nums[0]`. Same reasoning as array-max/min: seeding with a real element keeps both extremes valid for all-negative or all-positive arrays. Seeding with 0 breaks one side or the other.
Overflow. min + max of values up to 10⁹ stays within int, but returning a long is a safe habit when you're unsure of the range.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [3, 7, 2, 9]seed
min=max=3
(3,3)11C · 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 — two comparisons per element in a single pass, O(1) state.
E · Edge cases and pitfalls4nums = [5] — min = max = 5 → 10.Explore edge casesHide edge cases
Edge cases handled
nums = [5]min = max = 5 → 10.nums = [-4, -1]min -4, max -1 → -5.
Pitfalls
- Seeding min/max with 0 instead of nums[0].
- Scanning twice when one pass works.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic long sumMinMax(int[] nums) { int min = nums[0], max = nums[0]; for (int x : nums) { if (x < min) min = x; if (x > max) max = x;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Seeding min/max with 0 instead of nums[0].
- Scanning twice when one pass suffices.
Revise
Carry the pattern into the interview
Pattern to remember
Dual running extremes
Code pattern to memorize
lo=hi=nums[0]; for x: lo=min(lo,x); hi=max(hi,x); return lo+hiRules to remember
- 1Seed both with nums[0].
- 2Two independent ifs per element.
- 3Return their sum.
Use this pattern when
- Multiple reductions over one array
- Fusing passes
Interview takeaway
Min and max can share one pass — track both.
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: Dual running extremes. Master it on small inputs and it transfers to harder problems.