FastPrepEvaluate an Expression Map
Problems/Evaluate an Expression Map

Evaluate an Expression Map

Harvey logoHarvey● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

You are given symbol definitions in the form name=expression. An expression may contain integer constants, references to previously defined or later defined symbols, and the binary operators +, -, *, and /.

Evaluate the final integer value of every symbol. Return the results in the same order as the input definitions, formatted as "name=value".

If the dependency graph contains a cycle, return a single-element array ["CYCLE"] instead of partial results.

Function

evaluateExpressionMap(definitions: String[]) → String[]

Complete the function evaluateExpressionMap in the editor below.

evaluateExpressionMap has the following parameter:

  1. String[] definitions: the symbol definitions

Returns String[]: evaluated symbol assignments in input order, or ["CYCLE"] if a cycle exists.

Examples

Example 1

definitions = ["a=1", "b=a+2", "c=b*3"]return = ["a=1", "b=3", "c=9"]

Each symbol depends only on earlier evaluated values, so the map resolves cleanly.

Example 2

definitions = ["a=b+1", "b=a+1"]return = ["CYCLE"]

The definitions form a cycle, so the special cycle marker is returned.

Constraints

  • 1 <= definitions.length <= 10^5
  • Expressions use integer arithmetic and may reference other symbols.
  • Use cycle detection to avoid infinite recursion.

More Harvey problems

  • Highlight and Rank Overlapping CitationsONSITE INTERVIEW · Seen Nov 2026
  • In-Memory Vault File SystemONSITE INTERVIEW · Seen Nov 2026
  • Spreadsheet Formula EvaluatorPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
All Harvey interview questionsSee Harvey hiring insights
Saved
class Solution {
    public String[] evaluateExpressionMap(String[] definitions) {
        // write your code here
    }

}
Checking account…