Quiz
1.4.2 Data structures
-
Which structure is immutable?
Show answer
Answer: Tuple
A tuple's items cannot be changed, added or removed after it has been created. Lists, arrays and stacks are mutable: their contents can change.
-
Which statement about arrays is true?
Show answer
Answer: An array's size is fixed when it is created
An array is a static structure: its size is set when it is declared. Its elements all have the same type and, in OCR pseudocode, its indexes start at 0. A list is the structure that can grow and hold mixed types.
-
Items 4, 9 and 2 are pushed onto an empty stack in that order. Then
pop()is called twice. Which item is left on the stack?Show answer
Answer: 4
A stack is last in, first out. The first pop returns 2 (the last item pushed) and the second returns 9, leaving 4, which was pushed first.
-
A circular queue is stored in an array with
maxSize6 (positions 0 to 5). The rear pointer is 5. Where does the next item go?Show answer
Answer: Position 0
The new rear is
(5 + 1) MOD 6, which is 0, so the pointer wraps round to the start of the array. The queue is only full if all 6 spaces hold items, which the rear pointer alone does not tell you. -
Why is inserting an item into the middle of a linked list quicker than inserting it into the middle of an array?
Show answer
Answer: Only pointers change; no items have to move
To insert into a linked list you store the item in a free node and change two pointers. In an array, every item after the insertion point must move along one place.
-
Which data structure would be best for an undo feature, which reverses the most recent action first?
Show answer
Answer: A stack
Undo needs the most recent action first: last in, first out, which is how a stack works. A queue would undo the oldest action first.
-
The values 40, 25, 60, 30 and 10 are added in that order to an empty binary search tree. Where does 30 go?
Show answer
Answer: Right child of 25
Start at the root, 40. 30 is smaller, so go left to 25. 30 is larger than 25, so it becomes 25's right child.
-
Which traversal of a binary search tree outputs its items in ascending order?
Show answer
Answer: In-order
In-order visits the left subtree, then the node, then the right subtree. In a binary search tree everything on the left is smaller and everything on the right is larger, so the items come out sorted.
-
In a binary search tree, a node with two children is removed. Which value can replace it?
Show answer
Answer: The smallest value in its right subtree
The smallest value in the right subtree is the node's in-order successor: it is larger than everything on the left and smaller than everything else on the right, so putting it in the gap keeps the tree in order. (The largest value in the left subtree would also work.) The largest value in the right subtree would be bigger than the rest of that subtree, breaking the rule.
-
A graph has 1000 vertices but each vertex is joined to only 3 others. Which way of storing it uses less memory?
Show answer
Answer: An adjacency list
The graph is sparse. An adjacency matrix needs 1000 × 1000 cells whether or not edges exist, while an adjacency list stores only the 3 edges of each vertex.
-
A hash table has 11 spaces and the hashing function
key MOD 11. Space 3 already holds an item, and space 4 is empty. Using linear probing, where is the key 25 stored?Show answer
Answer: Space 4
25 MOD 11is 3, but space 3 is taken: a collision. Linear probing puts the item in the next free space, which is 4. -
Which structure is used to keep track of vertices still to visit in a breadth-first traversal of a graph?
Show answer
Answer: A queue
Breadth-first visits vertices in the order they were discovered, nearest first, which is first in, first out: a queue. A depth-first traversal uses a stack.