FunctionRacerProblemsRace

Dead-End Driveway

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

Cars park one behind another in a narrow dead-end driveway, so only the car that parked last can drive straight out. events is applied in order:

Return the total number of times a car had to back out to let another car leave.

Example 1

events ["in a","in b","in c","out a","out c"]
returns 2

For a to leave, b and c back out (2 moves). Then c is last in line and leaves without moving anyone.

Example 2

events ["in x","out x","in y","in z","out y","in w","out z"]
returns 2

Only z backs out for y, and only w backs out for z.

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