Dsa
Queue
Easy
Priority Queue with Anti-Starvation
A service center has 'VIP' and 'Standard' customers. VIPs are always served first. However, to prevent standard customers from waiting forever, the '3-1 Rule' is applied: after every 3 VIP customers served, if there is a Standard customer waiting, the center *must* serve exactly one Standard customer before serving any more VIPs. This rule only triggers if both types are waiting.
Input: N (number of customers). Then N lines: 'V' (VIP) or 'S' (Standard) and the customer's name, in order of arrival.
Output: The names in order of service.
Example:
Input:
5
V Alice
V Bob
V Charlie
V Dave
S Eve
Output:
Alice
Bob
Charlie
Eve
Dave
Key concepts
queuepriority
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