Design a thread-safe grocery inventory system with stock transfers and auto-replenishment

Quick Overview

Design and implement a grocery inventory manager that tracks stock across several fulfillment centers, moves stock between them atomically and tops it up through a pluggable replenishment rule. It tests low-level design, data modeling, input validation and safe locking under concurrent calls.

Design a thread-safe grocery inventory system with stock transfers and auto-replenishment

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a grocery inventory management system that spans multiple fulfillment centers. Each center keeps its own stock of products, and the system must support adding centers, adding products with initial quantities, updating stock, and querying availability both per center and globally. The system must support transferring stock between fulfillment centers atomically, so that a product can never be transferred beyond the quantity available at the source center. It must also support a pluggable replenishment strategy. When a rule is configured for a product and the stock of that product at any center falls below the rule's threshold, the stock is automatically replenished to `threshold + replenish_amount`. All operations must be safe under concurrent calls from multiple threads. Implement a class `InventoryManager` with these methods: - `add_center(center_name: str) -> bool`: add a new fulfillment center. Returns `True` if added, `False` if it already exists. - `add_product(center_name: str, product_name: str, quantity: int) -> bool`: add a product to a center with an initial quantity. Returns `False` if the center does not exist or the product already exists at that center. - `update_stock(center_name: str, product_name: str, quantity: int) -> bool`: set the stock of a product at a center. Returns `False` if the center or product does not exist. Triggers the replenishment check. - `get_stock(center_name: str, product_name: str) -> int`: get the stock of a product at one center. Returns `-1` if the center or product does not exist. - `get_availability(product_name: str) -> int`: get the total stock of a product across all fulfillment centers. Returns `0` if the product is not found. - `transfer_stock(from_center: str, to_center: str, product_name: str, quantity: int) -> bool`: move a quantity of a product between centers. Returns `True` on success, `False` if stock is insufficient or a center or product is invalid. - `set_replenishment_strategy(product_name: str, threshold: int, replenish_amount: int) -> None`: set the replenishment rule for a product. If its stock falls below `threshold` at any center, it is replenished to `threshold + replenish_amount`. - `remove_product(center_name: str, product_name: str) -> bool`: remove a product from a center. Returns `False` if the center or product does not exist. - `list_products(center_name: str) -> List[str]`: list all products at a center, sorted alphabetically. Returns an empty list if the center does not exist. For example, on a fresh `InventoryManager`: ```text add_center("north") -> True add_center("south") -> True add_center("north") -> False (already exists) add_product("north", "milk", 50) -> True add_product("south", "milk", 10) -> True add_product("north", "eggs", 30) -> True get_availability("milk") -> 60 transfer_stock("north", "south", "milk", 20) -> True (north 30, south 30) transfer_stock("south", "north", "milk", 100) -> False (south has only 30) list_products("north") -> ["eggs", "milk"] set_replenishment_strategy("milk", 25, 15) -> None (no center is below 25) update_stock("north", "milk", 5) -> True (5 < 25, so north becomes 40) get_stock("north", "milk") -> 40 get_availability("milk") -> 70 get_stock("east", "milk") -> -1 (no such center) ``` ```hint Pick the core structure Look for one structure that answers both a per-center lookup and a cross-center total, and work out what `get_availability` has to scan. ``` ```hint Atomic transfers A transfer is a check followed by two writes. Think about what another thread could do between the check and the writes, and how to rule that out. ``` ```hint What pluggable asks for Ask what the inventory needs from a replenishment rule, given a center's current stock, and put that behind an interface. Watch for a helper that tries to take a lock its caller already holds. ``` ### Constraints & Assumptions - `1 <= number of centers <= 100` - `1 <= number of products per center <= 10^4` - `0 <= quantity <= 10^6` - Center and product names are non-empty strings. - Replenishment threshold and amount are non-negative integers. - A product name means the same product at every center, which is why `get_availability` can sum across centers. ### Clarifying Questions to Ask - When a rule is set, should centers that are already below the threshold be topped up right away? Should `add_product` run the check too? - If the destination center does not stock the product yet, should a transfer create it there? - What should `transfer_stock` return when the source and destination are the same center? - Does "sorted alphabetically" mean plain case-sensitive string order? - How much contention should the design expect, for example many threads writing to different centers at the same time? ### What a Strong Answer Covers - A data model for per-center stock and the cost of per-center versus global queries. - Exact validation and return values for each method, including missing centers or products and non-positive transfer quantities. - How a transfer stays atomic, with every check and both writes inside one critical section. - A replenishment design behind an interface, with a clear statement of which operations trigger it. - The locking scheme and its trade-offs, including how finer-grained locks would avoid deadlock. ### Follow-up Questions - How would you let transfers between different pairs of centers run in parallel without risking deadlock? - `get_availability` is called far more often than any write. How would you make it O(1) and keep it consistent? - How would you add reservations, where an order holds stock and later commits or releases it, without overselling? - What changes if each fulfillment center owns its own database and a transfer crosses the network?

Overview: Design and implement a grocery inventory manager that tracks stock across several fulfillment centers, moves stock between them atomically and tops it up through a pluggable replenishment rule. It tests low-level design, data modeling, input validation and safe locking under concurrent calls.

Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a grocery inventory management system that spans multiple fulfillment centers. Each center keeps its own stock of products, and the system must support adding centers, adding products with initial quantities, updating stock, and querying availability both per center and globally.

The system must support transferring stock between fulfillment centers atomically, so that a product can never be transferred beyond the quantity available at the source center.

It must also support a pluggable replenishment strategy. When a rule is configured for a product and the stock of that product at any center falls below the rule's threshold, the stock is automatically replenished to threshold + replenish_amount. All operations must be safe under concurrent calls from multiple threads.

Implement a class InventoryManager with these methods:

  • add_center(center_name: str) -> bool : add a new fulfillment center. Returns True if added, False if it already exists.
  • add_product(center_name: str, product_name: str, quantity: int) -> bool : add a product to a center with an initial quantity. Returns False if the center does not exist or the product already exists at that center.
  • update_stock(center_name: str, product_name: str, quantity: int) -> bool : set the stock of a product at a center. Returns False if the center or product does not exist. Triggers the replenishment check.
  • get_stock(center_name: str, product_name: str) -> int : get the stock of a product at one center. Returns -1 if the center or product does not exist.
  • get_availability(product_name: str) -> int : get the total stock of a product across all fulfillment centers. Returns 0 if the product is not found.
  • transfer_stock(from_center: str, to_center: str, product_name: str, quantity: int) -> bool : move a quantity of a product between centers. Returns True on success, False if stock is insufficient or a center or product is invalid.
  • set_replenishment_strategy(product_name: str, threshold: int, replenish_amount: int) -> None : set the replenishment rule for a product. If its stock falls below threshold at any center, it is replenished to threshold + replenish_amount .
  • remove_product(center_name: str, product_name: str) -> bool : remove a product from a center. Returns False if the center or product does not exist.
  • list_products(center_name: str) -> List[str] : list all products at a center, sorted alphabetically. Returns an empty list if the center does not exist.

For example, on a fresh InventoryManager:

add_center("north")                            -> True
add_center("south")                            -> True
add_center("north")                            -> False   (already exists)
add_product("north", "milk", 50)               -> True
add_product("south", "milk", 10)               -> True
add_product("north", "eggs", 30)               -> True
get_availability("milk")                       -> 60
transfer_stock("north", "south", "milk", 20)   -> True    (north 30, south 30)
transfer_stock("south", "north", "milk", 100)  -> False   (south has only 30)
list_products("north")                         -> ["eggs", "milk"]
set_replenishment_strategy("milk", 25, 15)     -> None    (no center is below 25)
update_stock("north", "milk", 5)               -> True    (5 < 25, so north becomes 40)
get_stock("north", "milk")                     -> 40
get_availability("milk")                       -> 70
get_stock("east", "milk")                      -> -1      (no such center)

Constraints & Assumptions

  • 1 <= number of centers <= 100
  • 1 <= number of products per center <= 10^4
  • 0 <= quantity <= 10^6
  • Center and product names are non-empty strings.
  • Replenishment threshold and amount are non-negative integers.
  • A product name means the same product at every center, which is why get_availability can sum across centers.

Clarifying Questions to Ask Guidance

  • When a rule is set, should centers that are already below the threshold be topped up right away? Should add_product run the check too?
  • If the destination center does not stock the product yet, should a transfer create it there?
  • What should transfer_stock return when the source and destination are the same center?
  • Does "sorted alphabetically" mean plain case-sensitive string order?
  • How much contention should the design expect, for example many threads writing to different centers at the same time?

What a Strong Answer Covers Guidance

  • A data model for per-center stock and the cost of per-center versus global queries.
  • Exact validation and return values for each method, including missing centers or products and non-positive transfer quantities.
  • How a transfer stays atomic, with every check and both writes inside one critical section.
  • A replenishment design behind an interface, with a clear statement of which operations trigger it.
  • The locking scheme and its trade-offs, including how finer-grained locks would avoid deadlock.

Follow-up Questions Guidance

  • How would you let transfers between different pairs of centers run in parallel without risking deadlock?
  • get_availability is called far more often than any write. How would you make it O(1) and keep it consistent?
  • How would you add reservations, where an order holds stock and later commits or releases it, without overselling?
  • What changes if each fulfillment center owns its own database and a transfer crosses the network?