OCR H446 A Level Computer Science · 1.4 Data Types, Data Structures and Boolean Algebra

Stretch tasks Data Structures

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

This or That

Pixel the cat asks 8 questions. Each has two answers: pick the right one.

  • Two answers to every question
  • A fish treat for every right answer
  • Streaks win Pixel shades, then a party hat

About 4 minutes

Task 3

Memory Match

Flip the cards two at a time to match each term card to its meaning card.

  • 12 cards: each term has a meaning
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

Task 4

Sort It

Sort each statement into the right box, like the tick box tables in the exam.

  • 12 cards to sort into 4 boxes
  • Drag a card, tap a box or press its number
  • Combo points for cards sorted in a row

12 cards, about 4 minutes

Task 5

Code Word

Guess the lesson's secret words. Every guess shows which letters are right.

  • A clue for every word
  • Tiles turn green, amber or grey after each guess
  • Fewer guesses win more points

3 words, about 4 minutes

Task 7

Bug Hunt

Two statements are true and one has a bug. Catch the bug before it gets away.

  • Three statements in every round
  • Tap the one with the mistake
  • Streaks win bonus points

5 rounds, about 4 minutes

Brain games

Train your brain on this lesson

Logic, memory and speed, built from this lesson's own key ideas. Win up to three diamonds in each game.

Logic

Odd One Out

Four tiles: three share a link and one does not. Spot the odd one out, then see what links the rest.

  • Three belong together, one does not
  • A hint shows the link, for fewer points
  • Quick answers and streaks win more

8 rounds, about 3 minutes

Memory

Memory Flash

Study the term cards, then they flip over. Find the card that goes with each clue, from memory.

  • Three levels: 3, 4 and 6 cards
  • Two tries to find each card
  • Streaks win bonus points

3 levels, about 4 minutes

Speed

Fact or Fib

Statements fly in one at a time. Is each one a fact or a fib? Decide fast.

  • Swipe, tap or use the arrow keys
  • Every fib comes with its fix
  • Streaks multiply your points

12 statements, about 3 minutes

The final challenge

City Run

Race a robot cat on inline skates through a neon cyber city, from Neon Station to the Hyper Tunnel. Every question is a row of neon gates across the tracks: skate through the one with the right answer, dodge the trains and barriers, and grab diamonds.

  • 20 questions from this lesson, in four parts of the city
  • Swipe, tap or use the arrow keys
  • Dodge trains and holograms, and jump crates and cones

20 questions, about 6 minutes

Bonus challenge

Neon Roll

Roll a glowing ball down an endless neon slope, past towers lit up with neon signs of Pixel the cat. Steer, jump the gaps and dodge the blocks. Questions from this lesson wait at gates along the way: every right answer keeps the ball rolling.

  • Questions from this lesson at every gate
  • Hold the arrow keys or a side of the slope to steer
  • 8 right answers win two diamonds, 15 win three

Endless, about 5 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Data Structures

Data Structures: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. Which structure suits an undo feature?

    • Binary search tree
    • Stack
    • Queue
    • Hash table
  2. What problem does a circular queue solve?

    • The queue can never become full
    • Space at the front is reused instead of wasted
    • The queue can hold mixed data types
    • Items can be searched faster
  3. Inserting into the middle of a linked list only needs pointers to change.

    • True
    • False
  4. Which traversal visits the left subtree, then the node, then the right subtree?

    • Post order
    • Breadth first
    • In order
    • Pre order
  5. Why does an adjacency matrix waste memory on a sparse graph?

    • It stores every edge three times
    • It needs a pointer for every vertex
    • It cannot store weights
    • It uses n squared cells however few edges there are
  6. Which TWO are true of arrays compared with linked lists?

    Tick 2.

    • Stored in contiguous memory
    • Inserting in the middle only changes pointers
    • They grow and shrink easily
    • Each item stores a pointer to the next
    • Direct access by index in constant time
  7. After push(3), push(7), push(1), pop() and push(5), order the stack from top to bottom.

    Number these 1 to 3.

    • 7
    • 3
    • 5
  8. A binary search tree stays balanced whatever order items are inserted in.

    • True
    • False
  9. What is it called when two keys hash to the same index?

  10. A hash table holds 70 items in 100 slots. What should happen as its load factor passes about 0.7?

    • The oldest items are deleted
    • It is sorted into order
    • Every key is hashed twice
    • It is rehashed into a larger array
  11. Which data structure does breadth first search use?

    • Hash table
    • Tuple
    • Queue
    • Stack
  12. What makes a tuple different from a list?

    • It can only hold numbers
    • It is always sorted
    • It has no order
    • It cannot be changed once created

Quiz Challenge, round 2: Quick Fire

  1. Explain why a linked list is better than an array when items are often inserted in the middle. [2 marks]

  2. Describe the difference between a stack and a queue. [2 marks]

  3. Explain why an in order traversal of a binary search tree outputs values in ascending order. [2 marks]

  4. Explain how separate chaining deals with collisions in a hash table. [2 marks]

Quiz Challenge, round 3: Big Think

  1. Compare an array and a linked list. [6 marks]

  2. Recommend a data structure for a contacts app that must find a contact by name instantly, and justify it. [4 marks]

This or That: tick the right answer

  1. A graph has a million vertices and few edges. Which representation suits it?

    • Adjacency list
    • Adjacency matrix
  2. Which suits a print queue?

    • A queue
    • A stack
  3. Which gives direct access by index in constant time?

    • A linked list
    • An array
  4. Which traversal suits deleting a whole tree?

    • Pre order
    • Post order
  5. Which way of resolving collisions can cause clustering?

    • Linear probing
    • Separate chaining
  6. Which structure is ordered but cannot be changed once made?

    • A tuple
    • A list
  7. Items go into a binary search tree in sorted order. What does it become?

    • Effectively a linked list
    • A balanced tree
  8. Which traversal suits copying a tree?

    • In order
    • Pre order

Memory Match: match each term to its meaning

TermMeaning
A Stack1 Items stored divided by slots available
B Queue2 A tree node with no children
C Load factor3 Each vertex stores its neighbours
D Leaf4 Nodes joined by pointers
E Adjacency list5 Last in, first out
F Linked list6 First in, first out

Sort It: tick one box in each row

Which data structure suits each job best?

StatementStackQueueHash tableBinary search tree
Undo in a word processor
Finding every score between 50 and 70
Caching web pages by their address
A print spooler
Keeping data sorted as items are added
A keyboard buffer
Return addresses when subroutines are called
Breadth first search
Depth first search
Finding a user's record by username instantly
Looking up a price from a product code
Listing names in alphabetical order

Code Word: write the word that fits each clue

  1. A first in, first out structure, like a line at a bus stop. (5 letters)

  2. A node in a graph, joined to others by edges. (6 letters)

  3. An ordered collection that cannot be changed once it is made. (5 letters)

Link Up: find the three groups

These 12 tiles make 3 groups of 4. Write each group in the table, and what links its tiles.

  • Chaining
  • Linked list
  • Rehashing
  • Queue
  • Root
  • Load factor
  • Child node
  • Parent node
  • Leaf
  • Stack
  • Array
  • Collision

Bug Hunt: tick the statement that is wrong

In each set, two statements are true and one has a mistake in it.

    • A circular queue wastes the space at the front of the array.
    • A queue is first in, first out.
    • A stack is last in, first out.
    • A linked list gives direct access to any item by its index.
    • An array is stored in contiguous memory.
    • A linked list node holds data and a pointer to the next node.
    • Pre order traversal visits the left subtree before the node.
    • Post order traversal suits deleting a tree.
    • In order traversal of a binary search tree gives ascending order.
    • An adjacency list suits a sparse graph.
    • An adjacency matrix makes checking for one edge quick.
    • An adjacency matrix uses less memory than a list for a sparse graph.
    • Two keys that hash to the same index cause a collision.
    • Linear probing can cause clustering.
    • A hash table gets faster as its load factor rises.

Brain game: Odd One Out

Three tiles in each set share a link. Tick the odd one out, then write what links the other three.

    • Rehashing
    • Queue
    • Stack
    • Array

    What links the other three?

    • Load factor
    • Child node
    • Parent node
    • Leaf

    What links the other three?

    • Chaining
    • Rehashing
    • Queue
    • Load factor

    What links the other three?

    • Linked list
    • Queue
    • Array
    • Collision

    What links the other three?

    • Root
    • Parent node
    • Leaf
    • Stack

    What links the other three?

    • Chaining
    • Rehashing
    • Root
    • Collision

    What links the other three?

Brain game: Memory Flash, on paper

Which term means this? Write your answer from memory.

  1. Items stored divided by slots available

  2. A tree node with no children

  3. Each vertex stores its neighbours

  4. Nodes joined by pointers

  5. Last in, first out

  6. First in, first out

Brain game: Fact or Fib

Tick Fact if the statement is true and Fib if it is not.

  1. A queue is first in, first out.

    • Fact
    • Fib
  2. An adjacency matrix uses less memory than a list for a sparse graph.

    • Fact
    • Fib
  3. A linked list gives direct access to any item by its index.

    • Fact
    • Fib
  4. Two keys that hash to the same index cause a collision.

    • Fact
    • Fib
  5. Linear probing can cause clustering.

    • Fact
    • Fib
  6. An adjacency list suits a sparse graph.

    • Fact
    • Fib
  7. Pre order traversal visits the left subtree before the node.

    • Fact
    • Fib
  8. A hash table gets faster as its load factor rises.

    • Fact
    • Fib
  9. An array is stored in contiguous memory.

    • Fact
    • Fib
  10. Post order traversal suits deleting a tree.

    • Fact
    • Fib
  11. A circular queue wastes the space at the front of the array.

    • Fact
    • Fib
  12. A binary search tree stays balanced whatever order items are inserted in.

    • Fact
    • Fib

City Run

City Run is a race through a neon cyber city, played on screen. It asks 20 of the questions above, from the quiz, This or That, Memory Flash and Fact or Fib.

Neon Roll

Neon Roll is a ball rolling down an endless neon slope, played on screen. Every right answer keeps it rolling. It asks the questions above, from the quiz, This or That, Memory Flash and Fact or Fib.

Answers

Round 1: Quiz Blitz

  1. Stack The last change made is the first one undone, which is last in, first out.
  2. Space at the front is reused instead of wasted The pointers wrap round using MOD, so slots freed at the front are used again.
  3. True The new node points at the next node and the previous node points at the new one. No items move.
  4. In order In order traversal of a binary search tree gives the values in ascending order.
  5. It uses n squared cells however few edges there are Most of the cells record that there is no edge, so an adjacency list is far smaller.
  6. Direct access by index in constant time; Stored in contiguous memory Arrays are contiguous with direct access. The other three describe linked lists.
  7. 5 → 7 → 3 pop() removes the 1 that was pushed last, then 5 goes on top of 7 and 3.
  8. False Inserting items in sorted order sends every item down the same side, so the tree degenerates into a linked list.
  9. collision Collisions are resolved by separate chaining or by probing for the next free slot.
  10. It is rehashed into a larger array As the load factor rises collisions become frequent, so the table moves to a bigger array.
  11. Queue Breadth first search visits nodes in the order they were found: first in, first out.
  12. It cannot be changed once created A tuple is ordered but immutable.

Round 2: Quick Fire

    • A linked list only changes pointers
    • An array must shift every later element
    Model answer

    Inserting into a linked list only means changing two pointers, but an array must shift every later element along to make room.

    • A stack is last in, first out
    • A queue is first in, first out
    • A stack works at one end; a queue adds at the rear and removes from the front
    Model answer

    A stack is last in, first out, so the item pushed most recently is popped first. A queue is first in, first out, so items leave in the order they arrived.

    • It visits the left subtree, then the node, then the right subtree
    • Everything on the left is smaller and everything on the right is larger
    Model answer

    In order traversal visits the left subtree, then the node, then the right subtree, and in a binary search tree everything on the left is smaller and everything on the right is larger.

    • Each slot holds a list
    • Every item hashing to that index is added to its list
    • A lookup hashes the key and searches that list
    Model answer

    Each slot holds a linked list, and every item whose key hashes to that index is added to the list, so finding an item means hashing its key and searching that slot's list.

Round 3: Big Think

    • An array is stored in contiguous memory
    • A linked list is made of nodes connected by pointers
    • An array has a fixed size, while a linked list grows and shrinks
    • An array gives direct access by index in constant time
    • A linked list must be traversed from the start
    • Inserting in the middle shifts an array but only changes pointers in a list
    • A linked list uses extra memory for each pointer
    Model answer

    An array is stored in contiguous memory and has a fixed size, while a linked list is made of nodes stored anywhere and connected by pointers, so it grows and shrinks dynamically. An array gives direct access to any element by its index in constant time, but a linked list must be traversed from the start. Inserting into the middle of an array means shifting every later element, but in a linked list only two pointers change. A linked list also uses extra memory to store a pointer in each node.

    • Recommends a hash table
    • A hash function maps each name to an index
    • Lookup takes constant time on average, however many contacts there are
    • Collisions are handled by chaining or probing
    • The table is rehashed when the load factor gets too high
    Model answer

    A hash table is the best choice. A hash function maps each name to an index in an array, so a contact is found in constant time on average, however many contacts there are. Collisions, where two names hash to the same index, can be handled by chaining, and the table is rehashed into a larger array when the load factor gets too high.

This or That

  1. Adjacency list A matrix needs n squared cells however few edges there are. A list only stores the edges that exist.
  2. A queue Jobs should print in the order they were sent: first in, first out.
  3. An array The address is the base plus the index times the element size, so no traversal is needed.
  4. Post order Post order visits both children before the node, so nothing is deleted while it is still needed.
  5. Linear probing Probing fills neighbouring slots, so items bunch together and later searches take longer.
  6. A tuple A tuple is immutable. A list can grow, shrink and change.
  7. Effectively a linked list Every new item goes down the same side, so searching becomes linear.
  8. Pre order Pre order visits each node before its children, so the copy can be built from the top down.

Memory Match

  • A 5 Stack: Last in, first out
  • B 6 Queue: First in, first out
  • C 1 Load factor: Items stored divided by slots available
  • D 2 Leaf: A tree node with no children
  • E 3 Adjacency list: Each vertex stores its neighbours
  • F 4 Linked list: Nodes joined by pointers

Sort It

  1. Stack Undo in a word processor The most recent change is undone first: last in, first out.
  2. Binary search tree Finding every score between 50 and 70 The tree keeps values ordered, so a range can be found without checking everything.
  3. Hash table Caching web pages by their address The address is the key, so a hash table finds the cached page at once.
  4. Queue A print spooler Documents print in the order they were sent: first in, first out.
  5. Binary search tree Keeping data sorted as items are added Each new item goes into its ordered place in the tree.
  6. Queue A keyboard buffer Key presses must be handled in the order they were typed.
  7. Stack Return addresses when subroutines are called The most recent call returns first, so return addresses go on a stack.
  8. Queue Breadth first search Breadth first search visits nodes in the order they were found, using a queue.
  9. Stack Depth first search Depth first search backtracks to the most recent branch, which a stack gives.
  10. Hash table Finding a user's record by username instantly A hash table finds a key in constant time on average.
  11. Hash table Looking up a price from a product code Lookup by key is exactly what a hash table is for.
  12. Binary search tree Listing names in alphabetical order An in order traversal of a binary search tree gives the names in order.

Code Word

  1. QUEUE A first in, first out structure, like a line at a bus stop.
  2. VERTEX A node in a graph, joined to others by edges.
  3. TUPLE An ordered collection that cannot be changed once it is made.

Link Up

  • Linear structures Array; Stack; Queue; Linked list
  • Parts of a tree Root; Leaf; Parent node; Child node
  • Hash tables Collision; Load factor; Rehashing; Chaining

Bug Hunt: the wrong statement, and why

  1. A circular queue wastes the space at the front of the array. That is the problem with a linear queue. A circular queue wraps its pointers round so the space is reused.
  2. A linked list gives direct access to any item by its index. A linked list must be traversed from the start. Direct access by index is what an array gives.
  3. Pre order traversal visits the left subtree before the node. Pre order visits the node first, then the left subtree, then the right.
  4. An adjacency matrix uses less memory than a list for a sparse graph. A matrix always uses n squared cells, so on a sparse graph it wastes far more memory than a list.
  5. A hash table gets faster as its load factor rises. As the load factor rises, collisions become more frequent and performance falls, so the table is rehashed.

Odd One Out

  1. Rehashing is the odd one out: it goes with Hash tables. The other three: Linear structures.
  2. Load factor is the odd one out: it goes with Hash tables. The other three: Parts of a tree.
  3. Queue is the odd one out: it goes with Linear structures. The other three: Hash tables.
  4. Collision is the odd one out: it goes with Hash tables. The other three: Linear structures.
  5. Stack is the odd one out: it goes with Linear structures. The other three: Parts of a tree.
  6. Root is the odd one out: it goes with Parts of a tree. The other three: Hash tables.

Memory Flash

  1. Load factor Items stored divided by slots available
  2. Leaf A tree node with no children
  3. Adjacency list Each vertex stores its neighbours
  4. Linked list Nodes joined by pointers
  5. Stack Last in, first out
  6. Queue First in, first out

Fact or Fib

  1. Fact A queue is first in, first out.
  2. Fib An adjacency matrix uses less memory than a list for a sparse graph. The fix: A matrix always uses n squared cells, so on a sparse graph it wastes far more memory than a list.
  3. Fib A linked list gives direct access to any item by its index. The fix: A linked list must be traversed from the start. Direct access by index is what an array gives.
  4. Fact Two keys that hash to the same index cause a collision.
  5. Fact Linear probing can cause clustering.
  6. Fact An adjacency list suits a sparse graph.
  7. Fib Pre order traversal visits the left subtree before the node. The fix: Pre order visits the node first, then the left subtree, then the right.
  8. Fib A hash table gets faster as its load factor rises. The fix: As the load factor rises, collisions become more frequent and performance falls, so the table is rehashed.
  9. Fact An array is stored in contiguous memory.
  10. Fact Post order traversal suits deleting a tree.
  11. Fib A circular queue wastes the space at the front of the array. The fix: That is the problem with a linear queue. A circular queue wraps its pointers round so the space is reused.
  12. Fib A binary search tree stays balanced whatever order items are inserted in. The fix: Inserting items in sorted order sends every item down the same side, so the tree degenerates into a linked list.