Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Walk the string tracking whether the previous character was a space (or it's the start). At a word-start letter, upper-case it; otherwise copy as-is.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you track word boundaries with a flag and capitalise selectively.
Where candidates slip
Missing the first word, or upper-casing the whole string.
How to stand out
Handle leading spaces and multiple spaces cleanly via the flag.
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
Word-start uppercase
1Hint 1Explore hintHide hint
When does a letter begin a word?
2Hint 2Explore hintHide hint
Track a 'start of word' boolean, set after each space.
3Hint 3Explore hintHide hint
Capitalise only word-start letters; leave the rest alone.
A · Core idea
Capitalise the first letter of each word: a letter is a word-start when it is the first character or the previous character was a space. Upper-case exactly those letters and copy the rest unchanged.
Why a 'word-start' flag. A single boolean — 'is the next letter the start of a word?' — set true initially and after each space, tells you which letters to capitalise. It cleanly handles the first word (no leading space) and runs of spaces.
Why not upper-case everything. That's a different problem (to-uppercase). Here only the leading letter of each word changes; the rest of each word is left as-is.
Edge handling. Reset the flag to true on every space; clear it once you've processed a non-space. Non-letter word-starts (e.g. a leading digit) simply aren't shifted.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "hi yo"h
start → H
H"Hi Yo"C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(n) space — one character visit and a flag update each step.
E · Edge cases and pitfalls5s = "hello world" — → "Hello World".Explore edge casesHide edge cases
Edge cases handled
s = "hello world"→ "Hello World".s = "a b c"→ "A B C".s = "hi"First word only → "Hi".
Pitfalls
- Capitalising every letter (that's to-uppercase).
- Forgetting the very first word when there's no leading space.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String capitalize(String s) { char[] c = s.toCharArray(); boolean start = true; for (int i = 0; i < c.length; i++) { if (c[i] == ' ') {Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Capitalising every letter (that's to-uppercase).
- Forgetting the very first word when there's no leading space.
Revise
Carry the pattern into the interview
Pattern to remember
Word-start flag
Code pattern to memorize
start=true; for ch: if ch==' ': start=true else: if start upper(ch); start=falseRules to remember
- 1Word start = first char or after space.
- 2Upper-case only those.
- 3Reset flag on spaces.
Use this pattern when
- Per-word boundary processing
Interview takeaway
A 'just saw a space' flag marks where words begin.
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: Word-start flag. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups