Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Fold a running maximum: `max(a, max(b, c))`. Each comparison keeps the larger value; after two comparisons you have the overall maximum.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you reduce a small problem to a reusable fold instead of a bespoke branch tangle.
Where candidates slip
Nested ifs with a subtle boundary bug, or comparing c against a instead of against max(a, b).
How to stand out
State explicitly that this is array-maximum with n = 3 and the same comparison generalises.
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
Nested max / running max
1Hint 1Explore hintHide hint
Can you reduce 'max of three' to repeated 'max of two'?
2Hint 2Explore hintHide hint
Compute max(b, c) first, then compare that result with a.
3Hint 3Explore hintHide hint
This is a running maximum unrolled — the same fold an array-max loop uses.
A · Core idea
The maximum of three numbers is the maximum of the first and the maximum of the other two: max(a, max(b, c)). You compute one pairwise max, then feed that 'running best' into a second comparison.
Why a running maximum, not three-way ifs. You could write a chain of if/else-if conditions, but those are error-prone (it's easy to get a boundary wrong). Folding — keep a best-so-far and compare the next value against it — is both shorter and the exact technique that scales to an array of a million numbers. Three values is just the fold unrolled twice.
Why ties are harmless. If two or three values are equal, each comparison keeps an equal value, so the final answer is still correct. No special case needed.
Showing the interviewer you see this as array-maximum with n = 3 — rather than a one-off trick — signals that you think in patterns.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = 3, b = 7, c = 5m = max(a, b)
max(3, 7) = 7
m = 77C · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Show the interviewer you see this as the base case of the array-maximum loop, not a special trick.
ComplexityO(1) time, O(1) space — two comparisons, fixed regardless of the values.
E · Edge cases and pitfalls4a = 9, b = 2, c = 9 — Tie at the top — returns 9; equal values survive each comparison.Explore edge casesHide edge cases
Edge cases handled
a = 9, b = 2, c = 9Tie at the top — returns 9; equal values survive each comparison.a = -1, b = -5, c = -2Returns -1, the largest (least negative).
Pitfalls
- Writing a long if/else-if chain that mishandles an equality boundary.
- Forgetting that the second comparison must use the result of the first, not the original a.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int max3(int a, int b, int c) { int m = a > b ? a : b; return m > c ? m : c; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Writing a long chain of nested ifs that's easy to get wrong.
- Forgetting that ties don't change the answer.
Revise
Carry the pattern into the interview
Pattern to remember
Running maximum (unrolled)
Code pattern to memorize
m = max(a, b); return max(m, c);Rules to remember
- 1
max(a, max(b, c)). - 2Each step keeps the larger value.
- 3This is array-max with n = 3.
Use this pattern when
- Largest of a small fixed set
- Demonstrating the running-max fold before scaling to arrays
Interview takeaway
A running max scales from 3 values to a million — same fold.
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 (unrolled). Master it on small inputs and it transfers to harder problems.