Question bank › IB Computer Science › B4 Abstract data types

Sets and hash tables (HL)

Answer on paper first, then reveal the mark scheme one mark at a time.

1StandardHL only4 marks

Consider the Python program below.

words = "the cat and the dog and the bird".split()
freq = {}
for w in words:
    freq[w] = freq.get(w, 0) + 1
print(freq)
print(max(freq, key=freq.get))

(a) State the output of the program. [3]

(b) Outline the purpose of freq.get(w, 0). [1]

2CoreHL only4 marks

A school stores club memberships as sets.

chess = {Ali, Bea, Cai, Dan}
robotics = {Bea, Dan, Eve}

(a) State the result of chess ∪ robotics (union). [1]

(b) State the result of chess ∩ robotics (intersection). [1]

(c) State the result of chess − robotics (difference). [1]

(d) Outline why a set, rather than a list, is a suitable ADT for storing the members of a club. [1]

3CoreHL only4 marks

A hash table is used to store student records, using the student ID as the key.

(a) Define the term hash function. [1]

(b) Define the term collision in the context of a hash table. [1]

(c) The hash table has 20 slots and currently holds 15 records. Calculate its load factor. [1]

(d) Explain one effect on performance of allowing the load factor to become high. [1]

Stuck on Sets and hash tables (HL)?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4StandardHL only5 marks

A hash table has 11 slots (indices 0 to 10). The hash function is h(k) = k mod 11 and collisions are resolved by linear probing (try the next index, wrapping from 10 to 0). The diagram shows the table after four keys have been inserted.

(a) Calculate h(58). [1]

(b) State the index at which 58 is stored, and the number of slots examined to store it. [2]

(c) The key 3 is then inserted. State the index at which it is stored. [1]

(d) Identify the problem with linear probing that this table illustrates. [1]

Hash table (11 slots)01225347414536678910index
5StandardHL only4 marks

A hash table with 7 slots uses the hash function h(k) = k mod 7. Collisions are resolved by separate chaining: each slot holds a linked list of the keys that hash to it. The diagram shows the table after seven keys have been inserted.

(a) State the index of the list to which the key 52 would be added. [1]

(b) Before 52 is added, a search is made for the key 38, which is not in the table. State the number of key comparisons needed to decide that 38 is not present. [1]

(c) Calculate the load factor of the table shown. [1]

(d) Four of the seven slots are empty, yet searches for some keys are slow. Explain why. [1]

Hash table with separate chaining02112931017243145456
6HardHL only7 marks

The Python program below uses sets to record the subject tags on a revision website.

tags = {"maths", "physics", "chemistry", "biology"}
needed = {"physics", "maths"}
print(needed <= tags)
print(tags >= {"maths", "art"})
tags.add("physics")
print(len(tags))
tags.discard("art")
tags.remove("biology")
print(sorted(tags))
print(sorted(needed - tags))

(a) State the five lines of output. [4]

(b) Construct a function is_subset(a, b) that returns True if every element of set a is also in set b, and False otherwise. You must not use the operators <=, >= or the methods issubset/issuperset. [3]

7HardHL only6 marks

A word game stores valid words in a hash table with 13 slots. The hash function adds the ASCII codes of the characters and takes the result mod 13.

ASCII codes: A = 65, C = 67, D = 68, G = 71, O = 79, T = 84

(a) Calculate the hash value of "CAT", showing your working. [2]

(b) Calculate the hash value of "DOG". [1]

(c) Explain why this hash function will always cause a collision between "CAT" and "ACT". [1]

(d) Suggest how the hash function could be improved to reduce collisions of this kind, and explain why it works. [2]

8HardHL only6 marks

A hash table has 8 slots and uses h(k) = k mod size with linear probing. The diagram shows the table after the keys 3, 11, 19, 6, 14 and 22 were inserted in that order.

The table is resized to 16 slots whenever an insertion would make the load factor greater than 0.75.

(a) Calculate the current load factor. [1]

(b) The key 30 is to be inserted next. Show that this causes the table to be resized. [1]

(c) The six keys are re-inserted into the new 16-slot table in their original order, using h(k) = k mod 16 and linear probing. State the new index of key 11 and of key 22. [2]

(d) Explain why each key must be re-hashed rather than copied to the same index in the larger table. [2]

Hash table (8 slots)220123311419566147index
9StandardHL only5 marks

A shop's stock system uses the Java code below.

HashMap<String, Integer> stock = new HashMap<>();
stock.put("pen", 12);
stock.put("ink", 5);
stock.put("pen", 7);
HashSet<String> sold = new HashSet<>();
sold.add("ink");
sold.add("pad");
sold.add("ink");
System.out.println(stock.size() + " " + stock.get("pen"));
System.out.println(stock.containsKey("pad") + " " + stock.getOrDefault("pad", 0));
System.out.println(sold.size());

(a) State the three lines of output. [3]

(b) The shop has 50 000 product codes. Compare the efficiency of checking whether a code is present using a HashSet with checking using an ArrayList. [2]

10StretchHL only6 marks

A hash table has 10 slots, uses h(k) = k mod 10 and resolves collisions with linear probing. The diagram shows the table after inserting 12, 22, 32 and 15 in that order.

The key 22 is then deleted by simply setting slot 3 back to empty.

(a) Explain why a subsequent search for 32 would incorrectly report that 32 is not in the table. [3]

(b) Describe how a "tombstone" (deleted marker) can be used to avoid this problem, for both searching and inserting. [2]

(c) Using the tombstone method instead, 22 is deleted and then 42 is inserted. State the index at which 42 is stored. [1]

Hash table (10 slots) before deletion011222233241556789index

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session