PythonStudio
Interview Problems

HashMap

medium · hashing, chaining

Brief-reported interview attribution (unverified): LeetCode 706 is the design-round version of this question

How to approach an LLD round

Implement a hash map for integer keys. get returns -1 for a missing key. Collisions are resolved by chaining. The bucket count is fixed at 769 (a prime).

class MyHashMap:
    def __init__(self, size=769):
    def put(self, key, value):
        # insert, or update the value if the key already exists
    def get(self, key):
        # -> value, or -1 if the key is absent
    def remove(self, key):
        # delete the key if present; a no-op if absent

Rules. Bucket index is key % size (Python's % keeps negative keys in range). Two keys that land in the same bucket must coexist, and removing one must not affect the other.

Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.

Public demo

# Public demo: run after implementing the class API above.
h = MyHashMap()
h.put(1, 10)
print(h.get(1))
print(h.get(2))

Starter code

class MyHashMap:

    def __init__(self, size=769):
        """Initialize the state described in the problem."""
        pass

    def put(self, key, value):
        """insert, or update the value if the key already exists"""
        pass

    def get(self, key):
        """-> value, or -1 if the key is absent"""
        pass

    def remove(self, key):
        """delete the key if present; a no-op if absent"""
        pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler