Reported July 2026
Googledepth first search

Template Variable Expansion

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

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

The detail that matters in this Google OA question, reported in July 2026, is that a mapped value can itself contain another %key% placeholder. So a single find-and-replace pass won't cut it. You're expanding templates recursively, and you have to return "ERROR" for a missing key or a cycle. It's a hash map plus depth-first resolution problem dressed up as string work. If the assessment is in the next day or two, learn the visiting-state trick below and you're most of the way there. And if your mind goes blank mid-assessment, StealthCoder runs invisibly on your screen and gives you a working solution as a safety net.

The problem

You are given key-value mappings and a source string. Any substring inside a pair of percent signs, such as %key%, should be treated as a key and replaced by its mapped value.
A mapped value may itself contain another key placeholder, so replacements may need to be resolved recursively.
If a referenced key is missing, or if resolving placeholders finds a cycle, return "ERROR".

Function
expandTemplate(mappings: String[][], source: String) → String

Examples
Example 1
mappings = [["x","%y%/home"],["y","user"]]
source = "/%x%/docs"
return = "/user/home/docs"
%x% expands to %y%/home, and %y% expands to user.
The source shared the rule but did not include this exact sample. FastPrep added this small example so the behavior can be checked directly.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a hash map from the mappings. Then write a resolve function that scans a string, finds each %key% pair, and replaces it with the fully expanded value of that key. Memoize finished keys so you don't redo work. The cycle check is the real trick. Keep a set of keys currently on the recursion stack. If you hit a key already in that set, return ERROR. A plain visited set is wrong, because two separate references to the same key aren't a cycle. Pitfalls: unmatched percent signs, empty keys, and forgetting to propagate ERROR up through every level. Decide up front how a lone % behaves, and stay consistent. If you freeze on the stack-versus-visited distinction live, StealthCoder is the hedge that shows you the clean version while you keep your own pace.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Template Variable Expansion 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 would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Template Variable Expansion FAQ

What's the trick in the Google template expansion problem?+

Recursive resolution with cycle detection. Store mappings in a hash map, expand each key by recursing into its value, and track keys currently being resolved. If you see one again before it finishes, that's a cycle, so return ERROR.

How do I detect a cycle correctly?+

Use an in-progress set that you add to on entry and remove from on exit. A permanent visited set gives false positives when the same key appears twice in a string, like %a%%a%. Only a key still on the current stack means a real cycle.

Should I memoize expanded values?+

Yes. Once a key is fully expanded, cache the result so repeated references don't recompute. It matters when many keys point to shared values. Don't cache partial results or ERROR states inconsistently, or you'll get wrong answers on later lookups.

What edge cases should I test before submitting?+

A missing key, a self-reference like a maps to %a%, a two-key cycle, a key used twice in the source, and a source with no placeholders. Also decide what happens with an unmatched percent sign and check that ERROR propagates from nested levels.

How do I prepare for this in 48 hours?+

Write it once from scratch: map, recursive resolve, in-progress set, memo. Then run the example where x expands to %y%/home and y is user. Practice string scanning with indexes, since parsing between percent signs is where most bugs show up.

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

OA at Google?
Invisible during screen share
Get it