PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

Evaluate variable assignments whose expressions may depend on definitions appearing later in the input. This algorithmic exercise checks safe parsing, dependency ordering, memoization or graph evaluation, preservation of original output order, wide integer arithmetic, and deep-chain robustness.

  • medium
  • Applied
  • Coding & Algorithms
  • Software Engineer

Evaluate Dependent Variable Assignments

Company: Applied

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Evaluate Dependent Variable Assignments ### Problem Implement `evaluate_assignments(expressions)`. Each input string assigns one variable to a sum of integer literals and other variables. Assignments may refer to variables defined later in the array. Evaluate every variable and return the results in the original assignment order. ### Function Contract ```text evaluate_assignments(expressions) -> evaluated ``` `evaluated` is a JSON array of `[variable_name, value]` pairs. Pair `i` must contain the variable assigned by `expressions[i]` and its final integer value. ### Expression Grammar - A variable name matches `[a-z][a-z0-9_]{0,31}`. - Each string has the form `variable = term + term + ...` with one or more terms. - A term is either a nonnegative decimal integer or a variable name. - ASCII spaces may appear around `=` and `+`; there are no other operators or parentheses. - Every variable is assigned exactly once. - Every referenced variable has an assignment somewhere in the input. - The dependency graph is acyclic, so every value is well defined. ### Examples ```text expressions = ["foo = bar + 5", "bar = 2", "abc = 3"] result = [["foo", 7], ["bar", 2], ["abc", 3]] ``` ```text expressions = ["g = abc + foo", "foo = bar + 5", "bar = 2", "abc = 3"] result = [["g", 10], ["foo", 7], ["bar", 2], ["abc", 3]] ``` ```text expressions = ["total=left+right+4", "right=6", "left=base+1", "base=2"] result = [["total", 13], ["right", 6], ["left", 3], ["base", 2]] ``` ### Requirements - `1 <= expressions.length <= 200,000`. - The total number of characters across all strings is at most `2,000,000`. - The total number of terms is at most `400,000`. - Every integer literal is at most `1,000,000,000`. - Every final value is at most `9,000,000,000,000,000`, so all arithmetic is exact in signed 64-bit integers and JavaScript safe integers. - Do not mutate `expressions`. - Target `O(C + V + E)` time and `O(C + V + E)` space, where `C` is the input character count, `V` the number of variables, and `E` the number of variable references. - An implementation must not rely on recursive call depth proportional to `V` when the language runtime has a shallow recursion limit. ```hint Do not trust input order A variable can depend on a line that appears later. Preserve output order separately from the order in which values become known. ``` ### Discussion Requirements 1. Explain why evaluating each line once from left to right fails on the second example. 2. Describe how memoized dependency evaluation or an equivalent topological method avoids repeated work. 3. Explain how to distinguish a numeric literal from a variable token without evaluating arbitrary code. 4. State how an iterative traversal can avoid stack overflow on a long dependency chain.

Quick Answer: Evaluate variable assignments whose expressions may depend on definitions appearing later in the input. This algorithmic exercise checks safe parsing, dependency ordering, memoization or graph evaluation, preservation of original output order, wide integer arithmetic, and deep-chain robustness.

Implement evaluate_assignments(expressions). Each input string assigns one variable to a sum of one or more terms, where a term is either a nonnegative decimal integer or another variable. A variable name matches [a-z][a-z0-9_]{0,31}. Each assignment has the form variable = term + term + ...; ASCII spaces may appear around = and +, and there are no other operators or parentheses. Every variable is assigned exactly once, every referenced variable is assigned somewhere in the input, and the dependency graph is acyclic. References may point to later assignments. Return a JSON array whose item i is [variable_name, final_value] for expressions[i]. Do not mutate expressions or evaluate arbitrary code.

Constraints

  • 1 <= expressions.length <= 200000
  • Total input characters are at most 2000000 and total terms are at most 400000
  • Variable names match [a-z][a-z0-9_]{0,31}
  • Each assignment contains one or more nonnegative integer or variable terms separated by +, with optional ASCII spaces around = and +
  • Every variable is assigned once, every reference is defined, and the dependency graph is acyclic
  • Each literal is at most 1000000000 and every final value is at most 9000000000000000

Examples

Input: (['foo = bar + 5', 'bar = 2', 'abc = 3'],)

Expected Output: [['foo', 7], ['bar', 2], ['abc', 3]]

Explanation: The forward reference to bar resolves before foo is finalized.

Input: (['g = abc + foo', 'foo = bar + 5', 'bar = 2', 'abc = 3'],)

Expected Output: [['g', 10], ['foo', 7], ['bar', 2], ['abc', 3]]

Explanation: Dependencies are evaluated independently of assignment order.

Hints

  1. Separate the order used for returning results from the order in which dependencies become resolved.
Last updated: Aug 6, 2026

Loading coding console...

PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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

  • Resolve Dependent Addition Equations - Applied (hard)
  • Multi-Agent Collision Simulator (Unicycle Kinematics) - Applied (medium)
  • Merge Overlapping Collinear Segments - Applied (hard)
  • Implement a Fixed-Capacity Deque - Applied (medium)