Q233FreeSystemVerilog
Trailing sliding window maximum
Interview prompt
Question
Change the window direction: for every index i, compute the maximum from a[max(0, i - K + 1)] through a[i], using a truncated window near the beginning.
Starting point
Question code
function automatic void sliding_window_max_trailing(
int k,
ref int a[],
ref int out[]
);Reviewed example
Trace one case
Input
a=[1,3,-1,-3,5,3,6,7]; K=3Expected output
[1,3,3,3,5,5,6,7]Each output covers the current value and at most two predecessors; expired deque indices are removed before reporting the front.
What to cover
Requirements
- Return one output per input index.
- Evict indices that are no longer in the trailing window.
- Maintain remaining candidates in decreasing-value order.
- Run in O(n) time.

