Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Append each non-space character to a builder and return it. Use a StringBuilder in Java to avoid O(n²) concatenation.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you avoid the O(n²) string-concatenation trap with a builder.
Where candidates slip
`+=` in a loop, or confusing remove-all with trim.
How to stand out
Explain the amortised-O(1) append of StringBuilder.
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
Filtered build
1Hint 1Explore hintHide hint
Append only the characters you want to keep.
2Hint 2Explore hintHide hint
Use a StringBuilder / join — why not `+=` in a loop?
3Hint 3Explore hintHide hint
Remove-all is different from trim (leading/trailing only).
A · Core idea
Filter out spaces by appending only non-space characters to a builder. The key performance point: use a StringBuilder (Java) or ''.join (Python), never repeated += on a String.
Why `+=` in a loop is O(n²). Java/Python strings are immutable, so each s += ch allocates a brand-new string and copies all previous characters. Over n appends that's 1+2+…+n = O(n²) copying. A StringBuilder appends into a growable buffer in amortised O(1), keeping the whole build O(n).
Remove-all vs trim. This removes every space, not just leading/trailing (that's trim). Be sure which the prompt wants.
Generalises to any filter. Swap the ch != ' ' predicate for any rule and you have a generic 'keep characters matching X' transform.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "a b c"a
keep
a"abc"C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
Calling out the StringBuilder-vs-+= performance trap is a nice senior touch.
ComplexityO(n) time, O(n) space — one pass appending into a growable buffer — O(n) time with amortised O(1) appends.
E · Edge cases and pitfalls5s = " x " — All spaces gone → "x".Explore edge casesHide edge cases
Edge cases handled
s = " x "All spaces gone → "x".s = "abc"No spaces → unchanged.s = " "All spaces → empty string.
Pitfalls
- Using String += in a loop (O(n²)).
- Only trimming ends instead of removing all spaces.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String removeSpaces(String s) { StringBuilder sb = new StringBuilder(); for (char ch : s.toCharArray()) { if (ch != ' ') sb.append(ch); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1String += in a loop
Immutable strings copy everything on each +=, making the build O(n²); a StringBuilder is O(n).
Explore the code comparisonHide the code comparison
String += in a loop
Immutable strings copy everything on each +=, making the build O(n²); a StringBuilder is O(n).
Don't write this
String r = ""; for (char ch : ...) if (ch!=' ') r += ch; // O(n^2)Write this instead
StringBuilder sb = new StringBuilder(); ... sb.append(ch);Revise
Carry the pattern into the interview
Pattern to remember
Filtered build
Code pattern to memorize
sb=builder; for ch: if ch!=' ': sb.append(ch)Rules to remember
- 1Keep non-spaces.
- 2Use StringBuilder/join.
- 3Avoid += in a loop.
Use this pattern when
- Filtering/transforming a string
Choose another pattern when
- Reaching for += in a loop — use a builder
Interview takeaway
Build filtered strings with a builder, never repeated concatenation.
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: Filtered build. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups