String Pattern Replacement
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's String Pattern Replacement showed up in July 2026, and the edge case that breaks a naive solution is hiding in Example 3. If you loop over the rules and call replace on the string, the value %b% inserted for %a% gets expanded on the next pass. The expected output says it shouldn't be. This is a string parsing problem dressed up as a find-and-replace. It's short, it looks easy, and it punishes the lazy approach. If you blank during the live OA, StealthCoder runs invisibly as a safety net and gives you the single-pass solution.
The problem
Given a string s and a list of replacement rules, replace every named placeholder in s with its corresponding value. Each placeholder has the form %key%. Each rule is a two-element array [key, value]. Every placeholder key in s has exactly one rule. Replacement values are inserted literally: any percent-delimited text inside a replacement value is not processed again. Function replacePlaceholders(s: String, replacements: String[][]) → String Examples Example 1 s = "abc%x%123" replacements = [["x","hello"]] return = "abchello123" The placeholder %x% uses the rule x -> hello. Example 2 s = "%first%-%last%-%first%" replacements = [["first","Ada"],["last","Lovelace"]] return = "Ada-Lovelace-Ada" Repeated placeholders use the same rule each time. Example 3 s = "%a%%b%" replacements = [["a","%b%"],["b","done"]] return = "%b%done" The two placeholders are adjacent. The value %b% inserted for %a% is copied literally rather than expanded recursively. Constraints 1 <= s.length <= 100000. 1 <= replacements.length <= 10000. Each rule contains a unique, non-empty key and a value. Keys contain only lowercase English letters, digits, and underscores. Every percent sign in s belongs to a well-formed placeholder %key%, and every such key has exactly one replacement rule. The total length of all keys and values is at most 100000. The final output length is at most 1000000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one left-to-right scan with a hash map. Build a dictionary from key to value first. Then walk s with an index. Copy normal characters into a list. When you hit a percent sign, find the next percent sign, slice out the key between them, look it up, append the value, and jump the index past the closing percent. Never rescan the output. That's what keeps inserted text literal. The pitfall is sequential str.replace over the rules. It re-processes inserted values and gives the wrong answer on Example 3. It's also slow on a 100000 character input. Use a list and join once at the end, not repeated string concatenation. Time is linear in the input plus output size. Adjacent placeholders like %a%%b% work fine, because after closing a placeholder the next character is the opening percent of the next one. StealthCoder is your hedge if the parsing loop gets tangled live.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill String Pattern Replacement 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
String Pattern Replacement FAQ
What's the trick in Google's String Pattern Replacement?+
Do a single left-to-right pass and never rescan what you've already written. Put the rules in a hash map, copy plain characters, and when you see a percent, read to the next percent, look up the key, append the value, and skip ahead. That keeps inserted values literal.
Why does calling replace for each rule fail?+
Sequential replace re-processes text you just inserted. In Example 3, replacing %a% puts %b% into the string, and the next rule then expands it into done. The expected output keeps %b% literal. A single pass avoids this because inserted text is never read again.
How hard is this problem really?+
Easy to medium. There's no fancy algorithm, just careful parsing. The difficulty is index handling around the percent delimiters and resisting the replace shortcut. If you've written a tokenizer before, it's about fifteen minutes. The constraints guarantee well-formed input, so you skip most error handling.
What's the time and space complexity?+
Building the map costs the total length of keys and values. The scan is linear in the length of s plus the output length. Space is the map plus the output buffer, which can reach 1000000 characters. Use a list of chunks and join once so you don't hit quadratic string building.
How do I prepare for this in 48 hours?+
Write the scan loop from scratch twice, then test Example 3 and adjacent placeholders like %a%%b%. Also try a string that is only one placeholder. Those cases cover the logic. Don't spend time on regex, since a manual index loop is simpler and easier to get right under pressure.