Ctrl K

A Picky Line at the Counter

Easy
A canteen has two kinds of trays, 0 and 1. The trays are stacked, with trays[0] on top. A line stands at the counter, line[0] at the front, and each person wants one particular kind of tray. There are exactly as many trays as people. Serving works like this: if the person at the front wants the kind on top of the stack, they take that tray and leave. Otherwise they go to the back of the line. Nobody may take a tray other than the top one. Serving stops once nobody left in line wants the top tray. Return how many people go without lunch.
Example 1
line=[0, 1, 1], trays=[1, 0, 1] → 0

The first person wants 0 but the top is 1, so they move to the back: [1, 1, 0]. The next one takes the 1, leaving [1, 0] and trays [0, 1]. The front 1 goes to the back again, the 0 is taken, then the last 1. Nobody is left hungry: 0.

Example 2
line=[0, 0, 1, 0], trays=[1, 1, 0, 0] → 3

The only person who wants 1 takes the top tray. The next tray is 1 again, but the three people left all want 0, so none of them eats. The answer is 3.

Constraints
  • 1 ≤ len(line) == len(trays) ≤ 100
  • line[i] and trays[i] are 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsQuiz Score Log EasyWaiting in the Ticket Line EasyRobots Clashing on a Rail MediumReplaying Stack Pushes and Pops MediumA Deck That Reveals in Order Medium
loading Python…
Input
line=[0, 1, 1], trays=[1, 0, 1]
Expected
0

Run checks the first 3 tests in your browser. Submit checks all 88 tests on the server, hidden ones included.