levo

Fundamentals of Data Structures

Hash tables

  • 2 things to know
  • Paper 1
Start revising · £5/month

Overview

Recall questions on hashing algorithms, working out where a key is stored, why collisions are unavoidable, and how rehashing resolves them.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

What is a collision?

Process

How does rehashing deal with two keys competing for one slot?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a hash table?

    Model answer

    A data structure that creates a mapping between keys and values. It stores items in an array of slots, and the position of each item is calculated directly from its key by a hashing algorithm, so an item can be found without searching through the other items.

  2. 2

    What is a hashing algorithm?

    Model answer

    An algorithm that takes a key and calculates from it an integer, the hash, which is used as the index of the slot in the table where the item associated with that key is stored. The same key always produces the same hash, and the hash must always fall within the range of valid indices for the table.

  3. 3

    Give four typical uses of a hash table.

    Model answer

    Indexing records in a database so a record can be retrieved directly from its key rather than by searching. Storing a compiler's symbol table, so the details of an identifier can be looked up quickly. Implementing a dictionary or associative array in a programming language. Caching, where a page or result is stored under a hash of its address.

  4. 4

    A hash table has 10 slots, indices 0 to 9, and uses h(k) = k MOD 10. Where are the keys 27, 13 and 82 stored?

    Model answer

    27 MOD 10 = 7, so the item with key 27 goes in slot 7. 13 MOD 10 = 3, so key 13 goes in slot 3. 82 MOD 10 = 2, so key 82 goes in slot 2.

  5. 5

    Apply the hashing algorithm 'sum the ASCII codes of the characters, then take the result MOD 10' to the key CAT.

    Model answer

    C is 67, A is 65 and T is 84, so the sum is 67 + 65 + 84 = 216. 216 MOD 10 = 6, so the item with key CAT is stored in slot 6.

  6. 6

    What is a collision?

    Model answer

    A collision occurs when two different key values compute the same hash, so the hashing algorithm sends both items to the same slot in the table. Only one item can occupy a slot, so the table must have a strategy for dealing with the second.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A driver is .
softwarehardwarestorage

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Fundamentals of Data Structures, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.