LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 5 of 13

Hashing: questions with answers

Hash functions, collisions, chaining and open addressing, load factor, hash maps and sets in Python, Java and C++, and hashing patterns such as prefix sums and rolling hashes.

Questions
45
Timed sets
3
Answers run or computed
29
Your accuracy
–

What this topic covers

18 subtopics
  • Hash maps6
  • Counting5
  • Open addressing5
  • Patterns4
  • Collisions3
  • Hash functions3
  • Sets3
  • Chaining2
  • Load factor2
  • Prefix sums2
  • Rolling hash2
  • Sliding window2
Show all 18 subtopics
  • Basics1
  • Distributed hashing1
  • equals and hashCode1
  • Hashable keys1
  • Probabilistic structures1
  • Security1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

A hash table turns a key into an array index with a hash function, giving O(1) average lookups. Two keys can land in the same slot – a collision – resolved by chaining (a list per slot) or open addressing (probe for the next free slot). GATE asks you to trace probing by hand; interviews use hash maps to turn O(n²) searches into one pass. Probing traces on this page were computed by a checker program.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Counting Beginner Practice Code executed

What does this print?

Python main.py
from collections import Counter

c = Counter("mississippi")
print(c.most_common(2))
[('i', 4), ('p', 2)]
[('s', 4), ('i', 4)]
[('m', 1), ('i', 4)]
[('i', 4), ('s', 4)]
Show answer and explanation
Correct answer D

Explanation

Counter is a dict of counts. i and s both occur 4 times; ties keep the order in which keys were first seen, and i appears before s.

Why the other options are wrong

  • As (4) beats p (2).
  • Bi is first seen before s.
  • Cmost_common sorts by count.
Question 2 Open addressing Intermediate Practice Answer computed

A table of size 7 uses h(k) = k mod 7 with linear probing. Keys are inserted in this order: 50, 700, 76, 85, 92, 73, 101. What is the final table (slots 0–6)?

Array
table
  1. 0·
  2. 1·
  3. 2·
  4. 3·
  5. 4·
  6. 5·
  7. 6·
  1. 700
  2. 50
  3. 92
  4. 85
  5. 73
  6. 101
  7. 76
  1. 700
  2. 50
  3. 85
  4. 73
  5. 92
  6. 101
  7. 76
  1. 700
  2. 50
  3. 85
  4. 92
  5. 73
  6. 101
  7. 76
  1. 50
  2. 700
  3. 85
  4. 92
  5. 73
  6. 101
  7. 76
Show answer and explanation
Correct answer C

Explanation

50 → 1, 700 → 0, 76 → 6. 85 hashes to 1 (taken) → 2. 92 hashes to 1 → 2 taken → 3. 73 hashes to 3 (taken) → 4. 101 hashes to 3 → 4 taken → 5. Note how keys pile into one run – primary clustering.

Why the other options are wrong

  • A85 is inserted before 92 and takes slot 2.
  • B92 is inserted before 73 and takes slot 3.
  • D700 mod 7 is 0.
43 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 13 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels
  13. 13 GATE-Style DSA & Algorithms Questions (MSQ & NAT)45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links