GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Recover insertion constraints from hash table

2 questions solved the same way, newest first. Back to Data Structures

How these are solved

For each displaced key require earlier occupancy along its linear probe path; validate candidate insertion orders or count their linear extensions. Same final table, different validation versus counting outputs; not duplicates.

2 marks · MCQ
Report issue

Linear-probing insertion sequence

Hashing

Question 52, GATE 2010: A hash table has length ten, hash function h(k)=k mod 10, open addressing and linear probing. After six insertions into the initially empty table, slots 2,3,4,5,6,7 contain 42,23,34,52,46,33 respectively; slots 0,1,8,9 are empty. Which sequence could have produced this table?
Options: (A) 46,42,34,52,23,33; (B) 34,42,23,52,33,46; (C) 46,34,42,23,52,33; (D) 42,46,33,23,34,52.
Question 52, GATE 2010: A hash table has length ten, hash function h(k)=k mod 10, open addressing and linear probing. After six insertions into the initially empty table, slots 2,3,4,5,6,7 contain 42,23,34,52,46,33 respectively; slots 0,1,8,9 are empty. Which sequence could have produced this table?
Options: (A) 46,42,34,52,23,33; (B) 34,42,23,52,33,46; (C) 46,34,42,23,52,33; (D) 42,46,33,23,34,52.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Simulate option C: 46 hashes to slot 6, 34 to 4, 42 to 2, and 23 to 3. Then 52 starts at 2 and probes occupied slots 2,3,4, stopping at 5. Finally 33 starts at 3 and probes occupied slots 3,4,5,6, stopping at 7. This exactly reproduces the figure. In A, 52 is inserted before 23 and takes slot 3; in B, 33 is inserted before 46 and takes slot 6; in D, 33 takes slot 3 before 23. Those disagree with the shown table, leaving C.

Linear probing tests h(k),h(k)+1,… modulo ten until an empty slot is reached. This means final position depends on insertion order, not only on the hash value. In the valid order, the four home-slot keys 42,23,34,46 establish the required occupied run before the displaced keys arrive. Neither 52 nor 33 wraps around the table in this instance.

Original paper, page 13

2 marks · MCQ
Report issue

Count insertion sequences for a hash table

Hashing

Question 53, GATE 2010: A hash table has length ten, hash function h(k)=k mod 10, open addressing and linear probing. After six insertions into the initially empty table, slots 2,3,4,5,6,7 contain 42,23,34,52,46,33 respectively; slots 0,1,8,9 are empty. How many distinct insertion permutations of these six keys produce this table?
Options: (A) 10; (B) 20; (C) 30; (D) 40.
Question 53, GATE 2010: A hash table has length ten, hash function h(k)=k mod 10, open addressing and linear probing. After six insertions into the initially empty table, slots 2,3,4,5,6,7 contain 42,23,34,52,46,33 respectively; slots 0,1,8,9 are empty. How many distinct insertion permutations of these six keys produce this table?
Options: (A) 10; (B) 20; (C) 30; (D) 40.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

For 52 to reach slot 5, the final occupants 42,23,34 of slots 2,3,4 must already be present. For 33 to reach slot 7, occupants 23,34,52,46 of slots 3,4,5,6 must already be present. Together these constraints make 33 last, and require 42,23,34 before 52; 46 can be anywhere among the first five. Among permutations of the first five keys, 52 must be last among {42,23,34,52}, which happens in 1/4 of the 5! orders. Thus 5!/4=30, option C. These constraints are sufficient because the home-slot keys then occupy their intended slots. Exhaustive simulation of all 720 permutations independently yields 30.

A second count inserts 46 into a sequence containing the other five keys. The first three among those five are 42,23,34 in any of 3! orders, followed by 52 and then 33. The 46 key may be inserted in any of the five gaps before 33, giving 3!×5=30. Inserting it after 33 would allow 33 to occupy slot 6 instead of 7.

Original paper, page 13