Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
Using a monotonic (increasing) stack to find, for each element of [20, 17, 18, 14, 44, 28], the nearest element to its left that is strictly smaller (or -1 if none), what is the resulting array?
- A. [44, 18, 44, 44, -1, -1]
- B. [-1, -1, 17, -1, 14, 14]
- C. [14, 14, -1, 17, -1, -1]
- D. [14, -1, -1, -1, 14, 17]
Correct answer: B. [-1, -1, 17, -1, 14, 14]
Scan left to right, popping stack entries that are >= the current value, then the new top (if any) is the answer for that position: [-1, -1, 17, -1, 14, 14]
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 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…
- 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…