Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Return `a > b ? a : b`. A single comparison decides it; ties can return either value.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can express a selection cleanly and recognise it as the base case of a running max.
Where candidates slip
Writing verbose nested ifs, or reaching for the library when asked to show the logic.
How to stand out
Point out this is exactly the comparison an array-maximum loop repeats n times.
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
Single comparison
1Hint 1Explore hintHide hint
Only one fact decides the answer: is `a` bigger than `b`?
2Hint 2Explore hintHide hint
A single comparison with a ternary returns the larger value directly.
3Hint 3Explore hintHide hint
Don't add a separate branch for equality — both values are the same then.
A · Core idea
The maximum of two values is decided by a single comparison: if a is larger, the answer is a; otherwise it is b. The ternary a > b ? a : b expresses exactly that in one expression.
Why ties don't matter. When a == b, both branches return the same numeric value, so it is irrelevant whether you use > or >= — the result is identical. Don't waste a branch worrying about equality.
Why this matters beyond two numbers. This one comparison is the atom of every 'running maximum' loop. Finding the max of an array is just this comparison applied repeatedly against an accumulator. Internalise the ternary here and the array version writes itself.
On using the library `Math.max`. It's correct and you'd use it in real code, but if the interviewer is testing fundamentals they want to see the comparison. Mention both: 'In production I'd call Math.max; here's the comparison it's doing under the hood.'
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
a = 4, b = 9compare
is 4 > 9? No
select b9C · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(1) time, O(1) space — exactly one comparison and one selection regardless of the operand sizes.
E · Edge cases and pitfalls4a = 4, b = 4 — Equal — returns 4; the `>` test is false so it returns b, which is the same value.Explore edge casesHide edge cases
Edge cases handled
a = 4, b = 4Equal — returns 4; the `>` test is false so it returns b, which is the same value.a = -5, b = -2Returns -2; comparisons work the same for negatives.
Pitfalls
- Over-engineering with if/else-if chains when one ternary suffices.
- Believing the `>` vs `>=` choice changes the result on a tie — it doesn't.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int max(int a, int b) { return a > b ? a : b; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Using `if/else if` but forgetting the equal case (still works, but shows fuzzy thinking).
- Calling a library `max` when the interviewer wants to see the comparison.
Revise
Carry the pattern into the interview
Pattern to remember
Ternary comparison
Code pattern to memorize
return a > b ? a : b;Rules to remember
- 1
a > b ? a : b. - 2Ties can return either operand.
- 3Generalises to N values by folding a running max.
Use this pattern when
- Choosing the larger of two values
- The base step inside any running-max loop
Interview takeaway
Master the ternary here; you'll reuse it inside every running-max loop.
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: Ternary comparison. Master it on small inputs and it transfers to harder problems.