Custom HashMap with O(1) Bulk Add to All Keys and All Values

Quick Overview

Design a hash map that supports insert and get plus two bulk updates, adding an integer to every key or to every value, with all four operations in constant time. It tests whether you can replace per-entry updates with constant-time bookkeeping and keep reads correct for entries inserted before and after each update.

Custom HashMap with O(1) Bulk Add to All Keys and All Values

Company: SIG (Susquehanna)

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a custom hash map that, in addition to the standard `insert` and `get` operations, supports two bulk update operations: adding an integer to every key and adding an integer to every value. The challenge is that all four operations (`insert`, `get`, `addToKey` and `addToValue`) must run in $O(1)$ time. This means the bulk operations cannot iterate over the stored entries. If `get` is called with a key that does not exist in the map, return `-1`. Implement the following methods: - `insert(key: int, value: int)`: insert or update the key-value pair in the map. - `get(key: int) -> int`: return the value associated with the key, or `-1` if the key does not exist. - `addToKey(x: int)`: add the integer `x` to every key currently stored in the map. - `addToValue(y: int)`: add the integer `y` to every value currently stored in the map. ```hint Constant work per bulk update A bulk update can only change a constant amount of state. Ask what single number could record the fact that every key has moved by the same amount. ``` ```hint Entries inserted later An entry inserted after several bulk updates must not pick up those earlier updates. Decide what form to store an entry in so that one rule recovers the current key and value for old and new entries alike. ``` ### Examples Example 1: ```text Input: ["CustomHashMap", "insert", "insert", "get", "addToKey", "get", "get", "addToValue", "get", "get"] [[], [1, 2], [3, 4], [3], [5], [8], [3], [10], [6], [8]] Output: [null, null, null, 4, null, 4, -1, null, 12, 14] Explanation: map = CustomHashMap() # empty map map.insert(1, 2) # logical: {1: 2} map.insert(3, 4) # logical: {1: 2, 3: 4} map.get(3) # -> 4 (direct lookup) map.addToKey(5) # logical keys shift: {6: 2, 8: 4} map.get(8) # -> 4 (was key 3, now key 8) map.get(3) # -> -1 (key 3 no longer exists) map.addToValue(10) # logical values shift: {6: 12, 8: 14} map.get(6) # -> 12 map.get(8) # -> 14 ``` Example 2: ```text Input: ["CustomHashMap", "get", "insert", "addToKey", "addToValue", "insert", "get", "get", "addToKey", "get"] [[], [42], [0, 0], [100], [-50], [5, 5], [100], [105], [-100], [5]] Output: [null, -1, null, null, null, null, -50, -1, null, -1] Explanation: map = CustomHashMap() # empty map map.get(42) # -> -1 (map is empty) map.insert(0, 0) # logical: {0: 0} map.addToKey(100) # logical: {100: 0} map.addToValue(-50) # logical: {100: -50} map.insert(5, 5) # logical: {100: -50, 5: 5} map.get(100) # -> -50 map.get(105) # -> -1 (no such key) map.addToKey(-100) # logical: {0: -50, -95: 5} map.get(5) # -> -1 (key 5 shifted to -95) ``` ### Constraints & Assumptions - `1 <= number of operations <= 10^5` - `-10^9 <= key, value <= 10^9` - `-10^9 <= x, y <= 10^9` - All four operations must run in $O(1)$ time. ### Clarifying Questions to Ask - Is average-case $O(1)$ from a hash table acceptable, or is a worst-case bound expected? - After many bulk updates, keys and values can drift outside the 32-bit range. What integer width should `get` return? - Should `insert` on a key that currently exists overwrite its value, including a key that reached its current value through `addToKey`? ### What a Strong Answer Covers - The constant amount of state that replaces iterating over entries in `addToKey` and `addToValue`. - How `insert` stores an entry so that bulk updates made before the insert do not apply to it, while later ones do. - How `get` maps a requested key to what is stored and back to the current value, including the missing-key case. - An argument that lookups and overwrites stay correct after any sequence of shifts. - Time and space complexity, and the integer range the accumulated updates can reach. ### Follow-up Questions - How would you add a `multiplyAllValues(m)` operation in $O(1)$ next to `addToValue`, and what goes wrong when `m` is 0? - How would you support `delete(key)` and `size()`? - How would you add an operation that returns the smallest current key? Does `addToKey` change which key is smallest? - `get` returns `-1` for a missing key. How would you change the API if `-1` is a legitimate value?

Overview: Design a hash map that supports insert and get plus two bulk updates, adding an integer to every key or to every value, with all four operations in constant time. It tests whether you can replace per-entry updates with constant-time bookkeeping and keep reads correct for entries inserted before and after each update.

SIG (Susquehanna) logo
SIG (Susquehanna)
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
1
0

Design a custom hash map that, in addition to the standard insert and get operations, supports two bulk update operations: adding an integer to every key and adding an integer to every value.

The challenge is that all four operations (insert, get, addToKey and addToValue) must run in O(1)O(1) time. This means the bulk operations cannot iterate over the stored entries.

If get is called with a key that does not exist in the map, return -1.

Implement the following methods:

  • insert(key: int, value: int) : insert or update the key-value pair in the map.
  • get(key: int) -> int : return the value associated with the key, or -1 if the key does not exist.
  • addToKey(x: int) : add the integer x to every key currently stored in the map.
  • addToValue(y: int) : add the integer y to every value currently stored in the map.

Examples

Example 1:

Input:
["CustomHashMap", "insert", "insert", "get", "addToKey", "get", "get", "addToValue", "get", "get"]
[[],              [1, 2],   [3, 4],   [3],   [5],        [8],   [3],   [10],         [6],   [8]]

Output: [null, null, null, 4, null, 4, -1, null, 12, 14]

Explanation:
map = CustomHashMap()            # empty map
map.insert(1, 2)                 # logical: {1: 2}
map.insert(3, 4)                 # logical: {1: 2, 3: 4}
map.get(3)                       # -> 4    (direct lookup)
map.addToKey(5)                  # logical keys shift: {6: 2, 8: 4}
map.get(8)                       # -> 4    (was key 3, now key 8)
map.get(3)                       # -> -1   (key 3 no longer exists)
map.addToValue(10)               # logical values shift: {6: 12, 8: 14}
map.get(6)                       # -> 12
map.get(8)                       # -> 14

Example 2:

Input:
["CustomHashMap", "get", "insert", "addToKey", "addToValue", "insert", "get", "get", "addToKey", "get"]
[[],              [42],  [0, 0],   [100],      [-50],        [5, 5],   [100], [105], [-100],     [5]]

Output: [null, -1, null, null, null, null, -50, -1, null, -1]

Explanation:
map = CustomHashMap()            # empty map
map.get(42)                      # -> -1   (map is empty)
map.insert(0, 0)                 # logical: {0: 0}
map.addToKey(100)                # logical: {100: 0}
map.addToValue(-50)              # logical: {100: -50}
map.insert(5, 5)                 # logical: {100: -50, 5: 5}
map.get(100)                     # -> -50
map.get(105)                     # -> -1   (no such key)
map.addToKey(-100)               # logical: {0: -50, -95: 5}
map.get(5)                       # -> -1   (key 5 shifted to -95)

Constraints & Assumptions

  • 1 <= number of operations <= 10^5
  • -10^9 <= key, value <= 10^9
  • -10^9 <= x, y <= 10^9
  • All four operations must run in O(1)O(1) time.

Clarifying Questions to Ask Guidance

  • Is average-case O(1)O(1) from a hash table acceptable, or is a worst-case bound expected?
  • After many bulk updates, keys and values can drift outside the 32-bit range. What integer width should get return?
  • Should insert on a key that currently exists overwrite its value, including a key that reached its current value through addToKey ?

What a Strong Answer Covers Guidance

  • The constant amount of state that replaces iterating over entries in addToKey and addToValue .
  • How insert stores an entry so that bulk updates made before the insert do not apply to it, while later ones do.
  • How get maps a requested key to what is stored and back to the current value, including the missing-key case.
  • An argument that lookups and overwrites stay correct after any sequence of shifts.
  • Time and space complexity, and the integer range the accumulated updates can reach.

Follow-up Questions Guidance

  • How would you add a multiplyAllValues(m) operation in O(1)O(1) next to addToValue , and what goes wrong when m is 0?
  • How would you support delete(key) and size() ?
  • How would you add an operation that returns the smallest current key? Does addToKey change which key is smallest?
  • get returns -1 for a missing key. How would you change the API if -1 is a legitimate value?