Consider a hash table of size m = 10000 and the hash function h(k) = ⌊m (kA…

2011

Consider a hash table of size m = 10000 and the hash function h(k) = ⌊m (kA mod 1)⌋ for A = (√5 − 1)/2. The location for the key k = 123456 is

Answer: C. 41Concept: The multiplication method of hashing uses a constant A with 0 < A < 1. For a key k it forms the product kA, discards the integer part and keeps only…

  1. A.

    46

  2. B.

    47

  3. C.

    41

  4. D.

    43

Attempted by 49 students.

Show answer & explanation

Correct answer: C

Concept: The multiplication method of hashing uses a constant A with 0 < A < 1. For a key k it forms the product kA, discards the integer part and keeps only the fractional part kA mod 1, which always lies in [0, 1); scaling that fraction by the table size m and taking the floor gives an index in {0, 1, …, m − 1}. Because only the fractional part is used, m may be any convenient value and need not be prime, and Knuth’s suggested constant A = (√5 − 1)/2 ≈ 0.6180339887, the golden-ratio conjugate, spreads consecutive keys evenly across the table.

Applying the method to this key, step by step:

  1. A = (√5 − 1)/2 = 0.6180339887498949, since √5 = 2.2360679774997896.

  2. kA = 123456 × 0.6180339887498949 = 76300.004115107018…

  3. kA mod 1 = 76300.004115107018… − 76300 = 0.004115107018…

  4. m × (kA mod 1) = 10000 × 0.004115107018… = 41.15107018…

  5. h(k) = ⌊41.15107018…⌋ = 41.

Cross-check: only the leading digits of the fractional part matter here, because 0.004115… scaled by 10000 lies strictly between 41 and 42, so no extra precision can move the floor to a different integer. The value also satisfies the range guarantee 0 ≤ h(k) ≤ m − 1 = 9999. Note that the constant must be carried to full precision: truncating it to A ≈ 0.618 gives 123456 × 0.618 = 76295.808, whose fractional part 0.808 would scale to a completely different index.

Therefore the location for the key k = 123456 is 41.

Explore the full course: Tpsc Assistant Technical Officer

Loading lesson…