levo

Fundamentals of Data Structures

Hash tables

  • 2 things to know
  • Paper 1

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.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

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

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

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

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

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

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

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.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

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