Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Trim and split on runs of whitespace, then count the pieces; or scan and count each position where a non-space follows a space (a word start).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you handle messy whitespace via transition counting or a robust split.
Where candidates slip
The spaces+1 approach failing on edge spacing.
How to stand out
Explain why counting word-starts is more robust than counting separators.
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
Count word-starts
1Hint 1Explore hintHide hint
Count where words *start*, not the spaces between them.
2Hint 2Explore hintHide hint
A word start = non-space whose previous char was a space (or the first char).
3Hint 3Explore hintHide hint
Watch leading, trailing, and repeated spaces.
A · Core idea
Count word beginnings, not separators. A character starts a word when it is non-space and either it's the first character or the preceding character was a space. Track an inWord flag and increment on each transition into a word.
Why not 'spaces + 1'. That formula breaks on leading/trailing spaces and on multiple spaces between words (' a b '). Counting transitions handles all of those messy cases uniformly.
Why not a naive split on a single space. Splitting "a b" on " " yields empty tokens you'd have to filter. Splitting on runs of whitespace (\s+) or counting transitions avoids that.
The flag mechanics. Set inWord = true and count when you enter a word; set inWord = false on a space. That single boolean is all the state you need.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = " hi yo "spaces
inWord stays false
count=02C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one character visit and a flag update — linear time, constant space.
E · Edge cases and pitfalls5s = "the quick fox" — 3 words.Explore edge casesHide edge cases
Edge cases handled
s = "the quick fox"3 words.s = " hello world "Extra spaces ignored → 2.s = ""0 words.
Pitfalls
- Counting spaces + 1 (breaks on multiple/leading/trailing spaces).
- Counting empty tokens from a naive single-space split.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int countWords(String s) { int count = 0; boolean inWord = false; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != ' ') {Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Counting spaces + 1 (breaks on multiple/leading/trailing spaces).
- Counting empty tokens from a naive split on a single space.
Revise
Carry the pattern into the interview
Pattern to remember
Count word-starts
Code pattern to memorize
inWord=false; for ch: if ch!=' ' and !inWord: count++; inWord=(ch!=' ')Rules to remember
- 1Word start = non-space after space.
- 2Track an inWord flag.
- 3split() handles runs of spaces.
Use this pattern when
- Tokenising with messy delimiters
Interview takeaway
Count transitions, not separators, to survive messy spacing.
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: Count word-starts. Master it on small inputs and it transfers to harder problems.