Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Time Needed to Buy Tickets
Bookmark
Queue Simulation
Single Pass
Input
Standard
Example 2
Larger
Custom
tickets
=
[2, 3, 2]
,
k
=
2
time =
0
counter
2
P0
3
P1
2
P2 = k
each second, the front person buys one ticket
time =
0
counter
2
P0
3
P1
2
P2 = k
each second, the front person buys one ticket
time =
1
counter
1
P0
3
P1
2
P2 = k
person 0
buys 1
remaining =
1
time =
2
counter
2
P1
2
P2 = k
1
P0
person 1
buys 1
remaining =
2
time =
2
counter
2
P2 = k
1
P0
2
P1
person 1
needs 2 more → back of the line
time =
3
counter
1
P2 = k
1
P0
2
P1
person 2
buys 1
remaining =
1
time =
4
counter
0
P0
2
P1
1
P2 = k
person 0
buys 1
remaining =
0
time =
4
counter
0
P0
2
P1
1
P2 = k
person 0
is done and leaves
time =
5
counter
1
P1
1
P2 = k
person 1
buys 1
remaining =
1
time =
6
counter
0
P2 = k
1
P1
person 2
buys 1
remaining =
0
time =
6
counter
0
P2 = k
1
P1
person 2 done in
6
algo
master
.
io
Step:
Start: run the queue until person 2 buys all 2 ticket(s).
0 / 12
Input
Standard
Example 2
Larger
Custom
tickets
=
[2, 3, 2]
,
k
=
2
0 / 12
algo
master
.
io
Step:
Start: run the queue until person 2 buys all 2 ticket(s).