Resolve a Variable Through Literal and Reference Assignments
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Resolve a target variable through an acyclic chain of named assignments until reaching an integer literal, independent of definition order.
Read the full Instacart Software Engineer interview experience this question came from
Constraints
- 1 <= len(assignments) <= 100000.
- Each assignment is [variable_name, right_hand_side].
- The order of assignments is arbitrary.
- 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 and no plus sign; leading zeros are allowed.
- Literal values are in [-1000000000, 1000000000].
- Variable names are unique, and target is 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: ([["value", "0"]], "value")
Expected Output: 0
Explanation: Minimum valid input: one assignment whose right-hand side is already the literal 0.
Input: ([["T1", "T2"], ["T2", "-8"], ["unused", "4"]], "T1")
Expected Output: -8
Explanation: T1 references T2 and T2 is the literal -8; the unrelated assignment 'unused' is never visited.
Hints
- The assignments arrive in arbitrary order, so the definition you need next may sit anywhere in the list; think about what you want in place before you start following anything.
- You can tell a right-hand side apart from a variable name by its first character alone: a name may never begin with a digit or a minus sign.
- A chain can be as long as the number of assignments, and leading zeros or a minus sign are part of a perfectly ordinary literal.