A monitoring system keeps a circular log buffer that stores AT MOST the most recent c events (the oldest is dropped once more than c events have been logged). Process n operations, in order:
LOG x - record integer x as the newest event. If the buffer already holds c events, the oldest is dropped first.BACK j - print the value of the event that is j steps back from the newest event CURRENTLY in the buffer, where BACK 0 means the newest event itself, BACK 1 the one before it, and so on. If the buffer currently holds fewer than j + 1 events, print NONE instead.Print one line of output per BACK operation, in the order they occur.
Line 1: two integers n and c.
Lines 2..n+1: one operation per line, either LOG x or BACK j.
One line per BACK operation: either the looked-up value, or NONE.
BACK operation appears in the input.Example 1
Input
5 3 LOG 10 LOG 20 LOG 30 LOG 40 BACK 1
Expected
30
Explanation
After logging 10, 20, 30 the buffer [10,20,30] is full at capacity 3. Logging 40 evicts the oldest (10), leaving [20,30,40]. BACK 1 asks for the event one step before the newest (40), which is 30.
Example 2
Input
3 2 BACK 0 LOG 5 BACK 0
Expected
NONE 5
Explanation
The first BACK 0 happens on an empty buffer, so it prints NONE. After logging 5, BACK 0 correctly reports the newest event, 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 →