Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2021/imperative-programming/5/b/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2021 Imperative programming 5 b Solution by
Codex 0 2026-10-25
Assume the supplied hash function is stable and produces valid table indices. The representation invariant places each name in its hashed bucket, permits at most one entry per name, and requires finite acyclic, disjoint bucket chains.
def store(name: String, number: String): Unit = {
val bucket = hash(name)
val entry = find(name, table(bucket))
if (entry == null) {
table(bucket) = new Entry(name, number, table(bucket))
} else {
entry.number = number
}
}If the key exists, updating its associated number implements map override without creating a duplicate entry. Otherwise a new entry is prepended to the correct bucket and leaves every existing mapping unchanged. Its immutable name preserves bucket membership. Thus the mapping afterwards is the old mapping overridden at precisely the supplied key. Hash collisions between different names do not imply key equality and are handled by separate chaining.
New to topics? Read the docs here!