Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Build the result, appending `newCh` where the character equals `oldCh` and the original character otherwise.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you do a clean per-character transform without the concatenation trap.
Where candidates slip
Swapping old/new, or O(n²) building.
How to stand out
Frame it as a general map-over-characters transform.
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
Map-and-build
1Hint 1Explore hintHide hint
Each output char is either the replacement or the original.
2Hint 2Explore hintHide hint
Be careful which argument is the target and which is the replacement.
3Hint 3Explore hintHide hint
Use a char array / builder, not += in a loop.
A · Core idea
Transform each character: output the replacement where it matches oldCh, otherwise the original. One pass with a char array (Java) or str.replace (Python) — the original is untouched because strings are immutable.
Why a map-and-build. Each output character is a pure function of the input character (replace-or-keep), so a single pass produces the result. This generalises to any per-character mapping.
Direction matters. Replace oldCh with newCh; swapping the arguments is the common bug. Be clear which is the search target and which is the replacement.
Performance. Use a char array or builder, not += in a loop, to stay O(n).
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "banana", old='a', new='o'b
keep
b"bonono"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 comparison and write per character.
E · Edge cases and pitfalls5s = "abc", old='x' — No match → unchanged.Explore edge casesHide edge cases
Edge cases handled
s = "abc", old='x'No match → unchanged.s = "aaa", old='a', new='b'→ "bbb".s = ""Empty → "".
Pitfalls
- Replacing the wrong direction (new with old).
- Using += in a loop instead of a char array/builder.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String replace(String s, char oldCh, char newCh) { char[] c = s.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] == oldCh) c[i] = newCh; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Replacing the wrong direction (new with old).
- Using += in a loop instead of a char array/builder.
Revise
Carry the pattern into the interview
Pattern to remember
Map-and-build
Code pattern to memorize
for ch: out += (ch==old ? new : ch)Rules to remember
- 1Match → replacement.
- 2Else → original.
- 3One pass.
Use this pattern when
- Per-character mapping
Interview takeaway
Per-character transforms are a single map-and-build pass.
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: Map-and-build. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups