Practice/Google/String Resolution and Variable Update
CodingOptional
Design a system that manages string variables with dynamic resolution capabilities. Your system needs to handle two core operations:
%) and replace them with their actual values. Variables can reference other variables, requiring recursive resolution.For example, if you have variables x = 'hello' and y = '%x world', resolving %y should produce 'hello world' by first resolving %x within the value of y.
resolveString(s, variables) that takes an input string and a dictionary of variables, returning the fully resolved string% followed by the variable name (single character or word)updateVariable(var, value, variables) to modify variable values% prefix in their values are literal stringsExample 1:
Variables: \{x: 'tech', y: 'interview'\} Input: "Welcome to %x %y" Output: "Welcome to tech interview" Explanation: Direct substitution of %x and %y with their values
Example 2:
Variables: \{x: 'Python', y: '%x programming', z: 'I love %y'\} Input: "%z" Output: "I love Python programming" Explanation: %z resolves to 'I love %y', which then resolves %y to '%x programming', finally resolving %x to 'Python'
Example 3:
Variables: \{x: 'old'\} After updateVariable('x', 'new', variables) Variables: \{x: 'new', y: '%x data'\} Input: "%y" Output: "new data" Explanation: After updating x, subsequent resolutions use the new value
Example 4:
Variables: \{x: 'test'\} Input: "No variables here" Output: "No variables here" Explanation: Strings without variable references return unchanged