Prompt
Reverse all words of 5+ letters in a string. Keep shorter words and spaces intact.
Solution
This is the split → map → join pipeline, the standard shape for any per-word (or per-token) string transformation. JavaScript strings are immutable, so "modify some words" really means "tokenize, transform tokens, reassemble". Each stage is O(n) over the total character count, so the whole thing is O(n) time, O(n) space — and since immutability forces allocation anyway, there's no cheaper approach to aim for; the question is about clean decomposition.
The idiom worth having at your fingertips is string reversal itself: w.split('').reverse().join(''), because strings have no .reverse() of their own. The conditional inside map — transform if length >= 5, else pass through unchanged — is the "selective map" pattern: map over everything, decide per element, never filter (filtering would drop the short words and lose positions).
Boundary detail: split(' ') splits on single spaces, which preserves word count for normal sentences; ''.split(' ') gives [''], and the empty "word" passes through untouched, so the empty string round-trips correctly. If the input could have multiple consecutive spaces, you'd need split(/(\s+)/) to preserve the exact whitespace — worth mentioning as a scope boundary.
Red flag: > instead of >= — the off-by-one that leaves exactly-5-letter words ("world") unreversed. Boundary conditions on "5 or more" phrasing are precisely what the hidden test checks. Also split('').reverse().join('') breaks on emoji/surrogate pairs — fine here, worth knowing.
Say it: "Split, map with a length condition, join — the selective-map pattern — and I use >= 5 because 'five or more' includes five."