Data Structures & AlgorithmsVery hard
A monotonic deque for sliding-window maximum stores array INDICES instead of values. Why?
- A. Indices are needed to compare values faster than the values themselves.
- B. Indices let the algorithm detect when the front element has slid out of the window (its index is too small).
- C. Only indices can be stored in a queue.
- D. Indices remove the need to pop smaller elements.
Correct answer: B. Indices let the algorithm detect when the front element has slid out of the window (its index is too small).
Each stored index can be compared against the current window start to decide whether to evict the front, and the values are still read from the array. Storing values alone would make expiry impossible to detect.
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
- When tracking a '' wildcard that may be '(', ')' or empty while scanning brackets, why is it enough to keep a range [lo, hi] of possible…
- Two different strings produced equal rolling-hash values. What does this tell you?
- In iterative reversal of a singly linked list, which pointer must be saved BEFORE the current node's next pointer is changed?
- For sorted positive integers, the smallest value that cannot be formed as a subset sum equals R+1, where R is the reach. Why does a value…
- Items (weight, value) are [(5, 60), (4, 40), (6, 50), (3, 30), (2, 15), (7, 70)]. Each item can be taken at most once, and the total…
- A shop has these items as (weight, value, number available): [(2, 10, 2), (3, 14, 3), (5, 30, 1)]. You may take at most the listed number…