Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Start at 0; for each char, do `result = result * 2 + (c - '0')`. O(length).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you use Horner's method instead of per-digit powers.
Where candidates slip
Power-per-digit, or using the raw char code.
How to stand out
Note it's the same scheme as decimal parsing, generalising 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
Horner's method
1Hint 1Explore hintHide hint
Process bits left to right — how does the running value change per bit?
2Hint 2Explore hintHide hint
result = result * 2 + bit avoids explicit powers of two.
3Hint 3Explore hintHide hint
Convert the char '0'/'1' to its int value.
A · Core idea
Convert a binary string to its integer value with Horner's method: scan left to right, and for each bit do result = result * 2 + bit.
Why Horner's rule works. Reading MSB-first, each new bit means the existing value shifts left one place (×2, like appending a binary digit) and the new bit is added in the units place. This evaluates the polynomial b₀·2ᵏ + … + bₖ without ever computing explicit powers of two — it's the same scheme as parsing a decimal number, just base 2.
Why not pow(2, i) per digit. Computing a power for each position is slower and more overflow-prone; Horner's needs only one multiply-add per bit.
Char-to-int. Convert each character with c - '0' to get 0 or 1; forgetting this and using the char's ASCII code is a common slip.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "101"'1'
0*2 + 1
result=15C · Code with teaching comments
D · Why it works and complexityO(length) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(length) time, O(1) space — one multiply-add per character — linear in the string length.
E · Edge cases and pitfalls5s = "1000" — → 8.Explore edge casesHide edge cases
Edge cases handled
s = "1000"→ 8.s = "0"→ 0.s = "1111"→ 15.
Pitfalls
- Computing pow(2, i) per digit (slower, overflow-prone).
- Not converting the char '0'/'1' to its int value.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int toDecimal(String s) { int result = 0; for (int i = 0; i < s.length(); i++) { result = result * 2 + (s.charAt(i) - '0'); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Computing pow(2, i) per digit (slower, overflow-prone).
- Not converting the char '0'/'1' to its int value.
Revise
Carry the pattern into the interview
Pattern to remember
Horner's method
Code pattern to memorize
result = 0; for c: result = result*2 + (c-'0')Rules to remember
- 1result = result*2 + bit.
- 2Left to right.
- 3char - '0' for the digit.
Use this pattern when
- Parsing numbers in any base
Interview takeaway
Horner's rule turns digit parsing into one multiply-add per char.
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: Horner's method. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups