Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Hash Tables and Dictionaries

4.2.6 Hash tables·4.2.7 Dictionaries

Aligned to the AQA 7517 specification

Level
Intermediate
Reading time
6 min
Published
13 June 2026
Updated
1 July 2026
On this page
  1. 1.Hash Tables
  2. 2.Hash Functions
  3. 3.Collisions — Chaining and Open Addressing
  4. 4.Load Factor and Rehashing
  5. 5.Dictionaries
  6. 6.Dictionary Implementations: Hash Table vs BST
  7. 7.Common Exam Mistakes

Key takeaways

  • A hash table maps keys to values by using a hash function to compute an array index, giving insert, lookup and delete an average time complexity of O(1).
  • Hash table operations are O(1) only on average; in the worst case where all keys collide to one slot, lookup is O(n).
  • A collision is when two different keys hash to the same index; it can be resolved by chaining (a linked list per slot), linear probing (try the next slot), or quadratic probing (offsets +1², +2², +3², ...).
  • Load factor is the number of entries divided by the table size; when it exceeds a threshold (around 0.7) rehashing doubles the table and reinserts every entry because the modulus has changed.
  • A dictionary is an abstract data type of unique key-value pairs defined by its operations, and AQA requires knowing it can be implemented by a hash table (average O(1)) or a binary search tree (average O(log n), keys in sorted order).

Hash Tables

A hash table (hash map) is a data structure that maps keys to values using a hash function to compute an index into an underlying array.

KeyHash function gives indexStored at
"Alice"3slot 3
"Bob"7slot 7
"Carol"1slot 1

Operations:

  • insert(key, value): hash the key, store value at that index
  • lookup(key): hash the key, retrieve value at that index
  • delete(key): hash the key, remove value at that index

All three operations are on average — this is the key advantage of hash tables over arrays and sorted lists.

Hash Functions

A hash function takes a key and produces an integer index within the bounds of the array. A good hash function:

  1. Is deterministic — same key always produces same index
  2. Distributes keys uniformly across available slots
  3. Is fast to compute

Simple example — hash a string by summing character codes modulo table size:

FUNCTION hashString(key, tableSize)
    total ← 0
    FOR i ← 1 TO LEN(key)
        total ← total + ASC(SUBSTRING(key, i, 1))
    NEXT i
    RETURN total MOD tableSize
ENDFUNCTION

Worked example — table size 10, key = "Ali":

  • ASC('A') = 65, ASC('l') = 108, ASC('i') = 105
  • total = 65 + 108 + 105 = 278
  • index = 278 MOD 10 = 8

Collisions — Chaining and Open Addressing

A collision occurs when two different keys hash to the same index. Collisions are inevitable for large datasets (infinitely many possible keys, finite table slots).

Chaining (separate chaining)

Each slot holds a linked list of all entries that hash to that slot. On collision, the new entry is appended to the list.

Worst case: all keys hash to one slot → lookup.

Open addressing — linear probing

When a collision occurs, probe the next slot sequentially until an empty slot is found. For example, inserting "Dave" hashes to slot 3, which is occupied, so probing tries slot 4; that slot is empty, so "Dave" is stored there.

Probe sequence: index, index+1, index+2, … wrapping around the table.

Open addressing — quadratic probing

Instead of probing sequentially, the offset grows quadratically: , , , … So a collision at slot 3 probes slot , then slot , then slot , and so on.

Quadratic probing reduces primary clustering (the bunching effect in linear probing where long filled runs form, slowing future insertions).

Load Factor and Rehashing

Load factor = (number of entries) / (table size). As the load factor rises, collision frequency increases and performance degrades.

Load factorEffect
< 0.5Low collision rate; good performance
~ 0.7Acceptable threshold; typical rehash trigger
> 0.9Frequent collisions; severe performance degradation

Rehashing doubles the table size and reinserts all entries when the load factor exceeds a threshold. All keys must be rehashed because the table size (the modulus) has changed.

How much of this have you taken in?

Quiz yourself on this section, free, no card needed.

Test myself

Dictionaries

A dictionary (also called a map or associative array) is an abstract data type that stores an unordered collection of key-value pairs where each key is unique.

Operations:

  • set(key, value): add or update the value for a key
  • get(key): retrieve the value associated with a key
  • delete(key): remove a key-value pair
  • contains(key): return True if the key exists
student_grades ← {}                // Empty dictionary

student_grades["Alice"] ← 90      // set
student_grades["Bob"]   ← 85

OUTPUT student_grades["Alice"]     // get → 90

student_grades["Alice"] ← 92      // update

IF "Bob" IN student_grades THEN   // contains
    DELETE student_grades["Bob"]   // delete
ENDIF

A dictionary is an abstract data type — the interface is defined by its operations (get, set, delete), not its implementation. The same ADT can be implemented in different ways.

Dictionary Implementations: Hash Table vs BST

AQA requires knowledge of two common dictionary implementations:

Hash tableBinary search tree (BST)
Average lookup
Worst-case lookup (all keys collide) (degenerate/unbalanced tree)
Key orderingUnorderedKeys stored in sorted order
Range queriesNot efficientEfficient (in-order traversal)
MemoryRequires pre-allocated tableDynamic; no wasted space
Best forFast single-key lookupOrdered iteration or range queries

Hash table implementation: hash the key to find the index directly. Average .

BST implementation: store key-value pairs as BST nodes, keys determining position. Lookup traverses the tree: go left if key < current node, right if key > current node. average.

Worked example — storing word frequencies:

freq ← {}
FOR each word IN document
    IF word IN freq THEN
        freq[word] ← freq[word] + 1
    ELSE
        freq[word] ← 1
    ENDIF
NEXT

A hash table implementation gives per word; a BST implementation gives per word but keeps words in alphabetical order if traversed in-order.

Common Exam Mistakes

1. Claiming hash tables are always O(1)

Hash table operations are on average, assuming a good hash function and low load factor. In the worst case (all keys collide to one slot), lookup is .

2. Confusing the dictionary ADT with its implementation

A dictionary is defined by its operations. A hash table is one implementation; a BST is another. Describing a dictionary as "a hash table" conflates the abstraction with one particular implementation.

3. Forgetting quadratic probing in open-addressing questions

AQA names both linear and quadratic probing. Linear probing uses sequential slots (+1, +2, +3, …); quadratic uses growing offsets (+1, +4, +9, …). Confusing the two or omitting quadratic loses marks.

4. Not accounting for collisions in exam traces

When tracing a hash table insertion, check whether the computed slot is already occupied. Apply the collision resolution strategy (chaining, linear probing, or quadratic probing) as specified by the question.

Key terms

Hash table
A data structure that maps keys to values by using a hash function to compute an index into an underlying array.
Hash function
A function that takes a key and produces an integer index within the array bounds; it should be deterministic, uniform, and fast.
Collision
The situation where two different keys hash to the same index.
Chaining
A collision strategy where each slot holds a linked list of all entries that hash to it.
Linear probing
An open-addressing strategy that probes the next slots sequentially (+1, +2, +3, ...) until an empty slot is found.
Quadratic probing
An open-addressing strategy whose probe offset grows quadratically (+1², +2², +3², ...), reducing primary clustering.
Primary clustering
The bunching effect in linear probing where long filled runs form and slow future insertions.
Load factor
The number of entries divided by the table size; higher values mean more collisions and worse performance.
Rehashing
Doubling the table size and reinserting all entries when the load factor exceeds a threshold, since the modulus has changed.
Dictionary
An abstract data type storing an unordered collection of unique key-value pairs, also called a map or associative array.
Abstract data type
A type defined by its operations (interface) rather than its implementation.

Frequently asked questions

No. Hash table insert, lookup and delete are O(1) only on average, assuming a good hash function and a low load factor. In the worst case, where all keys collide to one slot, lookup degrades to O(n).

Both are open-addressing collision strategies. Linear probing tries the next slots sequentially (+1, +2, +3, ...), while quadratic probing uses growing offsets (+1², +2², +3², ...). Quadratic probing reduces primary clustering, the bunching of long filled runs in linear probing.

A dictionary is an abstract data type defined by its operations (get, set, delete, contains) on unique key-value pairs. A hash table is one implementation of it; a binary search tree is another. Describing a dictionary simply as a hash table confuses the abstraction with one implementation.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Binary Search Trees

Related lessons

8 min

Lesson

Binary Search Trees

A-Level Computer Science · AQA 7517

1 month ago

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.