Served in Order
People line up at a counter. events is a list of strings read from first to last. The string "NEXT" means the person at the front of the line is served and leaves. Any other string is the name of a person who joins the back of the line at that moment.
Return the names in the order they were served. A NEXT while nobody is waiting does nothing. The same name may join more than once, and each visit is served separately.
Example 1
events ["ann","bob","NEXT","cid","NEXT","NEXT"]
returns ["ann","bob","cid"]
ann and bob are waiting when the first NEXT comes, so ann is served; cid joins, then bob and cid are served.
Example 2
events ["NEXT","dee","NEXT","NEXT"]
returns ["dee"]
The first NEXT finds an empty line, so only dee is served; the last NEXT finds nobody.
Constraints
0 <= len(events) <= 100- names are 1 to 10 lowercase letters and are never
NEXT
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Stack After CommandsBeginner
- Typing With UndoBeginner
- Card WarEasy
- Cups on the CounterEasy
- Depth If BalancedEasy
- Drop the Unmatched BracketsEasy
- Editor With Undo and RedoEasy
- First Loss in Each BlockEasy