Dsa
Queue
Hard
The Cascading Overflow Buffer
A network switch has two buffers: Primary (size P) and Secondary (size S).
1. When a packet arrives, it tries to enter the Primary buffer (FIFO).
2. If the Primary buffer is full, the *oldest* packet in the Primary buffer is forced out and moved to the Secondary buffer to make room for the new arrival at the end of Primary.
3. If the Secondary buffer is also full when a packet is forced into it, the oldest packet in the Secondary buffer is dropped (deleted).
4. Periodically, the system 'processes' a packet: it takes the oldest packet from the Primary buffer. If Primary is empty, it takes from Secondary.
Input: P (Primary size), S (Secondary size), N (number of operations). Operations are 'A ID' (Arrival of packet ID) or 'P' (Process a packet).
Output: For every 'P' operation, print the ID processed or 'NONE' if both are empty. Finally, print 'DROPPED:' followed by IDs of dropped packets.
Example:
Input:
2 1 5
A 1
A 2
A 3
P
P
Output:
2
3
DROPPED: 1
Key concepts
queuesimulationbuffer management
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now