Question bank › A-level › A-level Computer Science › Fundamentals of data structures

Hash tables, dictionaries and vectors

10 questions53 marksabout 65 min

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

1Hard6 marks

A hash table has 11 locations, indexed 0 to 10. The hashing function is h(key) = key MOD 11. Collisions are handled by rehashing using linear probing (the next free location is used, wrapping round from 10 to 0).

Index012345678910
Key

(a) Show the contents of the hash table after the keys 23, 45, 70, 13, 81 are inserted in that order. [3]

(b) Explain the steps taken to search the table for the key 92 and state how many occupied locations are examined before the search ends. [2]

(c) State what is meant by a collision in a hash table. [1]

2Standard4 marks

The Python program below uses a dictionary.

words = "the cat sat on the mat the end".split()
counts = {}
for w in words:
    counts[w] = counts.get(w, 0) + 1
print(counts["the"], len(counts))

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

(b) Explain why a dictionary is a suitable data structure for counting how many times each word occurs in a very long document, rather than a list of [word, count] pairs. [2]

3Standard5 marks

The vectors u = [3, −1, 2] and v = [1, 4, −2] are defined over ℝ.

(a) Calculate u + v. [1]

(b) Calculate 2u − v. [1]

(c) Calculate the dot product u · v and state what the result tells you about the angle between the two vectors. [2]

(d) The vector w = 0.25u + 0.75v. Explain why w is a convex combination of u and v. [1]

Stuck on Hash tables, dictionaries and vectors?

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.

4Core4 marks

A program stores customer records in a hash table. Each record has a unique customer ID which is used as the key.

(a) Describe how a record is located in a hash table when its key is known. [2]

(b) State one other situation where a hash table could be used. [1]

(c) Explain why the designer of a hash table usually chooses to have more locations in the table than the number of records expected. [1]

5Standard5 marks

A hash table has 13 locations, indexed 0 to 12. Keys are words written in capital letters. The hashing algorithm adds together the ASCII codes of the characters in the key and then calculates the result MOD 13.

ASCII codes: A = 65, D = 68, E = 69, L = 76

(a) Calculate the hash value for the key LEAD. You must show your working. [2]

(b) Explain why the keys DEAL and LADE will both cause a collision with LEAD. [1]

(c) A programmer changes the algorithm so that the ASCII code of each character is first multiplied by its position in the word (the first character by 1, the second by 2, and so on) before the values are added and the result taken MOD 13.

Calculate the new hash values for LEAD and DEAL and explain why this algorithm is an improvement. [2]

6Hard6 marks

A hash table has 7 buckets, indexed 0 to 6. The hashing function is h(key) = key MOD 7. Collisions are handled by chaining: each bucket holds a pointer to a linked list of all the keys that hash to that bucket, with new keys added to the end of the list.

The following keys are inserted in this order: 18, 25, 9, 32, 14, 23

(a) Show the contents of each bucket after all six keys have been inserted. [2]

(b) The load factor of a hash table is the number of items stored divided by the number of buckets. Calculate the load factor of this table, giving your answer to 2 decimal places. [1]

(c) State how many key comparisons are needed to establish that the key 11 is not in the table. [1]

(d) Explain two advantages of chaining compared with rehashing using linear probing (open addressing). [2]

7Standard6 marks

A vector a is stored in a program as the dictionary

a = {0: 2.5, 1: -1.0, 2: 4.0}

(a) Using mathematical notation, state the set of vectors that a belongs to if all its components are real numbers. [1]

(b) Show how a could instead be represented as a list. [1]

(c) A vector can also be thought of as a function. Explain how a can be represented as a function, stating the domain and codomain of the function. [2]

(d) A point P in two dimensions is represented by the vector p = [2, −1]. P is first translated by the vector t = [3, 4] and the result is then scaled by the scalar 2.

Calculate the vector representing the final position of P. [2]

8Hard5 marks

The diagram shows two vectors u = [4, 1] and v = [1, 3] drawn as arrows from the origin. The angle between them is θ.

(a) Calculate the dot product u · v. [1]

(b) Use the dot product to calculate the angle θ. Give your answer in degrees to the nearest degree. [3]

(c) State a non-zero vector w that is perpendicular to u, and use the dot product to justify your answer. [1]

0112233445xyuvθ
9Standard6 marks

A search tool builds an index showing which pages of a short document contain each keyword. The Python program below uses a dictionary to do this.

pages = [["hash", "tree"], ["tree", "graph"], ["hash", "graph", "tree"]]
index = {}
for p in range(len(pages)):
    for word in pages[p]:
        if word in index:
            index[word].append(p + 1)
        else:
            index[word] = [p + 1]
print(index["tree"])
print(len(index))
print([n for n in index["hash"] if n in index["graph"]])

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

(b) For the dictionary index, state what is used as the key and what is stored as the value. [1]

(c) The document is extended to 5000 pages. Explain why using this dictionary to find the pages containing a given word is more efficient than searching through every page each time a word is looked up. [2]

10Stretch6 marks

A hash table has 10 locations, indexed 0 to 9. The hashing function is h(key) = key MOD 10. Collisions are handled by rehashing using linear probing (wrapping round from 9 to 0). A search stops when the key is found or an empty location is reached.

The keys 42, 52, 63, 72, 13 were inserted in that order, giving:

Index0123456789
Key4252637213

The key 52 is then deleted by setting location 3 back to empty.

(a) Explain why a subsequent search for the key 72 will fail, even though 72 is still in the table. [3]

(b) Describe how the deletion should be carried out so that searches still work correctly, and how the search and insertion algorithms must treat such a location. [2]

(c) When the table becomes too full it is replaced by a larger table with 23 locations and the hashing function is changed to key MOD 23. Explain why each key must be rehashed into the new table rather than copied to the same index. [1]

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