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?
- A. 4
- B. 3
- C. 5
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the largest rectangular area in a histogram with bar heights 6, 2, 5, 4, 5, 1, 6 (each bar has width 1)?
- In the undirected graph with edges (1,2), (1,3), (2,4), (3,4), (4,5), (3,6), how many vertices are at shortest-path distance exactly 2…
- What is the minimum number of knight moves needed to go from square (0, 0) to (3, 3) on an 8 by 8 board (coordinates 0 to 7)?
- Two sorted linked lists are [1, 5, 9, 12] and [2, 3, 8]. They are merged into one sorted list. What is the 5th value of the merged list…
- In the array [2, 8, 5, 5, 3, 7, 5], how many index pairs i < j have a[i] + a[j] = 10?
- How many distinct sums are obtained by contiguous non-empty subarrays of [1, -1, 2, -2]?