Computer Science I / Choosing a Data Structure by Workload
Practice question · Sort into groups

Sort each requirement by whether a hash table alone is enough for it.

Groups: A hash table handles this well · Needs an ordered structure instead

Hints
  1. A hash table can answer questions about one key it is given, and nothing about how keys relate to each other.
  2. Three of these requirements mention a comparison between keys rather than a single exact key.
Show the answer

A hash table handles this well: Fetch the record stored under a known id, Check whether an id has already been used, Delete the record stored under a known id

Needs an ordered structure instead: Report every id between 1000 and 2000, Print all records in ascending id order, Find the smallest id greater than a given one

Why

Exact-key fetch, membership and deletion are all single-key questions, which is precisely what hashing makes constant. Ranges, sorted output and successor queries all compare keys to one another, and hashing destroys that relationship by design. The dividing question is always: does the operation name one key, or a relation between keys?

Read the lesson: Choosing a Data Structure by Workload →

Practise Choosing a Data Structure by Workload

The app has 5 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Choosing a Data Structure by Workload