Data Structures & AlgorithmsHardGATE CS · DSA
For each element of [23, 12, 7, 21, 25, 8, 13, 1], find the next greater element to its RIGHT (−1 if none), using a monotonic stack. What is the output?
- A. [−1, 23, 12, 23, −1, 25, 25, 13]
- B. [25, 21, 21, 25, −1, 13, −1, −1]
- C. [25, 25, 25, 25, −1, 13, −1, −1]
- D. [−1, −1, 21, 25, −1, 13, −1, −1]
Correct answer: B. [25, 21, 21, 25, −1, 13, −1, −1]
Scan left to right keeping a decreasing stack of indices; when a larger value arrives it resolves every smaller element on the stack. The 'first larger value to the right' is needed — not the largest, and not just the immediate neighbour.
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
- Reverse the order of words, collapsing multiple spaces. What is printed? python s = " oil is data new " w = s.split() print("|" + "…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…
- ^ is exponentiation (highest precedence, right-associative); and / come next; + and − are lowest; others are left-associative. What is the…
- Keys 93, 31, 142, 74, 55, 139, 99 are inserted in this order into an empty table of size 11 by double hashing: h1(k) = k mod 11, h2(k) = 5…
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "dbcedadaadcced" with all characters…
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [20…