Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE
A monotonic decreasing stack of indices is used to compute the next-greater-element array for [48, 12, 29, 6, 30, 28] (push index i after popping all stack-top indices whose value is less than arr[i]). How many indices remain on the stack after the whole array is processed?
- A. 6
- B. 4
- C. 2
- D. 3
Correct answer: D. 3
These are exactly the elements with no next-greater element to their right, 3 of them in this array.
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
- A singly linked list is 47 -> 24 -> 78 -> 79 -> 98 -> 29 -> 13 -> None. The standard iterative in-place reversal (prev = None; while cur…
- Using the sliding window technique, what is the maximum sum of any 8 consecutive elements in [24, 30, 4, 27, 25, 2, 13, 22, 26, 1]?
- A singly linked list has nodes at positions 0..6 holding 58 -> 20 -> 49 -> 17 -> 78 -> 37 -> 22, but the last node's next pointer points…
- Heap sort (build a max-heap, then repeatedly swap the root with the last unsorted element and sift down) is run on [1, 27, 40, 40, 63, 2…
- A monotonic decreasing stack of indices is used to compute the next-greater-element array for [41, 35, 17, 34, 4, 37, 29, 44] (push index…
- What is the space complexity of this recursive factorial function, as a function of n (ignoring the final integer result size)? c int…