Search HoldMyTwist

Browse all posts

Episode 4

The Parade Printer Cuts the Line

The robot would like to get on with printing the duck pennant, but there's a prior job in the queue.

Episodes

Computing Basics

    The robot works at a little parade print desk. A yellow wind-up duck in the aqua monitor holds a pennant sketch. The waiting tray beside the printer is empty.
    The robot tilts the empty waiting tray toward the monitor. Its front faces the printer; its back faces the arriving job cards.
    A STAR job card arrives. The robot places it alone in the waiting tray. The printer remains idle.
    The MOON card arrives after STAR. The DUCK card arrives after MOON. The waiting queue, front to back, is STAR, MOON, DUCK.
    The robot reaches toward the newest card, DUCK, at the back. Its pincer has not removed a card or started the printer.
    The duck stretches a wing toward the STAR card at the front. The robot stops. No print has begun and the queue order is unchanged.
    The robot follows the waiting row from STAR at the front through MOON to DUCK at the back. The duck lowers its own sketch.
    The robot takes only the STAR card from the front. The duck watches its DUCK card remain behind MOON.
    STAR moves into the active printer slot. A star pennant emerges from the printer. STAR is no longer in the waiting tray.
    Duck speaks from off screen as a close view shows the MOON and DUCK job cards waiting, and the completed STAR pennant hanging beside them.
    A new SUN card arrives. The robot adds it after DUCK. The waiting queue is now MOON, DUCK, SUN, from front to back.
    The duck points to the three waiting cards. MOON is at the front, then DUCK, then SUN. The robot pauses before taking another card.
    Duck holds a wing to its beak and studies the queue from the monitor. The next job has not been chosen.
    Robot pauses with an empty pincer over the front stop of the waiting rail. No job card has moved.
    The robot takes MOON from the front. DUCK and SUN remain in that order. The duck nods from inside the monitor.
    Robot hangs the completed MOON pennant after STAR. DUCK and SUN remain waiting. The printer output mouth is empty.
    Robot holds the single printed DUCK pennant. SUN is the only waiting card; the printer output is empty. Duck copies the pennant pose inside the monitor.
    Four finished pennants hang in STAR, MOON, DUCK, SUN order. The waiting tray and printer output are empty. Robot gestures toward the final SUN pennant.
    The robot looks into the empty waiting tray. The four printed pennants flutter beside it. No new card has arrived.
    The robot keeps its pincer away from the empty tray and does not start another print. The duck checks the empty tray from the monitor.
    Robot steadies the string of four completed pennants, STAR, MOON, DUCK, SUN, before the rainy window. Duck admires them from the monitor. The waiting rail is empty.
    The duck folds its wings with a sheepish grin. The robot hangs the DUCK pennant beside the monitor. The waiting tray stays empty.

    Episode transcript

    The parade print desk

    Duck: We need the parade pennants with the names!

    FRONT: nearest printer. BACK: new arrivals. EMPTY

    Robot: Empty queue. There's nothing to print.

    STAR job card appears.

    FRONT [STAR] BACK

    Robot: STAR first.

    MOON is next to the STAR. DUCK is next to the MOON.

    ARRIVALS: STAR → MOON → DUCK. FRONT [STAR, MOON, DUCK] BACK

    Duck: Then MOON. Then MY DUCK!

    A tempting cut-in

    The robot's arm swings at the latest card, DUCK, in the back. It hasn't taken a card out or triggered the printer with the pincer.

    Robot: That duck pennant looks nifty. I'll print that first.

    Duck extends one of his wings in the direction of the STAR card in the front. The robot halts. No printing has been started. The order in the queue remains.

    Duck: Wait! Someone had STAR ahead of me.

    The robot traces a row that is waiting from STAR (in front) to MOON and DUCK (in back).

    Robot: This desk does First Come, First Served.

    The duck sees his DUCK card stays behind MOON.

    FRONT [STAR, MOON, DUCK] BACK

    Duck: STAR goes first. I have no problem waiting.

    A new arrival

    STAR goes into active printer. You see a star pennant come out of the printer. STAR is no longer in the waiting tray.

    Robot: STAR is being printed.

    FRONT [MOON, DUCK] BACK

    Duck: MOON leads now.

    New SUN card shows up!

    FRONT [MOON, DUCK, SUN] BACK

    Robot: SUN joins on the rear side.

    Duck points at 3 pending cards. MOON is first, then DUCK, then SUN. Robot hesitates before picking up another card.

    First in, first out

    Robot grabs MOON from front. DUCK and SUN stay in order. Duck is nodding inside monitor.

    ACTIVE: MOON. WAITING: DUCK, SUN

    Robot: MOON.

    Bot adds it after the STAR pennant. DUCK is the front waiting card now.

    PRINTED: STAR, MOON. FRONT [DUCK, SUN] BACK

    Duck: There goes MOON!

    Robot takes off DUCK (in front) and prints a duck pennant. There's only SUN waiting.

    PRINTED: STAR, MOON, DUCK. WAITING: SUN

    Duck: My turn!

    The robot pulls the last sun card out. It prints the sun pennant. There's nothing in the waiting tray. The four completed pennants are suspended in order STAR, MOON, DUCK, SUN.

    PRINTED: STAR, MOON, DUCK, SUN. EMPTY

    Robot: SUN ends the row.

    No waiting jobs

    The robot scans the empty wait tray. The four printed pennants are waving. There is no new card.

    Duck: Any jobs available?

    The robot holds back its pincer from the empty tray. It doesn't initiate another print. The duck observes the empty tray on the monitor.

    Robot: None. We will sit it out until the next job comes in.

    All of the jobs that were asked have been printed out once.

    Robot: Good thing you stopped me jumping the queue.

    First in, first out (FIFO)

    A queue is an ordered waiting list of items. In this story, the items are print jobs waiting to be done.

    A card and a printed pennant are two separate things. STAR is a card that says you want a pennant printed with the text STAR on it.

    JOB CARD: STAR → PRINTED PENNANT: STAR

    FRONT = HEAD. BACK = TAIL.

    There is a head and tail of the waiting queue. Enqueue always adds the new item at the tail of the queue after the existing waiting jobs. Dequeue removes from the head (oldest waiting job) for printing. New items can never "jump" to the head before the items that have been waiting longer. So the relative waiting order between items never changes as new items come.

    This desk uses FIFO. There may be priorities and cancellation rules on real printers.

    Try a new queue

    Answer

    BOAT should be processed next (already waiting when TREE arrived).

    WAITING: [BOAT, TREE] → TAKE BOAT → [TREE] → TAKE TREE → EMPTY

    Stop processing when there are no jobs left to process.

    In this case dequeuing means that you will take out something from the waiting queue to process it. The job you take out will not be canceled.

    Sources

    Paul E. Black, “queue”, NIST Dictionary of Algorithms and Data Structures (opens in a new tab)