Resolve a Variable Through Literal and Reference Assignments
InstacartSoftware Engineer · OnsiteLast updated
Resolve a target variable through an acyclic chain of named assignments until reaching an integer literal, independent of definition order.
Resolve a variable whose assignment is either an integer literal or the name of another variable.
Function Signature
resolve_variable(assignments: list[list[str]], target: str) -> int
Input
Each assignment is [variable_name, right_hand_side]. A right-hand side is either a decimal integer literal or another variable name. The order of assignments is arbitrary.
For this exercise, names match [A-Za-z_][A-Za-z0-9_]*. Integer literals use an optional minus sign followed by one or more digits, with no whitespace or plus sign. Literal values are in [-1000000000, 1000000000].
Output
Follow references beginning at target and return the terminal integer value.
Constraints
1 <= len(assignments) <= 100000.- Variable names are unique, and
targetis defined. - Every referenced variable is defined.
- The complete reference graph is acyclic, so every chain ends in an integer literal.
- Each string has at most 40 characters.
- These validity rules are explicit baseline assumptions; malformed definitions and cycles are outside this console contract.
Examples
Input: assignments = [["T1","T2"],["T2","-8"],["unused","4"]], target = "T1"
Output: -8
Input: assignments = [["A","007"],["B","A"]], target = "B"
Output: 7
Input: assignments = [["value","0"]], target = "value"
Output: 0
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.