Evaluate an Expression with Custom Precedence
Updated
Problem statement
Evaluate the valid expression expression using operator priorities supplied by priority. For this exercise, the only binary operators are +, - and *. The three-character priority string lists them from highest to lowest precedence, with each appearing once.
Every operator is left-associative: repeated operators at the same precedence are evaluated from left to right. Use the supplied priorities rather than the usual arithmetic priorities. The expression contains nonnegative decimal integer literals and these binary operators, with optional ASCII spaces around tokens. Leading zeros are allowed. There are no unary operators or parentheses.
Return the exact signed 64-bit integer result. Intermediate results may be negative. The practice bounds ensure every intermediate and final value fits that range. Do not use a host-language expression-evaluation function.
Function
evaluateExpression(expression: String, priority: String) → longExamples
Example 1
expression = "2+3*4"priority = "+*-"return = 20Priority +*- makes addition higher than multiplication. Reduce 2+3 to 5, then compute 5*4=20. The usual-precedence result 14 would be wrong here.
Example 2
expression = "9-3-2"priority = "*+-"return = 4Only subtraction is present, and it is left-associative. Compute (9-3)-2=4, rather than 9-(3-2)=8.
Constraints
- For this exercise, assume 1 <= expression.length <= 200.
- priority has exactly 3 characters and is a permutation of +,-,* from highest to lowest precedence.
- The valid flat expression has 1 through 6 decimal integer operands, each between 0 and 1000, alternating with binary +,-,* operators. Optional ASCII spaces surround tokens; leading zeros are allowed, and unary operators and parentheses are absent.
More Morgan Stanley Onsite Interview questions
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
- Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
- Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
- Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
- Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
- LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
- All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Number Words to Digits in Any LanguageMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Inorder Tree String EquivalenceGoogle · ONSITE INTERVIEWGoogle interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)