4/28
Part 1 of 6 · A queue and its holdersLearn and do

Name the states before you write the class

Every task in the queue is in one of four states, and it moves between them on four events:

The four states of a task, and the events that move it
reserveackfailfail-lastwaitingreserveddonedead
  • waiting: in the line, ready to be handed out.
  • reserved: a worker holds it.
  • done: the holder acked it. Nothing moves it again.
  • dead: it failed too many times and sits in the dead-letter queue. Nothing moves it again either, until a person sends it back.

Say these out loud at the start of the interview. They answer most edge cases before you write any code. Can you ack a waiting task? No, only a reserved one. Can a done task fail? No. Can a task be reserved twice at once? No, a reserve needs a waiting task.

The candidate who failed this round missed one rule, the place a failed task goes back to (1 report). A state table turns the edge cases into a list you can check, instead of something you remember under time pressure.

A table also makes a good test plan. Every arrow in the figure is a test that the move works. Every missing arrow is a test that the move is refused.

The function next_state in the editor answers one question: from this state, on this event, where does a task go? Write it as a lookup in a dict of the four allowed moves.