Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Split on spaces and fold a running longest, updating only when a word is strictly longer (so the first of a tie wins).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you map this to a running max and handle the tie rule with strictness.
Where candidates slip
`>=` returning the wrong tie, or empty tokens from a bad split.
How to stand out
Identify it as array-maximum keyed by word length.
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
Running longest
1Hint 1Explore hintHide hint
Split into words, then it's a running max over lengths.
2Hint 2Explore hintHide hint
Which comparison keeps the FIRST word on a length tie?
3Hint 3Explore hintHide hint
Use strict > so later equal-length words don't replace the earlier one.
A · Core idea
Tokenise the sentence into words, then apply the running-maximum pattern over word lengths, keeping the first word on a tie.
Why strict `>` for the tie rule. Updating best only when a word is strictly longer means a later word of equal length never replaces the earlier one — so the first of several same-length words wins, as required. Using >= would keep the last instead.
Why tokenise robustly. Split on runs of whitespace so extra spaces don't produce empty 'words' that could masquerade as length-0 entries.
This is the same fold as array-maximum, just with the comparison key being length(word) instead of the value itself.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "the quick brown fox"the
len 3 > 0
best=the"quick"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 pass over the words comparing lengths; tokenising is linear in the input.
E · Edge cases and pitfalls5s = "a bb ccc" — Longest is "ccc".Explore edge casesHide edge cases
Edge cases handled
s = "a bb ccc"Longest is "ccc".s = "hi"Only word → "hi".s = "to be or"All len 2; first "to" wins.
Pitfalls
- Using `>=` and returning the last tie instead of the first.
- Splitting incorrectly and getting empty tokens.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String longestWord(String s) { String best = ""; for (String w : s.split(" ")) { if (w.length() > best.length()) best = w; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Using `>=` and returning the last tie instead of the first.
- Splitting incorrectly and getting empty tokens.
Revise
Carry the pattern into the interview
Pattern to remember
Running maximum (by length)
Code pattern to memorize
best=''; for w in words: if len(w)>len(best): best=wRules to remember
- 1Split into words.
- 2Strict > for first-on-tie.
- 3Track the best.
Use this pattern when
- Max-by-key over tokens
- First-on-tie selection
Interview takeaway
Use strict > in running-max when ties should keep the earlier item.
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: Running maximum (by length). Master it on small inputs and it transfers to harder problems.