Fair turns: back of the queue
transcript
An online shop runs a flash sale. The stock is limited to a few units. The sale must be fair to every customer.
The customer at the front buys one unit. If they want more, they go to the back of the line. In this way, everyone gets a turn.
Three customers want two, three and one. The stock is five units. Customer two finishes first.
Back of the queue gives three, minus one, two. Front gives one, four, minus one.
Sometimes the queue is empty. Jump the clock ahead. Go to the next arrival second.
Each unit sold takes one turn. The stock limits the number of turns. So the cost is linear in the stock.
The stock can reach zero. Then the sale stops. Those still waiting get minus one.
In the interview, say who joins first. Say what an empty queue does. And say what happens at zero stock.
One unit a turn. Then the back. Full breakdown in the lecture.