Reported January 2022
Bloombergstring

Replace Characters in a Mutable String

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The edge case that sinks a naive solution on this Bloomberg OA, reported in January 2022, is the one you don't test: an empty string, or a source character that never appears. The task is simple on paper. Replace every occurrence of one character with another and return the result. It's a string problem with a input up to 10^6 characters, so the way you build the output matters more than the logic. If you blank on the details live, StealthCoder runs invisibly as a safety net and puts a clean solution on screen. Otherwise, read on and you won't need it.

The problem

Replace every occurrence of the one-character string from in text with the one-character string to. Return the result.

Function
replaceCharacter(text: String, sourceCharacter: String, targetCharacter: String) → String

Examples
Example 1
text = "banana"
sourceCharacter = "a"
targetCharacter = "o"
return = "bonono"
All three a characters become o.

Constraints
0 <= text.length <= 10^6.
from.length == to.length == 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that there is no trick. Walk the string once, and for each character, emit the target if it matches the source, else emit the original. Use a list or builder and join at the end. The pitfall is performance. If you concatenate strings in a loop, some languages copy the whole string each time, and at 10^6 characters that turns O(n) into O(n^2). The second pitfall is the edge cases: text length 0 should return an empty string, and a source that doesn't appear should return the text unchanged. Also watch out if the source and target are identical, which should just return the same string. Don't reach for regex, since special characters like a dot or star in the source would be treated as patterns. The built-in replace works too, but the manual pass shows you understand it. If your mind goes blank mid-assessment, StealthCoder is the hedge that gives you the pass-and-join approach instantly.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Replace Characters in a Mutable String 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 StealthCoder

Related leaked OAs

⏵ The honest play

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.

Replace Characters in a Mutable String FAQ

How hard is the Bloomberg replace characters question really?+

It's easy. One pass over the string, swap matches, return the result. The difficulty is in not overthinking it and in handling large inputs efficiently. If you finish the core logic in minutes, spend the rest of the time on edge cases like empty input.

What's the trick to this problem?+

Build the output in a list or string builder and join once at the end. Repeated concatenation can be quadratic in some languages. With text up to 10^6 characters, that's the one way to get a timeout on an otherwise trivial problem.

Can I just use the built-in replace function?+

Usually yes, and it's correct for a plain character swap. Avoid regex-based replace, because characters like a dot or plus have special meaning there. If the assessment is meant to test string handling, a manual loop is the safer, more explainable choice.

What edge cases should I test?+

Test an empty text, a source character that doesn't appear, text made entirely of the source character, and source equal to target. Also test a special character like a period as the source. These cover nearly every way a quick solution goes wrong.

How do I prepare for this in 48 hours?+

Don't study this one. Write it once in your language, confirm how string building works there, and run the edge cases. Then spend the remaining time on harder string and hash map problems, since Bloomberg OAs often pair an easy question with a tougher one.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it