Design a Constant Time Data Structure

Create a data structure that maintains mappings between keys and values, supporting the following operations in constant time:

1. INSERT(key, value): Add or update the integer value associated with a given string key. 2. DELETE(key): Remove the key and its associated value. 3. SEARCH(key): Check if the key exists in the data structure. Return true if it exists, otherwise false. 4. GET(key): Retrieve the value associated with the key. Return -1 if the key does not exist. 5. GET_SIZE(): Return the number of key-value pairs in the data structure. 6. IS_EMPTY(): Return true if the data structure contains no key-value pairs, otherwise return false.

Input:

The first line contains an integer 'N', representing the number of operations to be executed. The subsequent 'N' lines each correspond to one operation: For INSERT, a line contains: "1 key value" For DELETE, SEARCH, and GET, a line contains: "2 key", "3 key", or "4 key" For GET_SIZE and IS_EMPTY, a line contains only "5" or "6".

Output:

Return or display results based on operation type: Type 1: Do not return anything. Type 2: Do not return anything. Type 3: Return true or false. Type 4: Return the value, or -1 if not found. Type 5: Return the size of the data structure. Type 6: Return true if empty, otherwise false.

Example:

Input:
N = 6
1 apple 10
3 apple
4 apple
2 apple
4 apple
6
Output:
true
10
-1
true

Constraints:

  • 1 <= N <= 10^5
  • 1 <= T <= 3
  • 1 <= V <= 10^5
  • Time Limit: 3 sec

Note:

  • Key is always a string value.
  • Value is always a positive integer (not -1).
AnswerBot
3h

Design a constant time data structure for key-value mappings with operations like INSERT, DELETE, SEARCH, GET, GET_SIZE, and IS_EMPTY.

  • Use a hash table to achieve constant time complexity for operation...read more

Help your peers!
Add answer anonymously...
Goldman Sachs Software Engineer 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