Implement an inventory manager with restock, sell and total-unit queries

Quick Overview

Implement an inventory manager class for a single store that restocks products, sells units only when enough stock exists, and reports per-product and total stock. It tests hash map design, validating input before changing state, and keeping a running total consistent with the counts.

Implement an inventory manager with restock, sell and total-unit queries

Company: Bloomberg

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design an `InventoryManager` class that tracks stock counts for products in a store. The system supports restocking items, selling items, and querying current stock levels. A common bug in such systems is a `sell` function that mistakenly increments stock instead of decrementing it. Your implementation must correctly decrement stock on every successful sale. If a `sell` request asks for more units than are currently in stock (or the product does not exist), the sale must fail and leave inventory unchanged. Implement the following methods: - `restock(productId: int, quantity: int) -> None`: add `quantity` units of `productId` to the inventory. - `sell(productId: int, quantity: int) -> bool`: decrement stock by `quantity`. Return `True` on success, or `False` if the product is unknown or has insufficient stock (inventory unchanged on failure). - `getStock(productId: int) -> int`: return the current stock count for `productId`, or `0` if it does not exist. - `getTotalUnits() -> int`: return the total number of units across all products currently in inventory. Example 1, starting from an empty inventory: ```text Input: ["InventoryManager", "restock", "restock", "getStock", "sell", "getStock", "getTotalUnits", "sell", "getStock"] [[], [1, 10], [2, 5], [1], [1, 3], [1], [], [1, 100], [1]] Output: [null, null, null, 10, true, 7, 12, false, 7] inv = InventoryManager() # empty inventory inv.restock(1, 10) # stock: {1: 10} inv.restock(2, 5) # stock: {1: 10, 2: 5} inv.getStock(1) # -> 10 inv.sell(1, 3) # -> True (stock: {1: 7, 2: 5}) inv.getStock(1) # -> 7 (decremented by 3) inv.getTotalUnits() # -> 12 (7 + 5) inv.sell(1, 100) # -> False (insufficient stock, nothing changes) inv.getStock(1) # -> 7 (still 7 after the failed sale) ``` Example 2, starting from an empty inventory: ```text Input: ["InventoryManager", "sell", "getStock", "restock", "sell", "sell", "getStock", "getTotalUnits"] [[], [42, 1], [42], [42, 4], [42, 4], [42, 1], [42], []] Output: [null, false, 0, null, true, false, 0, 0] inv = InventoryManager() # empty inventory inv.sell(42, 1) # -> False (product 42 unknown) inv.getStock(42) # -> 0 inv.restock(42, 4) # stock: {42: 4} inv.sell(42, 4) # -> True (stock: {42: 0}) inv.sell(42, 1) # -> False (stock is 0) inv.getStock(42) # -> 0 inv.getTotalUnits() # -> 0 ``` ```hint Order of operations Decide whether a sale is allowed before changing anything, and check what `getTotalUnits` would cost if it summed every product on each call. ``` ### Constraints & Assumptions - `1 <= quantity <= 10^4` - `0 <= productId <= 10^5` - At most `10^4` operations will be called across `restock`, `sell`, `getStock`, and `getTotalUnits`. - A failed `sell` (insufficient stock or unknown product) must not modify inventory. ### Clarifying Questions to Ask - Will several threads call these methods at the same time, or is single-threaded use enough? - Should a product that sells down to 0 stay tracked, or be dropped from the inventory? - Can the stated quantity range be trusted, or should calls outside it be rejected? ### What a Strong Answer Covers - A map from product id to stock count, and the cost of each operation. - A `sell` path that checks existence and stock before writing, so a failed sale changes nothing. - A running total that stays in step with every successful `restock` and `sell`. - Edge cases such as unknown products, selling exactly the remaining stock, and stock at 0. ### Follow-up Questions - How would you make the class safe when several threads restock and sell at the same time? - How would you hold units for items sitting in a shopping cart before checkout? - How would you add a low-stock alert that fires when a product drops below a threshold? - What tests would catch a `sell` that increments stock or a total that drifts from the per-product counts?

Overview: Implement an inventory manager class for a single store that restocks products, sells units only when enough stock exists, and reports per-product and total stock. It tests hash map design, validating input before changing state, and keeping a running total consistent with the counts.

Bloomberg logo
Bloomberg
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an InventoryManager class that tracks stock counts for products in a store. The system supports restocking items, selling items, and querying current stock levels.

A common bug in such systems is a sell function that mistakenly increments stock instead of decrementing it. Your implementation must correctly decrement stock on every successful sale.

If a sell request asks for more units than are currently in stock (or the product does not exist), the sale must fail and leave inventory unchanged.

Implement the following methods:

  • restock(productId: int, quantity: int) -> None : add quantity units of productId to the inventory.
  • sell(productId: int, quantity: int) -> bool : decrement stock by quantity . Return True on success, or False if the product is unknown or has insufficient stock (inventory unchanged on failure).
  • getStock(productId: int) -> int : return the current stock count for productId , or 0 if it does not exist.
  • getTotalUnits() -> int : return the total number of units across all products currently in inventory.

Example 1, starting from an empty inventory:

Input:
["InventoryManager", "restock", "restock", "getStock", "sell", "getStock", "getTotalUnits", "sell",   "getStock"]
[[],                 [1, 10],   [2, 5],    [1],        [1, 3], [1],        [],              [1, 100], [1]]

Output: [null, null, null, 10, true, 7, 12, false, 7]

inv = InventoryManager()     # empty inventory
inv.restock(1, 10)           # stock: {1: 10}
inv.restock(2, 5)            # stock: {1: 10, 2: 5}
inv.getStock(1)              # -> 10
inv.sell(1, 3)               # -> True   (stock: {1: 7, 2: 5})
inv.getStock(1)              # -> 7      (decremented by 3)
inv.getTotalUnits()          # -> 12     (7 + 5)
inv.sell(1, 100)             # -> False  (insufficient stock, nothing changes)
inv.getStock(1)              # -> 7      (still 7 after the failed sale)

Example 2, starting from an empty inventory:

Input:
["InventoryManager", "sell",  "getStock", "restock", "sell",  "sell",  "getStock", "getTotalUnits"]
[[],                 [42, 1], [42],       [42, 4],   [42, 4], [42, 1], [42],       []]

Output: [null, false, 0, null, true, false, 0, 0]

inv = InventoryManager()     # empty inventory
inv.sell(42, 1)              # -> False  (product 42 unknown)
inv.getStock(42)             # -> 0
inv.restock(42, 4)           # stock: {42: 4}
inv.sell(42, 4)              # -> True   (stock: {42: 0})
inv.sell(42, 1)              # -> False  (stock is 0)
inv.getStock(42)             # -> 0
inv.getTotalUnits()          # -> 0

Constraints & Assumptions

  • 1 <= quantity <= 10^4
  • 0 <= productId <= 10^5
  • At most 10^4 operations will be called across restock , sell , getStock , and getTotalUnits .
  • A failed sell (insufficient stock or unknown product) must not modify inventory.

Clarifying Questions to Ask Guidance

  • Will several threads call these methods at the same time, or is single-threaded use enough?
  • Should a product that sells down to 0 stay tracked, or be dropped from the inventory?
  • Can the stated quantity range be trusted, or should calls outside it be rejected?

What a Strong Answer Covers Guidance

  • A map from product id to stock count, and the cost of each operation.
  • A sell path that checks existence and stock before writing, so a failed sale changes nothing.
  • A running total that stays in step with every successful restock and sell .
  • Edge cases such as unknown products, selling exactly the remaining stock, and stock at 0.

Follow-up Questions Guidance

  • How would you make the class safe when several threads restock and sell at the same time?
  • How would you hold units for items sitting in a shopping cart before checkout?
  • How would you add a low-stock alert that fires when a product drops below a threshold?
  • What tests would catch a sell that increments stock or a total that drifts from the per-product counts?