Hashing Questions with Answers

66 practice questions on Hashing (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 66 free in the AptiNest app.

Sample questions

Question 1Easy

Using a frequency map, what is the first non-repeating character of "swisstown"?

  1. i
  2. w
  3. s
  4. t

Answer: A. i

Explanation

Count all characters, then scan again from the left; 'i' is the first with count 1.

Step-by-step solution

  1. Counts: s 3, w 2, i 1, t 1, o 1, n 1.
  2. Scan in order: s, w, i -> i is the first with count 1.
  3. Answer i.

Why the other options are wrong

  • B. w appears twice.
  • C. s appears three times.
  • D. t also appears once but comes after i.

Tip: First unique: count pass, then order pass.

Question 2Easy

A hash table with 50 slots holds 35 keys. What is its load factor?

  1. 15
  2. 0.7
  3. 1.43
  4. 0.35

Answer: B. 0.7

Explanation

Load factor alpha = n/m
= 35/50 = 0.7.

Step-by-step solution

  1. Load factor alpha = keys / slots.
  2. 35 / 50 = 0.7.
  3. So 0.7.

Why the other options are wrong

  • A. 15 is the number of free slots.
  • C. 1.43 is slots / keys, inverted.
  • D. 0.35 divides by 100 instead of 50.

Tip: alpha = n / m.

Question 3Easy

A hash table doubles its size and rehashes all keys when the load factor exceeds 0.75. What is the amortised cost of an insertion?

  1. O(1)
  2. O(n log n)
  3. O(n)
  4. O(log n)

Answer: A. O(1)

Explanation

Rehashing n keys happens only after about n cheap insertions, so the cost spreads to O(1) each.

Step-by-step solution

  1. Doubling costs O(n) but happens rarely.
  2. Total copy work over n inserts is n + n/2 + n/4 + ... < 2n.
  3. So O(1) amortised.

Why the other options are wrong

  • B. Not that large.
  • C. That is the cost of a single resize.
  • D. No logarithmic factor arises.

Question 4Easy

Which operation is NOT efficient in a standard hash table?

  1. Deletion by key
  2. Lookup by key
  3. Finding all keys in a sorted range
  4. Insertion

Answer: C. Finding all keys in a sorted range

Explanation

Hashing scatters keys, so range queries need a full scan; use a balanced BST or B-tree for ranges.

Step-by-step solution

  1. Hashing scatters keys with no order.
  2. A range query must scan all keys.
  3. Use a balanced BST or B-tree for ranges.

Why the other options are wrong

  • A. Deletion by key is O(1) average.
  • B. Lookup is O(1) average.
  • D. Insertion is O(1) average.

Question 5Medium

Using the mid-square method, the key 56 is squared and the middle two digits of the 4-digit result give the hash. What is the hash?

  1. 31
  2. 36
  3. 13
  4. 56

Answer: C. 13

Explanation

56^2 = 3136, and the middle two digits are 13.

Step-by-step solution

  1. 56^2 = 3136.
  2. The middle two digits of 3136 are 1 and 3.
  3. Hash = 13.

Why the other options are wrong

  • A. Reverses the middle digits; 3136 has 1 then 3 in the middle.
  • B. 36 are the last two digits.
  • D. That is the key itself.

Question 6Medium

A hash table with separate chaining has 20 buckets and stores 60 keys. What is the load factor, and the expected length of a chain?

  1. 3, and about 3 keys per chain
  2. 20, and about 3 keys per chain
  3. 0.33, and about 1 key per chain
  4. 60, and about 60 keys per chain

Answer: A. 3, and about 3 keys per chain

Explanation

Load factor alpha = n / m
= 60 / 20 = 3.
With uniform hashing, an average chain has alpha keys, so an unsuccessful search costs about O(1 + alpha).

Step-by-step solution

  1. Load factor = keys / buckets = 60 / 20 = 3.
  2. With uniform hashing, each chain holds about 3 keys on average.
  3. An unsuccessful search costs about O(1 + 3).

Why the other options are wrong

  • B. 20 is the number of buckets, not the load factor.
  • C. This divides buckets by keys, the wrong way round.
  • D. 60 is the total number of keys.

Tip: Load factor = n / m.

Question 7Medium

A hash function randomly distributes records one by one in a space that can hold x number of records. The probability that the mth record is the first record to result in collision is

  1. (x-1) (x-2)...(x-(m-2))(m-1)/xm-1
  2. (x-1) (x-2)...(x-(m-1))(m-1)/xm-1
  3. (x-1) (x-2)...(x-(m-2))(m-1)/xm
  4. (x-1) (x-2)...(x-(m-1))(m-1)/xm

Answer: A.

Explanation

Probability for the first record not colliding is x/x.
Probability for the second record not colliding is x-1/x.
(This is because one place is already occupied, So, favorable number of cases is x-1).
Probability for the third record not colliding is x-2/x.
Probability for the (m-1)th record not colliding is x-(m-2)/x.
Now the next (mth) record is resulting in a collision. Out of the x places, it should has to one of the (m-1) places already, filled. So probability is (m-1)/x. The required probability is

(x/x) (x-1/x) (x-2/x) ... (x-(m-2)/x) (m-1/x)

Step-by-step solution

  1. The first m-1 records must not collide: chances x/x, (x-1)/x, ..., (x-(m-2))/x.
  2. Record m must hit one of the m-1 filled places: chance (m-1)/x.
  3. Multiply and cancel one x: (x-1)(x-2)...(x-(m-2))(m-1) over x^(m-1). Marked answer: option A.

Why the other options are wrong

  • B. The product should stop at (x-(m-2)); (x-(m-1)) is one term too many.
  • C. The denominator should be x^(m-1), not x^m.
  • D. Two errors: the extra term (x-(m-1)) in the numerator and the denominator x^m.

Tip: First collision at record m: multiply the chances of no collision, then multiply by (m-1)/x.

Question 8Medium

The average search time of hashing, with linear probing will be less if the load factor

  1. is far less than one
  2. equals one
  3. is far greater than one
  4. none of the above

Answer: A. is far less than one

Explanation

Load factor is the ration of number of records that are currently present and the total number of records that can be present. If the load factor is less, free space will be more. This means probability of collision is less. So, the search time will be less.

Step-by-step solution

  1. Load factor = (number of records) / (total slots).
  2. Low load factor means more free space.
  3. More free space reduces collisions.
  4. Fewer collisions mean shorter search time.
  5. So, average search time is less when load factor is far less than one.

Why the other options are wrong

  • B. Load factor equals one means no free space, increasing collision chances.
  • C. High load factor causes more collisions and longer search times.
  • D. The correct answer is option A, so this is incorrect.

Tip: Remember: lower load factor = better performance in hashing with linear probing.

Question 9Hard

Cuckoo hashing guarantees which of the following?

  1. No need to ever rehash
  2. O(1) worst-case insertions
  3. Worst-case O(1) lookups (at most two locations checked)
  4. Ordered iteration over keys

Answer: C. Worst-case O(1) lookups (at most two locations checked)

Explanation

Each key lives in one of two possible slots; inserts may displace keys and occasionally trigger a rehash.

Step-by-step solution

  1. Each key has exactly two possible positions.
  2. A lookup checks at most those two.
  3. So lookups are worst-case O(1).

Why the other options are wrong

  • A. Insert cycles force a rehash.
  • B. Insertions can trigger long eviction chains.
  • D. Hashing does not keep order.

Question 10Hard

Keys 19, 27, 36, 10, 64 are inserted in that order into a table of size 11 with double hashing: h1(k) = k mod 11, h2(k) = 7 - (k mod 7), probing (h1(k) + i * h2(k)) mod 11. Where is 64 stored?

  1. 8
  2. 9
  3. 10
  4. 4

Answer: B. 9

Explanation

h1(64) = 9, and slot 9 is free, so 64 is stored there with no probing.

Step-by-step solution

  1. 19->8, 27->5, 36->3, 10->10.
  2. 64: h1 = 9, slot 9 is free.
  3. So 64 goes to 9 without probing.

Why the other options are wrong

  • A. 8 holds 19; 64's home is 9.
  • C. 10 holds 10.
  • D. 4 is where the first probe step (9 + 6) would land, but slot 9 is free so no step is taken.

Practise all 66 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics