Skip to main content
Back to problems
Leetcode
Medium
Design
Hash Maps
Google
Amazon
Design Hashmap

Design a hash map that supports insertion, lookup, and deletion of integer key-value pairs.

Acceptance 0%
Problem Statement

Problem

Implement a hash map from scratch with the following operations:

  • put(key, value): Insert the pair (key, value) into the map. If key already exists, update its value.
  • get(key): Return the value associated with key, or -1 if the key does not exist.
  • remove(key): Delete the key and its value from the map if it exists.

Your implementation should behave like a simple associative array for integer keys and values.

Goal

Design the data structure so that the operations are efficient on average and handle key collisions correctly.

Input Format

The interface is called directly by the judge.

  • put(key, value)
  • get(key)
  • remove(key)

All keys and values are integers.

Output Format

  • get(key) returns the stored value, or -1 if the key is absent.
  • put and remove do not return a value.

Constraints

  • Keys and values are integers.
  • The number of operations can be large, so average-case efficiency matters.
  • The structure must support updates and deletions.

Note: Exact official constraints are not provided here; use a standard hash map design approach.

Examples
Sample cases returned by the problem API.

Example 1

Input

put(1, 1)
put(2, 2)
get(1)
get(3)
put(2, 1)
get(2)
remove(2)
get(2)

Output

1
-1
1
-1

Explanation

  • get(1) returns 1.
  • get(3) returns -1 because the key is missing.
  • put(2, 1) updates the existing value for key 2.
  • get(2) returns 1.
  • After remove(2), get(2) returns -1.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.