📄 Paper 1 · 4.2 Data Structures
⭐ Pro
4.2.6 Hash Tables
AQA 7517 · A-Level Computer Science · ~16 min read

What is a Hash Table?

A hash table (also called a hash map) is a data structure that stores key-value pairs. It uses a hash function to compute an index (the hash value) into an array of buckets, from which the desired value can be found. Goal: O(1) average-case lookup.

Hash Functions

A hash function takes a key and returns an integer index within the table size.

// Simple modulo hash function:
hash(key) = key MOD tableSize

// Example: tableSize = 10
hash(23) = 23 MOD 10 = 3   → store at index 3
hash(47) = 47 MOD 10 = 7   → store at index 7
hash(33) = 33 MOD 10 = 3   → COLLISION! index 3 already used

A good hash function: fast to compute, distributes keys uniformly, minimises collisions.

Collisions

A collision occurs when two different keys produce the same hash value (same index). Collisions are inevitable — the hash function maps a large key space to a small index space.

Collision Resolution

1. Linear Probing (Open Addressing)

If the desired slot is occupied, check the next slot sequentially until an empty slot is found.

// Linear probing with tableSize = 10
hash(23) = 3  → store at index 3
hash(33) = 3  → index 3 occupied, try 4 → store at index 4
hash(43) = 3  → index 3 occupied, try 4 occupied, try 5 → store at index 5

Problem: clustering — long sequences of filled slots form, degrading performance.

2. Chaining (Separate Chaining)

Each slot in the table holds a linked list (or another structure). Colliding items are added to the same list.

// Chaining:
index 3 → [23] → [33] → [43] → null

Advantage: table never "fills up". Disadvantage: extra memory for linked lists; cache-unfriendly.

Hash Table Operations

OperationAverageWorst case
SearchO(1)O(n) — if many collisions
InsertO(1)O(n)
DeleteO(1)O(n)

Applications

  • Implementing dictionaries / symbol tables in compilers
  • Database indexing
  • Caching (e.g. DNS lookups)
  • Password hashing (cryptographic hash functions)
Exam tip: Be able to apply a hash function to a set of keys, identify collisions, and show how linear probing resolves them. Know that average case is O(1) but worst case is O(n). Know both chaining and linear probing as collision resolution methods.
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.2.6 Hash Tables

8 questions · instantly marked · AQA 7517 standard

Q1What is a hash table and what is the purpose of a hash function?[3]
✅ Mark scheme
Mark scheme
Hash table — a data structure that stores key-value pairs [1]; using a hash function to compute an index into an array [1]; aiming for O(1) average lookup time [1].
Q2Using hash(key) = key MOD 7, find the hash values for: 15, 22, 36, 43.[4]
✅ Mark scheme
Mark scheme
hash(15)=15 MOD 7=1 [1]; hash(22)=22 MOD 7=1 [1]; hash(36)=36 MOD 7=1 [1]; hash(43)=43 MOD 7=1 [1]. (All map to 1 — a collision-heavy example)
Q3What is a collision in a hash table? Why are collisions inevitable?[2]
✅ Mark scheme
Mark scheme
A collision is when two different keys produce the same hash value/index [1]; collisions are inevitable because the hash function maps a large key space to a much smaller index range (pigeonhole principle) [1].
Q4Explain how linear probing resolves collisions. What problem can result from linear probing?[3]
✅ Mark scheme
Mark scheme
If a slot is occupied, the next slot(s) are checked sequentially until an empty slot is found [1]; the item is placed in the next available slot [1]; clustering — long runs of occupied adjacent slots form, degrading performance [1].
Q5Explain chaining as an alternative collision resolution technique.[2]
✅ Mark scheme
Mark scheme
Each bucket/slot in the hash table holds a linked list (or chain) [1]; all items that hash to the same index are added to that bucket's linked list [1].
Q6State the average-case and worst-case time complexity for searching a hash table. What causes the worst case?[3]
✅ Mark scheme
Mark scheme
Average: O(1) [1]; worst: O(n) [1]; worst case caused by all keys hashing to the same index (many collisions) — requiring a full linear scan [1].
Q7State three properties of a good hash function.[3]
✅ Mark scheme
Mark scheme
Fast to compute [1]; distributes keys uniformly across the table [1]; minimises collisions [1]; deterministic (same key always produces same hash) [1] — any three.
Q8Give two real-world applications of hash tables.[2]
✅ Mark scheme
Mark scheme
Any two: compiler symbol tables [1]; database indexing [1]; caching (DNS lookups) [1]; password hashing [1]; implementing dictionaries in programming languages [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 10
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Hash Tables

10 questions · 10 minutes

← 4.2.5 Trees
16 of 70 · AQA 7517
4.2.7 Dictionaries →