Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern

Keys 38, 13, 87, 73, 18, 97, 3, 68, 72, 98 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a table of size 7 (buckets 0 to 6). What are the final bucket contents, and what is the longest chain length?

  1. A. 18, 97, 3, 38, 87, 73, 13 — longest chain 1
  2. B. 98, ∅, 72, 38→87→73→3, 18, 68, 13→97 — longest chain 4
  3. C. 98, ∅, 72, 38→87→73→3, 18, 68, 13→97 — longest chain 3
  4. D. 98, ∅, 72, 3→73→87→38, 18, 68, 97→13 — longest chain 4

Correct answer: B. 98, ∅, 72, 38→87→73→3, 18, 68, 13→97 — longest chain 4

Each key is appended to the chain at index k mod 7 in insertion order, giving buckets [98, ∅, 72, 38→87→73→3, 18, 68, 13→97], whose longest chain has 4 key(s). This differs from open addressing (linear probing), which would spread keys like [18, 97, 3, 38, 87, 73, 13] into single-slot buckets instead of chains.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic