PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to parse and evaluate assignment expressions, resolve inter-variable dependencies, and detect cycles or unsolvable references.

  • Medium
  • Instacart
  • Coding & Algorithms
  • Software Engineer

Evaluate dependent variable expressions

Company: Instacart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

##### Question Given a target variable name and a list of assignment strings of the form 'Ti = expression' where the expression is either a number, a single variable, or arithmetic (+, −) over variables/numbers, compute the numeric value of the target variable. Extend to handle expressions that reference other variables recursively; detect cycles or unsolvable references and report an error.

Quick Answer: This question evaluates a candidate's ability to parse and evaluate assignment expressions, resolve inter-variable dependencies, and detect cycles or unsolvable references.

You are given a target variable name and a list of assignment strings of the form 'name = expression'. Each expression consists only of integer literals and variable names combined using '+' and '-' with optional whitespace; no parentheses or other operators exist. A unary '+' or '-' may precede any term. Variable names match [A-Za-z_][A-Za-z0-9_]* and are case-sensitive. Evaluate the target variable by recursively resolving dependencies. If a variable is assigned multiple times, the last assignment takes precedence. Return the integer value of the target if it can be computed. Return 'ERROR' if there is a cyclic dependency, if the target or any referenced variable is undefined, or if an expression is syntactically invalid.

Constraints

  • 1 <= len(assignments) <= 10^4
  • Total length of all assignment strings <= 2 * 10^5
  • Variable names match [A-Za-z_][A-Za-z0-9_]*
  • Expressions contain only integers, variable names, '+', '-', and spaces; no parentheses
  • A leading '+' or '-' may appear before any term (number or variable)
  • If a variable is assigned multiple times, the last assignment is used
  • Return 'ERROR' on cyclic dependencies or undefined variables

Examples

Input:

Expected Output: ERROR

Input:

Expected Output: ERROR

Hints

  1. Parse assignments into a map from variable name to its expression; let the last occurrence win.
  2. Use DFS with memoization to evaluate each variable once; keep a 'visiting' set to detect cycles.
  3. Scan expressions character-by-character: read optional '+'/'-' signs to determine the term's sign, then read either an integer literal or a variable name.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Fix the Broken Search Filter in a Book Catalog API - Instacart (medium)
  • Find Largest Adjacent Stock Price Change - Instacart (medium)
  • Implement an In-Memory File Storage System - Instacart (medium)
  • Decode an encoded string - Instacart (medium)
  • Evaluate an arithmetic expression - Instacart (medium)