Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
If n == 0 return "0". Else, while n > 0, prepend `n % 2` and set `n /= 2`. (Built-ins: Integer.toBinaryString / bin(n).)
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know the repeated-division base-conversion and handle 0/ordering.
Where candidates slip
Empty string for 0, or not reversing.
How to stand out
Generalise the scheme to any base.
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
Repeated division by 2
1Hint 1Explore hintHide hint
What does n % 2 give you, and in which order?
2Hint 2Explore hintHide hint
Collect remainders, then reverse (or prepend).
3Hint 3Explore hintHide hint
What must you return for n = 0?
A · Core idea
Convert a non-negative integer to its binary string by repeated division by 2: each n % 2 is the next bit (least significant first) and n / 2 shrinks the number. Collect the bits and reverse.
Why bits come out LSB-first. The first remainder is the units (2⁰) bit, the next is the 2¹ bit, etc. — so you generate the binary digits in reverse order. Reverse the collected bits (or prepend each) to get the conventional MSB-first string.
Why handle 0 explicitly. For n = 0 the while (n > 0) loop never runs and would return an empty string; the convention is "0", so special-case it.
General base conversion. The same mod-then-divide scheme converts to any base (use base b instead of 2). Built-ins exist (Integer.toBinaryString, bin(n)) but the algorithm is the point.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 55 % 2
1, n=2
bits: 1"101"C · Code with teaching comments
D · Why it works and complexityO(log n) timeO(log n) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(log n) time, O(log n) space — one division per bit (≈ log₂ n), producing a string of that length.
E · Edge cases and pitfalls5n = 0 — Special-cased → "0".Explore edge casesHide edge cases
Edge cases handled
n = 0Special-cased → "0".n = 8→ "1000".n = 1→ "1".
Pitfalls
- Returning empty string for n == 0.
- Forgetting to reverse the collected bits.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String toBinary(int n) { if (n == 0) return "0"; StringBuilder sb = new StringBuilder(); while (n > 0) { sb.append(n % 2);Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Returning empty string for n == 0.
- Forgetting to reverse the collected bits.
Revise
Carry the pattern into the interview
Pattern to remember
Repeated mod/div base conversion
Code pattern to memorize
while n>0: bits.append(n%2); n//=2; reverse (handle 0)Rules to remember
- 1bit = n%2.
- 2n /= 2.
- 3Reverse at the end; handle 0.
Use this pattern when
- Base conversion to base b
Interview takeaway
%2 and /2 convert decimal to binary, LSB first.
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: Repeated mod/div base conversion. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups