Resolve a Variable Through Literal and Reference Assignments

Read the full interview experience this question came from →

Quick Overview

Resolve a target variable through an acyclic chain of named assignments until reaching an integer literal, independent of definition order.

Resolve a Variable Through Literal and Reference Assignments

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

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 `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: `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`

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

|Home/Coding & Algorithms/Instacart
Instacart logo
Instacart
Sep 11, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

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 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: 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

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...