Implement nested template string substitution
Company: Waymo
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Onsite
Quick Answer: This question evaluates understanding of string processing, dependency resolution, recursive expansion, cycle detection, and efficiency techniques such as memoization for nested template substitution.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ({'X':'a','Y':'b','Z':'%X% and %Y%'}, '%X% and %Z%')
Expected Output: 'a and a and b'
Explanation: Prompt example.
Input: ({'A':'%B%','B':'%A%'}, '%A%')
Expected Output: '<CYCLE:A>'
Explanation: Cycle marker.
Input: ({'A':'hi'}, '%A% %MISSING%')
Expected Output: 'hi %MISSING%'
Explanation: Missing key is preserved.
Hints
- Choose a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.