Data Structures and Algorithms (DSA27) · Week 13

Hash Tables (Lecture 13)

These quizzes are for self-study. Your answers are saved on this device only and are not sent anywhere.

Score: 0 / 22 · Answered: 0

  1. W13-M01 Recall

    A hash table finds the slot of a key by computing:

  2. W13-M02 Apply

    In a table of capacity 8, the index of the key 45 is:

  3. W13-M03 Recall

    The one rule every hash function must obey is:

  4. W13-M04 Explain

    python -c "print(hash('abc'))" prints a different number each time you run it because:

  5. W13-M05 Apply

    Which of these is the same in every run of Python?

  6. W13-M06 Explain

    m.put([1, 2], "x") raises TypeError: unhashable type: 'list' because:

  7. W13-M07 Apply

    23 keys are placed in 365 slots at random. The probability that at least two share a slot is about:

  8. W13-M08 Recall

    The load factor of a hash table is:

  9. W13-M09 Apply

    A ChainingHashMap() receives put for 5, 13 and 21. Bucket 5 is:

  10. W13-M10 Apply

    In the table of W13-M09, get(29) compares 29 with how many entries before raising KeyError?

  11. W13-M11 Apply

    A ChainingHashMap() receives distinct keys one at a time. The first resize happens during the put of the:

  12. W13-M12 Explain

    _resize cannot copy old[i] into new[i] because:

  13. W13-M13 Apply

    An OpenAddressingHashMap() receives put for 6, 14 and 22. Key 22 is in slot:

  14. W13-M14 Apply

    In the table of W13-M13, get(30, None) makes how many probes?

  15. W13-M15 Explain

    In open addressing, delete leaves a tombstone instead of None because:

  16. W13-M16 Apply

    get meets a tombstone while probing. It:

  17. W13-M17 Explain

    put counts tombstones in its load check because:

  18. W13-M18 Recall

    Primary clustering is:

  19. W13-M19 Apply

    For linear probing with a good hash, Knuth's formula for an unsuccessful search is 1/2(1 + 1/(1-α)2). At α = 0.5 it gives:

  20. W13-M20 Explain

    The keys 0, 1024, 2048, 3072, … (n of them) are put into a ChainingHashMap, whose capacity is always a power of two. While the capacity is at most 1,024, one get costs:

  21. W13-M21 Recall

    Python's dict is:

  22. W13-M22 Explain

    Which question does a sorted array answer in O(log n) but a hash table cannot answer faster than O(n)?

18 marks

Written questions

5 written questions (18 marks). Write your answer first, then open the model answer and its marking points, and mark yourself honestly: each point is worth about one mark.

Written total: 0 / 18

  1. W13-E1 Explain 4 marks

    Explain how a hash table finds a key in O(1) time on average. State the assumption behind "on average", give the worst case, and name three things that can push a real table towards it.

  2. W13-E2 Explain 3 marks

    A student's class defines __hash__ to return 0 for every object. Is a hash table of these objects wrong, or only slow? Answer for chaining and for open addressing, and give the cost of get and of building a table of n keys.

  3. W13-E3 Apply 4 marks

    Compare chaining and open addressing: what a slot holds, the load factors each can tolerate, how each deletes, and one practical advantage of each.

  4. W13-E4 Explain 4 marks

    Explain tombstones. Why can an open-addressing delete not simply empty the slot? State how get, put and _resize each treat a tombstone, and why the load check must count them.

  5. W13-E5 Explain 3 marks

    Show that a hash table that doubles its capacity when the load factor passes a fixed limit does amortised O(1) work per put on rehashing. What happens if it grows by a fixed 8 slots instead?