Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Design a hashset

Moderate
0/80
Average time to solve is 25m
7 upvotes
Asked in companies
Goldman SachsExpedia GroupMorgan Stanley

Problem statement

Design a HashSet without using any built-in hash table libraries.

Implement the following public functions :

1) Constructor: It initializes the data members as required.

2) add(value): It inserts an element into the HashSet. The function takes one argument which is the value that needs to be added and returns nothing

3) contains(value): It checks whether the element exists in the HashSet or not. The function takes one argument which is the value that needs to be searched for in the HashSet. The function returns true if the element exists, otherwise returns false.

4) remove(value): It removes an element from the HashSet. The function takes one argument which is the value that needs to be removed from the HashSet and returns the element which is being removed. If the element does not exist in the HashSet or if HashSet is empty, return -1.
Operations Performed on the HashSet:
Query-1 (Denoted by an integer 1)- Inserts an element in the HashSet

Query-2 (Denoted by an integer 2)- Returns a boolean value denoting whether the element is present in the HashSet or not.

Query-3 (Denoted by an integer 3)- Removes the element from the HashSet.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= Q <= 10^3
1 <= query type <= 3
0 <= VALUE <= 10^6

Where ‘Q’ is the total number of queries, ‘value’ is the element that will be added, removed, or whose existence in the HashSet is to be checked.

Time limit: 1 second
Sample input 1
6
1 500
1 200
1 400
2 200
2 600
3 200
Sample Output 1:
True
False
200
Explanation of sample input 1:
There are 6 queries.

Query 1: 1 represents the add function. 500 is added to the HashSet.
Query 2: 1 represents the add function. 200 is added to the HashSet.
Query 3: 1 represents the add function. 400 is added to the HashSet.
Query 4: 2 represents the contains function. Since 200 is present in the HashSet, it returns true.
Query 5: 2 represents the contains function. Since 600 is not present in the HashSet, it returns false.
Query 6: 3 represents the remove function. Since 200 is present in the HashSet, it is removed and 200 is returned.
Sample input 2
5
1 20
1 50
2 30
3 20
3 50
Sample Output 2:
False
20
50
Full screen
Console