Skip to content

Stack

storing

Level 1 of 3

Also called Last 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 stack is given 4, 9, -, 7, 2, -. Each number is put on, and each dash takes the top off. What does the pile hold then, bottom first?

the answer, and why

the answer is B

9 goes on 4 and comes straight back off. Then 7 and 2 go on, and the last take takes 2, the top.

Stack, newest out. Puts and takes happen at the right end, the top. The heavy line is the bottom.

put 44put 949take49gives 9put 747put 2472take472gives 2

Out, in turn: 9, 2.

  • just put in
  • just taken out
Each operation in turn. The pile ends holding 4 and 7.

if you chose another

  • That keeps 9, but the first take takes 9, the top then.
  • That takes 4, the bottom. A take only ever takes the top.
  • That misses the first take, which takes 9 off.

question 2

How many numbers come back from an empty stack given 8, 3, -, -, -, 5, -?

the answer, and why

the answer is C

The first two takes give back 3, then 8. The third finds the pile empty and gives nothing. The last gives back 5.

Stack, newest out. Puts and takes happen at the right end, the top. The heavy line is the bottom.

put 88put 383take83gives 3take8gives 8takeempty, nothing comesput 55take5gives 5

Out, in turn: 3, 8, 5.

  • just put in
  • just taken out
The third take finds nothing, so 3 numbers come back.

if you chose another

  • That counts every dash, but the third take finds the pile empty.
  • That stops before 5 goes on and comes back.
  • That counts every operation, the puts too.

question 3

Which is true of putting nn numbers on an empty stack, then taking all nn off?

the answer, and why

the answer is A

Each put and each take touches only the end of the row and counts 1, whatever the numbers. Section 1 says so, and lines 1 and 2 of section 4's first question.

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

if you chose another

  • No put or take reads a number, so every order costs the same.
  • The top is the end of the row, found with no search.
  • A take removes the last number and counts 1, however tall the pile.

question 4

A stack and a queue are each given 3, 6, 1, -, -, 8, -, -. In what order do the numbers come out of the stack?

the answer, and why

the answer is A

The first take gives 1, the newest, then 6. 8 goes on and comes straight back. 3, the oldest, comes out last.

Stack, newest out. Puts and takes happen at the right end, the top. The heavy line is the bottom.

put 33put 636put 1361take361gives 1take36gives 6put 838take38gives 8take3gives 3

Out, in turn: 1, 6, 8, 3.

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

join 33join 636join 1361leave613gives 3leave16gives 6join 818leave81gives 1leave8gives 8

Out, in turn: 3, 6, 1, 8.

  • just put in
  • just taken out
The same operations, side by side. On the stack, 8 comes straight back off; in the queue, it waits behind 1.

this tests how it works, on the learn page

if you chose another

  • That is the queue's order, oldest first.
  • That reverses the order the numbers went on, as if all went on before any came off.
  • That gives 3 before 8, but 3 is under 8 when the last two takes come.

pen and paper

question 5about 8 minutes

A program keeps a pile in a row with room for 1 number. A put that finds the row full first copies every number into a new row with twice the room. You put on 7 numbers, so nn is 7. Here pp is the smallest power of 2 at or above 7, which is 8.

  1. At which puts does the row double?
  2. How many numbers are copied in all?
  3. Check your count against p−1p - 1, the formula of section 4, and against 2n−12n - 1.
a hint

The put after 1, 2 and 4 numbers finds the row full.

a worked answer

The row doubles at puts 2, 3 and 5, when it holds 1, 2 and 4 numbers. Those copies make 1 + 2 + 4, which is 7. With pp at 8, p−1p - 1 is 7 too. That is at most 2n−12n - 1, which is 13.

copiedput 10put 21put 32put 40put 54put 60put 70Copies in all: 7.
  • held
  • just put in
  • copied into the new row, bracketed under it
  • room to spare
Seven puts into a row with room for 1, doubling at puts 2, 3 and 5.
A row with room for 1 doubles when a put finds it full. How many numbers do nn puts copy?
  1. the move: a full row doubles

    the line:
    room for 1, 2, 4, 8:each full row doubles\begin{gathered} \text{room for 1, 2, 4, 8:} \\ \text{each full row doubles} \end{gathered}
    copiedput 10put 21put 32put 40put 54put 60put 70put 80put 98Copies in all: 15.
    • held
    • just put in
    • copied into the new row, bracketed under it
    • room to spare
    Nine puts. The row doubles at puts 2, 3, 5 and 9, copying 1, 2, 4 and 8 numbers.

    why it is allowed: A put that finds the row full first copies every number into a new row with twice the room. Then it writes its own.

  2. the move: price a doubling

    the line:
    the put that finds the rowfull copies all it holds,its room: 1, 2, 4, and on\begin{gathered} \text{the put that finds the row} \\ \text{full copies all it holds,} \\ \text{its room: 1, 2, 4, and on} \end{gathered}
    full row6295new row629574 copied
    • held
    • copied into the new row, bracketed under it
    • just put in
    • room to spare
    Put 5 finds a row of 4 full. It copies each number to the same place in a row of 8, then writes 7.

    why it is allowed: The room starts at 1 and only doubles, so it runs 1, 2, 4, 8. A full row holds as many numbers as its room, and the next put copies them all. Put 5 finds 4 and copies 4.

  3. the move: the last room

    the line:
    the room after n puts is p,the smallest power of 2at or above n\begin{gathered} \text{the room after } n \text{ puts is } p \text{,} \\ \text{the smallest power of 2} \\ \text{at or above } n \end{gathered}
    × 21248169
    Doublings of 1 up to 16. 9 lies between 8 and 16, so pp is 16.

    why it is allowed: For 9, put 9 found the row full at 8 and doubled it to 16. So 8 is below 9, and 16 is at or above 9. In general, by line 2 the rooms run 1, 2, 4, and on, so pp is a power of 2. It holds all nn, so it is at least nn. It doubled from half of pp only when a put found that half full, so half of pp is below nn. This holds from 2 numbers on; at 1, nothing doubles.

  4. the move: add the copies

    the line:
    C=1+2+4+⋯+p2C = 1 + 2 + 4 + \dots + \frac{p}{2}

    why it is allowed: CC is the copies of nn puts. By lines 2 and 3, the doublings copy the rooms 1, 2, 4, and on, up to half of pp, the room before the last doubling. For 9, 1 + 2 + 4 + 8, and pp is 16.

  5. the move: add the doublings

    the line:
    C=p−1C = p - 1
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

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

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: The box under this line shows a doubling sum from 1 is one less than the next doubling. In the box, nn is not the puts: it counts the doublings from 1 to the last term. For 9 puts it is 3, as 1 doubled 3 times is 8, half of pp. After half of pp comes pp. For 9, 15 is 16 less 1.

  6. the move: the room is under twice the numbers

    the line:
    p<2np < 2n

    why it is allowed: By line 3, half of pp is below nn. Doubling both, pp is below 2n2n. For 9, 8 is below 9, so 16 is below 18.

  7. the move: a ceiling

    the line:
    C≤2n−1C \le 2n - 1

    why it is allowed: By lines 5 and 6, CC is pp less 1, and pp is below 2n2n, so CC is at most 2n−12n - 1. For 9, 15 is at most 17. That is under 2 copies a number; in Sebastian Bourges' books, 'Under two moves per item' (volume two, chapter 21; their moves are this page's copies).

  8. the move: add the puts' own writes

    the line:
    C+n≤3n−1C + n \le 3n - 1

    why it is allowed: Each put also writes its own number, nn writes in all. Add nn to both sides; 2n−12n - 1 and nn make 3n−13n - 1. For 9, 15 + 9 is 24, at most 26.

  9. the move: read it as a bound

    the line:
    C+n∈O(n)C + n \in O(n)

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

  10. the move: share it out

    the line:
    C+nn≤3−1n\frac{C + n}{n} \le 3 - \frac{1}{n}
    putcopies and its writeso farshare so far
    1111
    2231.5
    3362
    4171.75
    55122.4
    6113about 2.17
    71142
    8115about 1.88
    9924about 2.67
    Each put's copies and its own write, added up as the puts come. The share never reaches 3.

    why it is allowed: Line 8 divided by nn shares the copies and writes over the nn puts. One put may copy many, as put 9 copies 8. For 9, 24 over 9.

  11. the move: under 3

    the line:
    C+nn<3\frac{C + n}{n} < 3

    why it is allowed: Line 10 takes one over nn from 3, and one over nn is above 0, so the share is under 3. For 9, 24 over 9 is about 2.67.

try it with a small nn: at n=9n = 9, line 4 gives C=15C = 15, which you can work out by hand

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

what a full answer includes
  • Doubling at puts 2, 3 and 5.
  • The copies 1 + 2 + 4, which is 7.
  • p−1p - 1 giving 7, at most 13.

question 6about 10 minutes

Another program keeps a pile in a row with its top at the start, place 1. A put first moves each number held one place along; a take moves each number still held one place back. Put on 6 numbers, then take all 6 off.

  1. How many moves do the 6 puts make?
  2. And the 6 takes?
  3. How many puts, takes and moves in all, against the top kept at the end?
a hint

Here kk is how many numbers the pile holds when a put comes, or still holds after a take. The put onto kk numbers moves all kk, and so does the take that leaves kk.

a worked answer

The puts find 0, 1, 2, 3, 4 and 5 numbers, 15 moves. The takes leave 5, 4, 3, 2, 1 and 0, 15 more, so 30 in all, and (n−1)n(n - 1)n at 6 is 30. With the 12 puts and takes that makes 42, which is n2+nn^2 + n at 6. Kept at the end, the pile costs 12 puts and takes and no moves.

operationsmovesputs and takes
6 puts0 + 1 + 2 + 3 + 4 + 5, which is 156
6 takes5 + 4 + 3 + 2 + 1 + 0, which is 156
in all3012
the top at the end012
The top at the start, 6 numbers on and off, then the top at the end.
Keep the top at the row's start instead. How many moves do nn puts, then nn takes, make?
  1. the move: a put at the start

    the line:
    a put onto k numbersmoves each one place along\begin{gathered} \text{a put onto } k \text{ numbers} \\ \text{moves each one place along} \end{gathered}

    Stack, top at the start: place 1, on the left. A put moves each number held one place along; a take moves each one back.

    movesput 440put 8841put 33842take843gives 32take48gives 81take4gives 40

    Moves in all: 6.

    • just put in
    • just taken out
    • moved one place along
    • moved one place back
    Three numbers on, then off, with the top at place 1, on the left. Kept at the end, the same pile needs no moves.

    why it is allowed: The top is now place 1, so the new number needs place 1 free. Each of the kk numbers held moves one place along first.

  2. the move: a take at the start

    the line:
    a take leaving k numbersmoves each one place back\begin{gathered} \text{a take leaving } k \text{ numbers} \\ \text{moves each one place back} \end{gathered}

    why it is allowed: The top leaves place 1, and each of the kk numbers still held moves one place back to fill the gap.

  3. the move: add the moves

    the line:
    M=(0+1+⋯+(n−1))×2M = \bigl(0 + 1 + \dots + (n - 1)\bigr) \times 2

    why it is allowed: MM is the moves of nn puts, then nn takes. By line 1 the puts move 0, 1, up to n−1n - 1. By line 2 the takes move n−1n - 1, down to 0, the same sum. For 3, 6.

  4. the move: leave out the 0

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

    why it is allowed: Adding 0 changes nothing.

  5. the move: use the page on adding up

    the line:
    M=(n−1)((n−1)+1)2×2M = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2} \times 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, that number times one more, halved. Here the last number is n−1n - 1. For 3, 2 times 3, halved, is 3, and times 2 is 6, as line 3.

  6. the move: halve, then double

    the line:
    M=(n−1)((n−1)+1)M = (n - 1)\bigl((n - 1) + 1\bigr)

    why it is allowed: Halving, then doubling, changes nothing. For 3, 3 doubled is 6.

  7. the move: tidy

    the line:
    M=(n−1)nM = (n - 1)n

    why it is allowed: (n−1)+1(n - 1) + 1 is nn. For 3, 2 times 3 is 6.

  8. the move: multiply out

    the line:
    M=n2−nM = n^2 - n

    why it is allowed: nn lots of nn, less nn lots of 1. For 3, 9 less 3.

  9. the move: add the puts and takes

    the line:
    M+2n=n2+nM + 2n = n^2 + n

    why it is allowed: Each put still writes its number and each take gives one back, 2n2n puts and takes, as with the top at the end. Add 2n2n to both sides; n2−nn^2 - n and 2n2n make n2+nn^2 + n. For 3, 6 + 6 is 12.

  10. the move: a ceiling

    the line:
    M≤n2M \le n^2

    why it is allowed: Line 8 takes nn away from n2n^2. For 3, 6 is at most 9.

  11. the move: read it as a bound

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

    why it is allowed: By line 10, with the fixed multiple 1.

  12. 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 3, 3 is at most 4.5.

  13. the move: a floor

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

    why it is allowed: Line 8 takes nn from n2n^2. By line 12, nn is at most half of n2n^2, so at least half is left. For 3, 9 less 3 is 6, at least 4.5.

  14. the move: read it as a bound

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

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

  15. the move: ceiling and floor together

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

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

try it with a small nn: at n=3n = 3, line 3 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
  • 15 moves for the puts.
  • 15 for the takes, 30 in all.
  • 42 against 12.