You maintain a bounded history of the most recent k integers added. This is NOT a fixed sliding step over a whole array; it simply grows one ADD at a time and a QUERY asks about whatever is currently held.
Process n operations, in order:
ADD x - record integer x as the newest value. If the history already holds k values, the oldest one is discarded first.QUERY - print the maximum value currently stored in the history. If the history is empty, print NONE instead.Print one line of output per QUERY operation, in the order they occur.
Line 1: two integers n and k.
Lines 2..n+1: one operation per line, either ADD x or QUERY.
One line per QUERY operation: either the current maximum, or NONE.
QUERY operation appears in the input.Example 1
Input
5 2 ADD 3 ADD 7 QUERY ADD 2 QUERY
Expected
7 7
Explanation
After adding 3 and 7 the last-2 history is [3,7] with max 7. Adding 2 evicts the oldest value (3), leaving [7,2], whose max is still 7.
Example 2
Input
3 3 QUERY ADD 5 QUERY
Expected
NONE 5
Explanation
The first QUERY happens before anything is added, so history is empty and the answer is NONE. After adding 5, the next QUERY reports 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →