Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

The shunting-yard algorithm converts the infix expression "a+b*c^d^e-f^(g*h)" to postfix, with ^ highest precedence and right-associative, then * and /, then + and −, all left-associative. What is the largest number of operators that sit on the operator stack at the same time?

  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 7

Correct answer: A. 4

When an operator arrives, pop the stacked operators that bind at least as tightly (for ^, only strictly tighter, since it is right-associative). Treating ^ as left-associative pops earlier and shrinks the stack; the peak is the number to read off, not the final operator count.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic