FastPrepFastPrep
Problem Brief

Substitute Templates

ONSITE INTERVIEW

Write a tool which supports substitutions of templates by other strings.

Function Description

Complete the function substituteTemplates in the editor.

substituteTemplates has the following parameters:

  1. 1. Map substitutions: a map containing the substitutions
  2. 2. String template: the template string to be resolved

Returns

String: the resolved template

1Example 1

Input
substitutions = {"X" -> "123", "Y" -> "456", "Z" -> "abc"}, template = "%X%_%Y%"
Output
"123_456"
Explanation
The template %X%_%Y% should be resolved by replacing %X% with 123 and %Y% with 456, resulting in 123_456.

2Example 2

Input
substitutions = {"X" -> "123", "Y" -> "456", "Z" -> "abc%Y%"}, template = "%X%_%Y%_%Z%"
Output
"123_456_abc456"
Explanation
The template %X%_%Y%_%Z% should be resolved by replacing %X% with 123, %Y% with 456, and %Z% with abc%Y% which further resolves to abc456. The final result is 123_456_abc456.
public String substituteTemplates(Map<String, String> substitutions, String template) {
  // write your code here
}
Input

substitutions

{"X" -> "123", "Y" -> "456", "Z" -> "abc"}

template

"%X%_%Y%"

Output

"123_456"

Sign in to submit your solution.