Skip to main content
← AT CS Study Guide

Unit 5 · Heavy

Sets, Maps, and Hash Tables

Lookup by key instead of by position. Hashing is the first place where average and worst case diverge sharply, which makes stating the case a required part of every answer.

What a strong answer looks like

A strong Unit 5 answer says which case it is describing and names the assumption about the hash function that makes the average case hold.

Topics in this unit

1

Sets and Maps as Abstractions

Know

A set stores membership without duplicates; a map stores key-to-value associations. Both are specifications, implementable by hashing or by a tree.

Apply

Choose a set when only membership matters and a map when each key carries data.

Watch out

Assuming iteration order. A hash-based structure gives no ordering guarantee.

Study move

Pick a problem for each and say why the other would be the wrong fit.

2

Hash Functions

Know

A hash function maps a key to a bucket. A good one spreads keys evenly, which is exactly the assumption behind constant average lookup.

Apply

State the even-distribution assumption whenever you quote constant-time behavior.

Watch out

Quoting constant time as if unconditional. With every key colliding, lookup is linear.

Study move

Describe a hash function that performs badly on a realistic key set and explain why.

3

Collision Resolution

Know

Chaining hangs a secondary structure off each bucket; open addressing probes for another slot. Linear probing suffers primary clustering, where occupied runs lengthen future probes.

Apply

Compare the two by memory behavior and by how gracefully they degrade as the table fills.

Watch out

Treating a collision as an error. Collisions are expected and handled, not prevented.

Study move

Insert a small set of colliding keys under both strategies and compare the probe counts.

4

Load Factor and Resizing

Know

Load factor is entries divided by buckets, and rising load predicts more collisions. Implementations resize past a threshold, rehashing every key.

Apply

Explain resizing as an amortised cost: occasional linear work spread across many insertions.

Watch out

Ignoring load factor when reasoning about performance, which makes the constant-time claim unfalsifiable.

Study move

State what happens to load factor and to cost when a table doubles its buckets.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Naming the case whenever quoting hash performance
  • Treating collisions as normal rather than exceptional
  • Connecting load factor to expected cost

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • Tree-backed maps. Some sections contrast hash maps with ordered tree maps in depth.
  • Custom hash functions. Writing one is required in some courses and only discussed in others.

Mastery checklist

  • State average and worst case for hash lookup.
  • Explain what a good hash function guarantees.
  • Compare chaining with probing.
  • Define load factor and say what it predicts.

Check yourself

  • What assumption makes hash lookup constant on average?
  • What is primary clustering and which strategy suffers it?
  • Why is resizing an amortised rather than a per-operation cost?

Modeling drill

Design the key for a map storing student records so that lookups stay fast, and say what would go wrong with a poorly chosen key.

SetMapHash functionBucketCollisionChainingProbingLoad factor