Practice Problems on Hashing
Last Updated :
28 Dec, 2024
In this article, we will discuss the types of questions based on hashing. Before understanding this, you should have idea about hashing, hash function, open addressing and chaining techniques (see: Introduction, Separate chaining, Open addressing).
These are some key points in hashing:
- The purpose of hashing is to achieve search, insert and delete an element in complexity O(1).
- Hash function is designed to distribute keys uniformly over the hash table.
- Load factor α in hash table can be defined as number of slots in hash table to number of keys to be inserted.
- For open addressing, load factor α is always less than one.
- The complexity of insertion, deletion and searching using open addressing is 1/(1-α).
- The complexity of insertion, deletion and searching using chaining method is (1+α).
These are the types of questions asked in hashing.
Type 1: Calculation of hash values for given keys -
In this type of questions, hash values are computed by applying given hash function on given keys.
Que - 1. Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod 10, which of the following statements are true? (GATE CS 2004)
i. 9679, 1989, 4199 hash to the same value
ii. 1471, 6171 hash to the same value
iii. All elements hash to the same value
iv. Each element hashes to a different value
(A) i only
(B) ii only
(C) i and ii only
(D) iii or iv
Solutions: Using given hash function h(x) = x mod 10
h(9679) = 9679%10 = 9
h(1989) = 1989%10 = 9
h(4199) = 4199%10 = 9
h(1471) = 1471%10 = 1
h(6171) = 6171%10 = 1
As we can see, 9679, 1989 and 4199 hash to same value 9. Also, 1471 and 6171 hash to same value 1. Therefore, statement (i) and (ii) are correct which match with option (C).
Type 2: Insertion of keys into hash table using linear probing as collision resolution technique -
In linear probing technique, collision is resolved by searching linearly in the hash table until an empty location is found.
Que - 2. The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into an initially empty hash table of length 10 using open addressing with hash function h(k) = k mod 10 and linear probing. What is the resultant hash table?

Solution: Keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted in hash table as:
For key 12, h(12) is 12%10 = 2. Therefore, 12 is placed at 2nd index in the hash table.
For key 18, h(18) is 18%10 = 8. Therefore, 18 is placed at 8th index in the hash table.
For key 13, h(13) is 13%10 = 3. Therefore, 13 is placed at 3rd index in the hash table.
For key 2, h(2) is 2%10 = 2. However, index 2 is already occupied with 12. Therefore, using linear probing, 2 will be placed at index 4 as index 2 and 3 are already occupied.
For key 3, h(3) is 3%10 = 3. However, index 3 is already occupied with 13. Therefore, using linear probing, 3 will be placed at index 5 as index 3 and 4 are already occupied.
Similarly, 23, 5 and 15 will be placed at index 6, 7, 9 respectively.
Therefore, correct option is (C).
Alternative Approach: We can also solve this using elimination approach as:
Option (A) and (B) are incorrect as all keys are not inserted in hash table.
Option (D) is incorrect as some indexes in hash table have more than one key which never happens using linear probing.
Remaining option is (C) which is the answer.
Type 3: Given a hash table with keys, verify/find possible sequence of keys leading to hash table -
For a given hash table, we can verify which sequence of keys can lead to that hash table. However, to find possible sequences leading to a given hash table, we need to consider all possibilities.
Que - 3. A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing. After inserting 6 values into an empty hash table, the table is as shown below.

Which one of the following choices gives a possible order in which the key values could have been inserted in the table?
(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
Solution: We will check whether sequence given in option A can lead to hash table given in question. Option A inserts 46, 42, 34, 52, 23, 33 as:
For key 46, h(46) is 46%10 = 6. Therefore, 46 is placed at 6th index in the hash table.
For key 42, h(42) is 42%10 = 2. Therefore, 42 is placed at 2nd index in the hash table.
For key 34, h(34) is 34%10 = 4. Therefore, 34 is placed at 4th index in the hash table.
For key 52, h(52) is 52%10 = 2. However, index 2 is occupied with 42. Therefore, 52 is placed at 3rd index in the hash table. But in given hash table, 52 is placed at 5th index. Therefore, sequence in option A can’t generate hash table given in question.
In the similar way, we can check for other options as well which leads to answer as (C).
Que - 4. How many different insertion sequences of the key values using the same hash function and linear probing will result in the hash table given in Question 3 above?
(A) 10
(B) 20
(C) 30
(D) 40
Solution: The first key which is not at the index computed by hash function is 52. It means index 2, 3 and 4 were already occupied and therefore, key 52 is placed at index 5.
The keys 42, 23 and 34 are present at index 2, 3, and 4 respectively. As these keys are at their correct position, their order of insertion does not matter. These 3 keys can be inserted in 3! = 6 ways. Therefore, the sequence will be any order of (42, 23, 34) followed by 52.
The next key which is not at the index computed by hash function is 33. It means indexes 3 to 6 were already occupied and key 33 is placed at index 7. Therefore, it is the last key to be inserted into hash table.
The key 46 is present at its correct position computed by hash function. Therefore, it can be inserted at any place in the sequence before 33. The sequence excluding 33 has 4 elements 42, 23, 34, 52 which create 5 positions for 46 (3 in-between and 2 corners).
Total number of ways is: 6*5 =30
Type 4: Chaining based collision resolution technique -
In chaining based collision resolution technique, the keys generating same hash value are placed in same bucket using pointers. The different types of questions based on chaining technique are:
Que - 5. Consider a hash table with 100 slots. Collisions are resolved using chaining. Assuming simple uniform hashing, what is the probability that the first 3 slots are unfilled after the first 3 insertions? (GATE-CS-2014)
(A) (97 × 97 × 97)/100^3
(B) (99 × 98 × 97)/100^3
(C) (97 × 96 × 95)/100^3
(D) (97 × 96 × 95)/(3! × 100^3)
Solution: In uniform hashing, the function evenly distributes keys into slots of hash table. Also, each key has an equal probability of being placed into a slot, being independent of the other elements already placed.
Therefore, the probability of remaining first 3 slots empty for first insertion (choosing 4 to 100 slot) = 97/100. As next insertions are independent on previous insertion, the probability for next insertions will also be 97/100. The required probability will be (97/100)^3.
Que - 6. Which one of the following hash functions on integers will distribute keys most uniformly over 10 buckets numbered 0 to 9 for i ranging from 0 to 2020? (GATE-CS-2015)
(A) h(i) =i^2 mod 10
(B) h(i) =i^3 mod 10
(C) h(i) = (11 ∗ i^2) mod 10
(D) h(i) = (12 ∗ i) mod 10
Solution: In uniform distribution, the function evenly distributes keys into slots of hash table.
For given hash functions, we have calculated hash values for keys 0 to 9 as:

As we can see from the table, i^3 mod10 is distributing evenly from indexes 0 to 9. Other functions have not utilized all indexes.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem