HashMap Implementation Problem Statement

Your task is to design a data structure that efficiently stores a mapping of keys to values and performs operations in constant time.

Explanation:

1. INSERT(key, value): Add or update a mapping from a string key to an integer value. The function does not return anything.

2. DELETE(key): Remove the mapping for a key if it exists. The function does not return anything.

3. SEARCH(key): Check if the key exists in the structure. Return true if it is found, otherwise return false.

4. GET(key): Get the integer value associated with a key. If the key is not found, return -1.

5. GET_SIZE(): Return the number of key-value pairs in the data structure.

6. IS_EMPTY(): Return true if the data structure is empty, otherwise false.

Input:

The first line contains an integer 'N', denoting the number of operations to be performed. Each of the following N lines contains:
- For 'INSERT', three inputs: operation type, key (string), value (integer).
- For 'DELETE', 'SEARCH', and 'GET', two inputs: operation type, key (string).
- For 'GET_SIZE' and 'IS_EMPTY', one input: operation type.

Output:

Corresponding outputs for the operations:
- For 'INSERT': no output required.
- For 'DELETE': no output required.
- For 'SEARCH': return true if the key is present, otherwise false.
- For 'GET': return the value if the key exists, otherwise -1.
- For 'GET_SIZE': return the size of the data structure.
- For 'IS_EMPTY': return true if the data structure is empty, otherwise false.

Example:

// No specific example is provided as output depends on dynamic operations.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ T ≤ 3 (where 'T' is the type of operation)
  • 1 ≤ V ≤ 105 (where 'V' is the value to be inserted)
  • Time Limit: 3 seconds

Note:

  • The key is always a string.
  • The value associated with a key can never be -1.
Be the first one to answer
Add answer anonymously...
Ernst & Young Technology Consultant 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