Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Compare `n` with 0: greater → 1, less → -1, equal → 0. Order the checks so zero is handled correctly.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you enumerate all cases of a small classification and handle the boundary (zero) cleanly.
Where candidates slip
Omitting the zero branch or writing conditions that aren't mutually exclusive.
How to stand out
Mention `Integer.signum` and the branchless `(n>0)-(n<0)` idiom as alternatives.
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
Three-way comparison
1Hint 1Explore hintHide hint
How many distinct outcomes are there? Map each to a return value.
2Hint 2Explore hintHide hint
Three guarded returns make the cases mutually exclusive for free.
3Hint 3Explore hintHide hint
The fall-through `return 0` handles zero without an explicit `== 0` test.
A · Core idea
There are exactly three outcomes — positive, negative, zero — so the cleanest solution is three guarded returns: if (n > 0) return 1; if (n < 0) return -1; return 0;.
Why guarded returns beat one big if/else. Each return exits immediately, so the branches are automatically mutually exclusive and you can't accidentally fall into two of them. The final return 0 is reached only when both comparisons failed, which is precisely the n == 0 case — so you never even write == 0 explicitly.
Why order is flexible but coverage is not. You may test < 0 first or > 0 first; what matters is that all three cases are covered and zero has a home. The classic bug is forgetting zero and returning a wrong default.
Library shortcut. Integer.signum(n) (Java) or (n > 0) - (n < 0) (a branchless C-style trick) both do this; mention them but show the explicit logic when fundamentals are being tested.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = -5n > 0?
-5 > 0 is false
skip-1C · 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 — at most two comparisons — constant.
E · Edge cases and pitfalls4n = 0 — Both comparisons fail, so the function falls through to `return 0`.Explore edge casesHide edge cases
Edge cases handled
n = 0Both comparisons fail, so the function falls through to `return 0`.n = 2147483647Returns 1; no arithmetic is done so there's no overflow risk.
Pitfalls
- Forgetting the zero case and returning a stray default.
- Using overlapping conditions so a value could match two branches.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int sign(int n) { if (n > 0) return 1; if (n < 0) return -1; return 0; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Forgetting the zero case and returning a wrong default.
- Using `Integer.signum`/`math.copysign` when asked to show the logic.
Revise
Carry the pattern into the interview
Pattern to remember
Three-way branch
Code pattern to memorize
if n>0 return 1; if n<0 return -1; return 0;Rules to remember
- 1
>0 → 1,<0 → -1, else 0. - 2Cover zero explicitly.
- 3Branches must be mutually exclusive.
Use this pattern when
- Three-way classification of one value
- Comparator-style results (-1/0/1)
Interview takeaway
Whenever a value splits into 3 outcomes, write 3 guarded returns.
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: Three-way branch. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups