Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE
Using a monotonic (decreasing) stack to find, for each element of [32, 3, 22, 30, 36, 26, 11, 9], the next element to its right that is strictly greater (or -1 if none), what is the resulting array?
- A. [36, 30, 22, 36, -1, -1, -1, -1]
- B. [36, 22, 30, 36, -1, -1, -1, -1]
- C. [-1, -1, 3, 22, 30, 22, 3, 3]
- D. [-1, 32, 32, 32, -1, 36, 26, 11]
Correct answer: B. [36, 22, 30, 36, -1, -1, -1, -1]
Scan left to right keeping a stack of indices whose values still need a next-greater; pop and record whenever the current value exceeds the stack top: [36, 22, 30, 36, -1, -1, -1, -1]
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
- Quicksort uses the Lomuto partition with the last element as pivot on [42, 63, 77, 95, 70, 32, 1, 32, 76]. After one partition call, at…
- Evaluate the postfix expression: 5 9 + 4 2 +
- A function makes two recursive calls on an array of size n, each on a half of size n/2, and does O(n) extra work to combine the results…
- A singly linked list has nodes at positions 0..5 holding 74 -> 10 -> 74 -> 58 -> 91 -> 24, but the last node's next pointer points back to…
- Selection sort (ascending, swap the minimum of the unsorted suffix into place each pass, skipping the swap if it is already in place) is…
- How many adjacent swaps does bubble sort (ascending) make on [97, 9, 32, 5, 21, 45, 53, 34]?