โ† Morgan Stanley questions
CodingReported in a candidate post, Aug 2018

Evaluate bracketed arithmetic expression indices on array

Filed under Morgan Stanley

Free read 2 of 3

Determine 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

  1. Determine if expressions can be nested and how array lookups are denoted.

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.