Practice

Hash Table Insert and Lookup

2025/Oct/Nov·Variant 1·Q3·[14 marks]
HARDHashing

Records are stored in a 2D hash table of 10 rows by 3 columns. The hash of a key is MOD(Key, 10) — that result is the row. If two keys hash to the same row, the later record uses the next free column in that row.

Empty slots store the key -1.

Write:

  • function Hash(Key) — return MOD(Key, 10)
  • procedure InsertData(Key, Data) — store the pair in the first empty column of the hashed row
  • function GetRecord(Key) — return the matching data, or "Not found"

The harness reads how many records to insert, then each key and its string, then how many lookups, then each lookup key.

Input: Insert count, key/data pairs, lookup count, then keys. Output: One lookup result per line.

Example:

Input:  3
        15
        alpha
        25
        bravo
        12
        charlie
        3
        25
        12
        99
Output: bravo
        charlie
        Not found
Premium is coming soon. All grading features are currently unlocked.

Sample Test Cases

Test 1: Collision then miss
Inputs: 3, 15, alpha, 25, bravo, 12, charlie, 3, 25, 12, 99
Expected: bravo charlie Not found
Test 2: Single record
Inputs: 1, 7, solo, 2, 7, 8
Expected: solo Not found