Using Both Ends of a Deque
A double-ended queue supports insertion and removal at the front and back. A useful interface can restrict that flexibility to express a policy.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace two arrival classes
Suppose ordinary jobs enter at the back and an urgent job enters at the front. From ordinary A and B, inserting urgent C produces processing order C, A, B. This changes the normal FIFO queue behavior.
Check fairness among urgent jobs
Inserting D at the front next would place D before C. If urgent jobs must preserve arrival order among themselves, use distinct queues or another documented policy. Function contracts can name which order callers receive.[1]
Choose the end that matches each operation
For a sliding-window maximum, keep candidate indices in increasing order and their values in decreasing order. Remove expired indices from the left. When a new value arrives, remove smaller candidates from the right because the newer larger value outlives them. See running minima for the related idea of retaining an extreme.
Values [3, 1, 4, 2], width 2
After indices 0, 1: deque [(0,3), (1,1)], maximum 3
At 2: expire 0, discard 1, insert (2,4), maximum 4
At 3: insert (3,2), maximum 4
Maintain index information when values repeat
Two equal readings can expire at different steps. Storing only their values makes expiry ambiguous unless you also store counts or positions. Test windows of length one and duplicate maxima at opposite window boundaries.