Evaluate bracketed arithmetic expression indices on array
Filed under Morgan Stanley
Free read 2 of 3Determine if expressions can be nested and how array lookups are denoted.
The question
Given an integer array 'a' and a string format expression containing arithmetic operations with brackets and array access notation, evaluate the expression using standard precedence rules and return the corresponding array element index.
Constraints
- Expression string length <= 500.
- Array indices must remain within bounds.
- Supported operators: +, -, *, /.
Examples
- Input
- a = [10, 20, 30, 40], expr = "a[1+1]"
- Output
- 30
- Why
- Expression inside brackets evaluates to 2, accessing a[2] which is 30.
Approach
Determine if expressions can be nested and how array lookups are denoted.
Use eval() if permitted, though interviewers expect a manual parser using stacks.
Tokenize the expression, use Dijkstra's Shunting-yard algorithm to convert to postfix, evaluate it, and fetch from array 'a'.
Invalid index lookups, division by zero, empty expression strings.
Tips
- What they are testing: Test nested parentheses and complex operator precedence combinations.
- Common mistake: Incorrect parsing of string tokens, missing multi-digit numbers or negative sign handling.
Related Morgan Stanley questions
Collected from public candidate posts and coding forums. Updated Nov 2026.