You maintain a fixed-capacity circular buffer of capacity c that starts empty. You are given n operations, each one of:
PUSH x - insert integer x at the back of the buffer. If the buffer already holds c elements, first drop the oldest element (the one that has been in the buffer the longest) to make room, then insert x.POP - remove the oldest element from the buffer. If the buffer is empty, this operation does nothing.After processing all n operations in order, print the buffer's contents from oldest to newest, space separated. If the buffer ends up empty, print EMPTY instead.
Line 1: two integers n and c.
Lines 2..n+1: one operation per line, either PUSH x or POP.
One line: the final buffer contents oldest-to-newest, space separated, or EMPTY.
Example 1
Input
4 2 PUSH 1 PUSH 2 PUSH 3 POP
Expected
3
Explanation
After pushing 1 and 2 the buffer [1,2] is full at capacity 2. Pushing 3 evicts the oldest (1), leaving [2,3]. The final POP removes 2, leaving just 3.
Example 2
Input
3 3 POP PUSH 5 PUSH 7
Expected
5 7
Explanation
The initial POP does nothing since the buffer starts empty. Pushing 5 then 7 (capacity 3, never full) leaves both, oldest to newest: 5 then 7.
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 →