Question 1Easy
Using a frequency map, what is the first non-repeating character of "swisstown"?
- i
- w
- s
- 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
- Counts: s 3, w 2, i 1, t 1, o 1, n 1.
- Scan in order: s, w, i -> i is the first with count 1.
- 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 4Easy
Which operation is NOT efficient in a standard hash table?
- Deletion by key
- Lookup by key
- Finding all keys in a sorted range
- 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
- Hashing scatters keys with no order.
- A range query must scan all keys.
- 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 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?
- 3, and about 3 keys per chain
- 20, and about 3 keys per chain
- 0.33, and about 1 key per chain
- 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
- Load factor = keys / buckets
= 60 / 20 = 3.
- With uniform hashing, each chain holds about 3 keys on average.
- 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
(x-1) (x-2)...(x-(m-2))(m-1)/xm-1
(x-1) (x-2)...(x-(m-1))(m-1)/xm-1
(x-1) (x-2)...(x-(m-2))(m-1)/xm
(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
- The first m-1 records must not collide: chances x/x, (x-1)/x, ..., (x-(m-2))/x.
- Record m must hit one of the m-1 filled places: chance (m-1)/x.
- 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
- is far less than one
- equals one
- is far greater than one
- 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
- Load factor = (number of records) / (total slots).
- Low load factor means more free space.
- More free space reduces collisions.
- Fewer collisions mean shorter search time.
- 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?
- No need to ever rehash
- O(1) worst-case insertions
- Worst-case O(1) lookups (at most two locations checked)
- 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
- Each key has exactly two possible positions.
- A lookup checks at most those two.
- 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?
- 8
- 9
- 10
- 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
- 19->8, 27->5, 36->3, 10->10.
- 64:
h1 = 9, slot 9 is free. - 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.