Collapse Extra Spaces In Place
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills a naive collapseSpaces is the string that's all spaces, or empty. Bloomberg reported this one in July 2019, and it looks like a warm-up until your output has a stray leading space or a trailing one you forgot to trim. The task is plain string normalization: strip the ends, squash every internal run of spaces to a single space, return the result. Input goes up to 10^5 characters, so quadratic string concatenation is a real risk. If you blank on the exact pointer logic during the live OA, StealthCoder runs invisibly as a safety net and hands you a clean solution.
The problem
Remove leading and trailing ASCII spaces from text and replace every nonempty internal run of spaces with one space. Return the normalized string. Function collapseSpaces(text: String) → String Examples Example 1 text = "hello world there" return = "hello world there" Each internal run collapses to one space. Constraints 0 <= text.length <= 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single pass with a write pointer, or a split-and-join that ignores empty tokens. In an array of characters, copy a character only if it's not a space, or if it's a space and the last written character isn't a space and something has already been written. After the loop, if the last written character is a space, drop it. That handles leading spaces, internal runs, and trailing spaces in one scan, O(n) time. The pitfalls: repeated string concatenation in languages with immutable strings, using a regex that leaves an edge space behind, and forgetting the all-spaces case, which should return an empty string. Only the ASCII space counts here, not tabs or newlines, so don't reach for a whitespace helper that eats more than the spec says. If the pointer logic slips under pressure, StealthCoder is your hedge during the live OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Collapse Extra Spaces In Place cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Collapse Extra Spaces In Place FAQ
How hard is the Bloomberg collapse spaces problem really?+
Easy on paper. The algorithm is one pass. The difficulty is edge cases: empty input, all spaces, a single leading run, and a single trailing run. If you test those four by hand before submitting, you're very likely fine.
What's the trick to solving it in place?+
Use a write index. Copy a non-space character always. Copy a space only if something has been written and the previous written character isn't a space. At the end, trim one trailing space if it exists, then cut the buffer to the write index.
Can I just use split and join?+
Yes, if the language lets you split on spaces and skip empty tokens, then join with one space. Watch that your split doesn't treat tabs or newlines as separators, since the spec says ASCII spaces only. Manual pointer code is safer for exact behavior.
What edge cases should I test first?+
Test the empty string, a string of only spaces, leading spaces only, trailing spaces only, and a single word with no spaces. Also try a long run of spaces in the middle. Those cases catch nearly every bug in this problem.
How do I prep for this in 48 hours?+
Write the two-pointer version once from memory, then run it on the edge cases above. Know the complexity: O(n) time, and O(n) extra space if your language has immutable strings. That's enough for this pattern.