Coding questions · Meta
How do you expand nested placeholders in strings?
This intermediate-level Coding & Algorithms problem for Data Scientist roles evaluates string parsing and manipulation skills along with dependency resolution techniques such as recursion or graph traversal, cycle detection, and memoization.
You are given a dictionary of string templates. Keys are identifiers like X, Y, Z. A template may contain placeholders of the form %KEY%, which should be replaced by the fully-expanded value of KEY.
Example dictionary:
X -> "a"Y -> "b"Z -> "%X% and %Y%"
Given an input string that may also contain placeholders (e.g., "%X% and %Z%"), return the fully expanded string.
Example:
- Input:
"%X% and %Z%" - Output:
"a and a and b"
Assumptions/requirements to clarify in your solution:
- Templates can reference other templates (nested expansion).
- Decide how to handle missing keys and cyclic references (e.g.,
A -> "%B%",B -> "%A%"). - Provide time/space complexity for your approach.
Expand Nested Placeholders
Expand %KEY% placeholders recursively. Missing keys stay unchanged; cycles become CYCLE:key.
Hints
- Model object-style prompts as operation streams when needed.
- Handle empty and boundary cases before the main logic.