Skip to content

Queue

storing

Level 1 of 3

Also called First in, first out.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

An empty queue is given 6, 2, -, 9, 4, -. Each number joins, and each dash lets the front one leave. What does the line hold then, front first?

the answer, and why

the answer is A

6 leaves first, the oldest. Then 9 and 4 join behind 2, and the last dash lets 2 leave.

Queue, oldest out. Numbers join at the right end, the back, and leave from the left end, the front.

join 66join 262leave26gives 6join 929join 4294leave942gives 2

Out, in turn: 6, 2.

  • just put in
  • just taken out
Each operation in turn. The line ends holding 9 and 4.

if you chose another

  • That is a stack's answer, where each dash takes the newest. A leave always takes the front.
  • That misses the last leave, which lets 2 go.
  • That reads the line from the back. Front first, 9 leads.

question 2

How many numbers leave an empty queue given -, 5, 1, -, -, -, 8, -?

the answer, and why

the answer is B

The first dash finds the line empty. 5 and 1 join and leave in turn. The next dash finds it empty again, then 8 joins and leaves.

Queue, oldest out. Numbers join at the right end, the back, and leave from the left end, the front.

leaveempty, nothing comesjoin 55join 151leave15gives 5leave1gives 1leaveempty, nothing comesjoin 88leave8gives 8

Out, in turn: 5, 1, 8.

  • just put in
  • just taken out
5 dashes. The 1st and the 4th find the line empty, so 3 numbers leave.

if you chose another

  • That counts every dash, but the first and the fourth find the line empty.
  • That counts one of the two dashes that find the line empty.
  • That counts every operation, the joins too.

question 3

A ring of 5 slots, numbered 0 to 4, holds 3 numbers, its front at slot 3. In which slot does the next number join?

the answer, and why

the answer is D

Counting on from the front, the 3 numbers fill slots 3 and 4, then carry on to slot 0. The next free slot is 1.

A ring. Past the last slot, the mark and the next join carry on from slot 0.

slot01234movesjoin 990join 5950join 79570leave 99570leave 5570leave 770join 660join 116110join 886110join 128126110Moves in all: 0.
  • a number waiting
  • just joined
  • just left
  • an empty slot
  • the front, next to leave
Seven numbers join a ring of 5, and three leave. 6 and 11 fill slots 3 and 4, 8 carries on to slot 0, and 12 joins at slot 1.

this tests best, worst and average, on the learn page

if you chose another

  • A ring of 5 has no slot 6. Past slot 4, it carries on from slot 0.
  • Slot 0 already holds the third number.
  • Here 3 is the front's slot and 3 the numbers held. That answer takes 4 away from 3 + 3, which is 6. Counting on instead, past slot 4 and back to slot 0, gives slot 1.

question 4

Which is true of letting nn numbers join an empty line, then all leave?

the answer, and why

the answer is A

A leave from kk numbers moves the k−1k - 1 behind the front, so the leaves move n−1n - 1, then one fewer, down to 0. For 6, 5 + 4 + 3 + 2 + 1 + 0 is 15, which is 5 times 6, halved. The learn page's moving-up lines add this up to (n−1)n2\frac{(n - 1)n}{2}. A mark moves no number.

this tests where the formula comes from, on the learn page

if you chose another

  • Each leave gives back one number either way, but moving up also moves every number behind the front.
  • Moving the mark is part of the leave, which counts 1 either way. No number moves.
  • A leave from kk numbers moves k−1k - 1 of them, not 1.

pen and paper

question 5about 6 minutes

A program keeps a line in a ring of 5 slots, numbered 0 to 4, empty at first. It is given 3, 8, 6, -, -, 1, 4, 9. Each number joins, and each dash lets the front one leave.

  1. In which slot does each number join?
  2. Where is the front at the end, and what does the line hold, front first?
  3. How many numbers move?
a hint

Count on from the front. Past slot 4, carry on from slot 0.

a worked answer

3, 8 and 6 join at slots 0, 1 and 2. 3 and 8 leave, so the front moves to slot 2. 1 and 4 join at slots 3 and 4, and 9 carries on to slot 0. The front is at slot 2, and the line holds 6, 1, 4 and 9, front first. No number moves.

A ring. Past the last slot, the mark and the next join carry on from slot 0.

slot01234movesjoin 330join 8380join 63860leave 33860leave 8860join 1610join 46140join 996140Moves in all: 0.
  • a number waiting
  • just joined
  • just left
  • an empty slot
  • the front, next to leave
The eight operations in a ring of 5 slots. 9 joins at slot 0, past the last slot, and no number moves.

this tests best, worst and average, on the learn page

what a full answer includes
  • Slots 0, 1, 2, 3, 4 and then 0.
  • The front at slot 2, the line 6, 1, 4, 9.
  • No moves.

question 6about 8 minutes

Another program keeps the front at slot 0. When the front number leaves, every number behind it moves one slot forward. Let 6 numbers join an empty line, then all 6 leave.

  1. How many moves do the 6 joins make?
  2. And the 6 leaves?
  3. How many joins, leaves and moves in all, and how many for a ring with room for all 6?
a hint

A leave from kk numbers moves the k−1k - 1 behind the front.

a worked answer

The joins move none. The leaves find 6, 5, 4, 3, 2 and 1 numbers, so they move 5 + 4 + 3 + 2 + 1 + 0, which is 15. That is (n−1)n2\frac{(n - 1)n}{2} at 6. With the 12 joins and leaves that makes 27, which is n2+3n2\frac{n^2 + 3n}{2} at 6. A ring with room for all 6 makes the 12, with no moves and no copies.

operationsmovesjoins and leaves
6 joins06
6 leaves5 + 4 + 3 + 2 + 1 + 0, which is 156
in all1512
a ring of 6 slots012
Moving up, 6 numbers in and out, then a ring with room for all 6.
Keep the front at slot 0 instead, moving every number up on each leave. How many moves do nn joins, then nn leaves, make? And how many with the joins and leaves?
  1. the move: a leave moves the rest up

    the line:
    a leave from k numbersmoves the k−1behind the front\begin{gathered} \text{a leave from } k \text{ numbers} \\ \text{moves the } k - 1 \\ \text{behind the front} \end{gathered}

    Moving up. The front stays at slot 0, and a leave moves every number behind it one slot forward.

    slot0123movesjoin 220join 7270join 52750join 927590leave2759gives 23leave759gives 72leave59gives 51leave9gives 90Moves in all: 6.
    • a number waiting
    • just joined
    • moved one slot forward
    • just left
    • an empty slot
    • the front, next to leave
    Four join, then four leave, the front kept at slot 0. The leaves move 3, 2, 1 and 0 numbers, 6 moves in all.

    why it is allowed: The front stays at slot 0. When its number leaves, each of the k−1k - 1 numbers behind it moves one slot forward. A join writes after the last number and moves none.

  2. the move: add the moves

    the line:
    M=(n−1)+⋯+1+0M = (n - 1) + \dots + 1 + 0

    why it is allowed: MM is the moves of nn joins, then nn leaves. By line 1 the joins move none, and the leaves find nn numbers, then one fewer each time. For 4, 3 + 2 + 1 + 0.

  3. the move: turn it round

    the line:
    M=0+1+⋯+(n−1)M = 0 + 1 + \dots + (n - 1)

    why it is allowed: By line 2, added in the other order, which gives the same. For 4, 0 + 1 + 2 + 3.

  4. the move: leave out the 0

    the line:
    M=1+2+⋯+(n−1)M = 1 + 2 + \dots + (n - 1)

    why it is allowed: By line 3. Adding 0 changes nothing. For 4, 1 + 2 + 3.

  5. the move: use the page on adding up

    the line:
    M=(n−1)((n−1)+1)2M = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.

      why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds nn standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The box under this line adds 1 up to a last number with two staircases: the sum is that last number times one more, halved. By line 4, here the last number is n−1n - 1, and one more is (n−1)+1(n - 1) + 1. For 4, 1 + 2 + 3 is 3 times 4, halved, which is 6.

  6. the move: tidy

    the line:
    M=(n−1)n2M = \frac{(n - 1)n}{2}

    why it is allowed: By line 5, as (n−1)+1(n - 1) + 1 is nn. For 4, 3 times 4, halved, is 6, as the picture counts.

  7. the move: multiply out

    the line:
    M=n2−n2M = \frac{n^2 - n}{2}

    why it is allowed: By line 6, nn lots of n−1n - 1 are n2n^2 less nn. For 4, 16 less 4, halved, is 6.

  8. the move: a ceiling

    the line:
    M≤n22M \le \frac{n^2}{2}

    why it is allowed: Line 7 takes nn away before halving. Leaving it in gives more. For 4, 6 is at most 8.

  9. the move: read it as a bound

    the line:
    M∈O(n2)M \in O(n^2)

    why it is allowed: By line 8, with the fixed multiple one half.

  10. the move: a fact about n

    the line:
    n≤n22 for n≥2n \le \frac{n^2}{2} \text{ for } n \ge 2

    why it is allowed: n22\frac{n^2}{2} is nn times half of nn, and half of nn is at least 1 from 2 on. For 4, 4 is at most 8.

  11. the move: put line 10 into the top

    the line:
    M≥n222 for n≥2M \ge \frac{\frac{n^2}{2}}{2} \text{ for } n \ge 2

    why it is allowed: By line 10, nn is at most half of n2n^2. So line 7's top, n2n^2 less nn, is at least n2n^2 less half of n2n^2, which is half of n2n^2. For 4, the top 16 less 4 is 12, at least 16 less 8, which is 8.

  12. the move: halve it

    the line:
    M≥n24 for n≥2M \ge \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: By line 11, half of n2n^2, halved, is a quarter of n2n^2. For 4, halving 12 and 8 gives 6, at least 4.

  13. the move: read it as a bound

    the line:
    M∈Ω(n2)M \in \Omega(n^2)

    why it is allowed: By line 12, from 2 numbers on, with the fixed multiple one quarter.

  14. the move: ceiling and floor together

    the line:
    M∈Θ(n2)M \in \Theta(n^2)

    why it is allowed: Lines 9 and 13 bound MM by the same growth, n2n^2.

  15. the move: add the joins and leaves

    the line:
    M+2n=n2−n2+2nM + 2n = \frac{n^2 - n}{2} + 2n

    why it is allowed: Each join and each leave still counts 1, 2n2n in all, as with a mark. Add 2n2n to both sides of line 7. For 4, 6 + 8 is 14.

  16. the move: one denominator

    the line:
    M+2n=n2−n+4n2M + 2n = \frac{n^2 - n + 4n}{2}

    why it is allowed: By line 15, 2n2n is 4n4n halved, so both parts sit over 2. For 4, 16 less 4, plus 16, is 28, and 28 halved is 14.

  17. the move: collect the n terms

    the line:
    M+2n=n2+3n2M + 2n = \frac{n^2 + 3n}{2}

    why it is allowed: By line 16, 4n4n less nn is 3n3n. For 4, 16 + 12 is 28, and 28 halved is 14.

try it with a small nn: at n=4n = 4, line 2 gives M=6M = 6, which you can work out by hand

this tests how it compares, on the learn page

what a full answer includes
  • No moves for the joins.
  • 15 for the leaves.
  • 27 against 12.