Advertisement

Design HashMap - LeetCode 706 Solution

Design HashMap - Complete Solution Guide

Design HashMap is LeetCode problem 706, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3.

Problem Statement

Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: MyHashMap() initializes the object with an empty map. void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value . int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key . void remove(key) removes the key and its corresponding value if the map contai

Detailed Explanation

The problem asks us to implement a HashMap data structure from scratch, without using any built-in hash table libraries. We need to implement the `put`, `get`, and `remove` operations. `put(key, value)` inserts a key-value pair into the map, updating the value if the key already exists. `get(key)` returns the value associated with the key, or -1 if the key is not found. `remove(key)` deletes the key-value pair if the key exists in the map. The keys and values are integers within the range [0, 10^6], and the number of calls to `put`, `get`, and `remove` will be at most 10^4.

Solution Approach

The solution uses a simple hash function (key % size) to map keys to indices in a list called `table`. Each element of the `table` is a list representing a bucket. When a key-value pair is inserted, we calculate the index using the hash function. If the key already exists in the bucket, we update its value. Otherwise, we append the new key-value pair to the bucket. For `get` and `remove`, we iterate through the bucket at the calculated index and search for the key. If found, we return the value or remove the key-value pair accordingly. If not found, `get` returns -1 and `remove` does nothing.

Step-by-Step Algorithm

  1. Step 1: Initialize the `MyHashMap` object with a fixed size for the hash table (`size`). Create a list (or array) called `table` of size `size`, where each element is initially an empty list.
  2. Step 2: In the `put(key, value)` function, calculate the index using the hash function: `index = key % size`.
  3. Step 3: Iterate through the list (bucket) at `table[index]`. If the key already exists, update the value associated with that key and return.
  4. Step 4: If the key doesn't exist in the bucket, append the `(key, value)` pair to the end of the list `table[index]`.
  5. Step 5: In the `get(key)` function, calculate the index using the hash function: `index = key % size`.
  6. Step 6: Iterate through the list at `table[index]`. If the key is found, return the corresponding value.
  7. Step 7: If the key is not found after iterating through the entire list, return -1.
  8. Step 8: In the `remove(key)` function, calculate the index using the hash function: `index = key % size`.
  9. Step 9: Iterate through the list at `table[index]`. If the key is found, remove the key-value pair from the list.
  10. Step 10: If the key is not found, do nothing.

Key Insights

  • Insight 1: We can't directly use keys as indices in an array due to the constraint 0 <= key <= 10^6, which would require a very large array and waste memory. Therefore, we need a hashing function to map keys to a smaller index range.
  • Insight 2: Collision handling is crucial. When different keys map to the same index, we need a strategy to store and retrieve them. Using a separate chaining approach (linked list or list) is a common and efficient solution.
  • Insight 3: Choosing a good hash function and table size is important for performance. A simple modulo operation works in this case, but other hash functions might be better for different data distributions. The size of the table should be chosen to minimize collisions.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Linked List, Design, Hash Function.

Companies

Asked at: Applied Intuition, Deutsche Bank, Nvidia, Palo Alto Networks, ServiceNow, Snowflake, Two Sigma.