Design a HashSet

Create a HashSet data structure without using any built-in hash table libraries.

Functions Description:

1) Constructor: Initializes the data members as required.

2) add(value): Inserts an element into the HashSet. Takes one argument, the value to be added, and returns nothing.

3) contains(value): Checks if the element exists in the HashSet. Takes one argument, the value to be searched for. Returns true if the element exists, otherwise returns false.

4) remove(value): Removes an element from the HashSet. Takes one argument, the value to be removed. Returns the removed element, or -1 if the element does not exist or the HashSet is empty.

Query Operations:

Query-1 (1): Inserts an element into the HashSet.
Query-2 (2): Returns a boolean indicating whether the element is present.
Query-3 (3): Removes the element from the HashSet.

Input:

The first line contains an integer ‘Q’, the number of queries.
Each of the next ‘Q’ lines contains two integers, representing the type of operation and the value.

Output:

For Query-1: Do not return anything.
For Query-2: Return true or false based on the presence of the element.
For Query-3: Return the element being removed, or -1 if not present.

Constraints:

  • 1 <= Q <= 103
  • 1 <= query type <= 3
  • 0 <= VALUE <= 106
  • Time limit: 1 second
Be the first one to answer
Add answer anonymously...
Goldman Sachs Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter