Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Initialise `max` to `nums[0]` (never to 0 — that breaks on all-negative arrays) and scan once, updating `max` whenever an element is larger.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you write a correct running-max fold and seed the accumulator with a real element.
Where candidates slip
Seeding with 0 (or Integer.MIN handled wrong) so all-negative inputs break.
How to stand out
Note this is the base fold reused by second-largest and Kadane's algorithm.
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 maximum
1Hint 1Explore hintHide hint
Carry a 'best so far' as you walk the array — what should it start as?
2Hint 2Explore hintHide hint
Seed it with nums[0], not 0, so all-negative arrays still work.
3Hint 3Explore hintHide hint
Update best whenever the current element is larger; one pass is enough.
A · Core idea
Finding the largest element is the canonical running maximum (a 'fold'): carry a best-so-far value and update it whenever you meet something bigger. One pass, constant extra space.
Why seed with `nums[0]`, never 0. The accumulator must start at a value that is actually achievable. If you seed max = 0 and the array is all-negative (e.g. [-5, -1, -8]), nothing ever beats 0, so you'd wrongly return 0 — a value that isn't even in the array. Seeding with the first element guarantees the answer is a real element.
Why one pass is optimal. You cannot know the maximum without looking at every element at least once (any element you skip could be the largest), so O(n) is a hard lower bound — there's no cleverer algorithm.
This fold is the atom of max-of-three, second-largest, Kadane's maximum subarray, and countless other scans. Master the seed-and-update shape here.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [3, 7, 2, 9, 4]seed
max = nums[0]
max = 39C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Seeding with 0 instead of nums[0] is the classic bug — it returns 0 for an all-negative array.
ComplexityO(n) time, O(1) space — every element is visited once with O(1) work, and you can't find the max without seeing each element at least once.
E · Edge cases and pitfalls5nums = [-5, -1, -8] — Returns -1; seeding with nums[0] (not 0) is what makes this correct.Explore edge casesHide edge cases
Edge cases handled
nums = [-5, -1, -8]Returns -1; seeding with nums[0] (not 0) is what makes this correct.nums = [42]Single element is the max.nums = [4, 4, 4]Ties don't matter; returns 4.
Pitfalls
- Seeding max with 0 — returns 0 for an all-negative array.
- Starting the scan at index 1 but seeding with 0 instead of nums[0].
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int maxOf(int[] nums) { int max = nums[0]; for (int x : nums) { 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.
1Seeding the max with 0
For an all-negative array nothing exceeds 0, so you return 0 — a value not in the array.
Explore the code comparisonHide the code comparison
Seeding the max with 0
For an all-negative array nothing exceeds 0, so you return 0 — a value not in the array.
Don't write this
int max = 0;
for (int x : nums) if (x > max) max = x; // [-5,-1] -> 0 (WRONG)Write this instead
int max = nums[0];
for (int x : nums) if (x > max) max = x;Revise
Carry the pattern into the interview
Pattern to remember
Running maximum
Code pattern to memorize
max = nums[0]; for x in nums: if x > max: max = xRules to remember
- 1Seed with nums[0], not 0.
- 2Update on every larger element.
- 3Single O(n) pass.
Use this pattern when
- Largest/smallest in a sequence
- The base step of any running-extreme scan
Choose another pattern when
- Array is sorted — the max is just the last element
Interview takeaway
Seed running extremes with the first element, never a constant.
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 maximum. Master it on small inputs and it transfers to harder problems.