String Pattern Replacement
Updated
Problem statement
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[][]) → StringExamples
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
sbelongs 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.
More Google Onsite Interview questions
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Portfolio Valuation, Part 1Stripe · PHONE SCREENStripe interview questions
- Format a Signed Integer with CommasHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Run-Length DecodingApple · PHONE SCREENApple interview questions
- Trace Pointer Rebinding and Pointee UpdatesArista Networks · PHONE SCREENArista Networks interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Decode and Store StringsAirbnb · PHONE SCREENAirbnb interview questions