FunctionRacerProblemsRace

Served in Order

BeginnerStacks and queuesPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems