Evaluate Sequential Variable Assignments
Updated
Problem statement
You are given arrays names and expressions. Process them from first to last. At index i, calculate expressions[i] using the current variable values, then store the result in names[i]. Return the results in assignment order.
An expression contains nonnegative decimal integer literals, lowercase variable names, optional unary + or - before an operand, and binary +, -, *, and /. Spaces may appear between tokens. Parentheses are absent.
Calculate multiplication and division before addition and subtraction. Calculate operators with the same priority from left to right. For integer division, remove the fractional part so the result rounds toward zero.
Every referenced variable has already been assigned. You may assign a variable again. Its expression uses the old value before the new value is stored. Every expression is valid, division by zero never occurs, and all intermediate values fit the stated bounds.
Function
evaluateAssignments(names: String[], expressions: String[]) → int[]Examples
Example 1
names = ["a","b","c"]expressions = ["2 + 3 * 4","a / 3","b * -2 + a"]return = [14,4,6]a is 14, b is 14 / 3 = 4, and c is 4 * -2 + 14 = 6.
Example 2
names = ["x","x","y"]expressions = ["7","x + 1","-x / 3"]return = [7,8,-2]The second assignment reads the old x, then sets it to 8. Division of -8 by 3 truncates to -2.
Example 3
names = []expressions = []return = []No assignments produce no values.
Constraints
0 <= names.length == expressions.length <= 10000.- Variable names contain 1 through 20 lowercase English letters.
- Each expression is nonempty; the total expression length is at most
200000. - Each operand has at most one unary sign. Signs are separate tokens even when adjacent to a binary operator.
- Integer literals and the absolute value of every intermediate result are at most
10^9. - Variable references always name a previously assigned variable.
More Google Onsite Interview questions
- Minimum Piano Hand Raises and Thumb PositionsONSITE INTERVIEW · Seen Dec 2026
- Swap Adjacent in LR StringONSITE INTERVIEW · Seen Dec 2026
- Top Ten Largest FilesPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Count Winning Dice Face PairsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find a Failing Test PairONSITE INTERVIEW · Seen Nov 2026
- Minimum Cars for Rental RequestsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- JSON Schema Object ValidatorDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Number of AtomsAmazon · ONSITE INTERVIEW
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions