96. Design Warehouse Stores Inventory Updater System

Design Warehouse Stores Inventory Update
Design and implement an interface for managing warehouse and store inventory updates. The system has two entities: warehouses and stores. Whenever new inventory is added to a warehouse, all stores mapped to that warehouse must be updated automatically. Each store is mapped to exactly one warehouse, while one warehouse may be mapped to zero or more stores.

The goal is to support inventory synchronization between warehouses and stores in a clean and extensible way. The design should make it easy to notify all affected stores whenever warehouse inventory changes.

Entity Rules

  • A store can be mapped to only one warehouse.
  • A warehouse can be mapped to many stores.
  • A warehouse may exist even if no store is currently mapped to it.
  • Inventory is maintained per productId.
  • When inventory is added to a warehouse, every mapped store must reflect the latest cumulative quantity for that product.
  • Each store keeps a mirrored view of the inventory of its mapped warehouse.

Class Interface

Constructor

WarehouseStoreInventoryUpdate(List<String> warehouseIds)
  • Initializes the system with the given warehouse ids.
  • Each warehouse id is unique.
  • No store is mapped initially.

Method 1

boolean registerStore(String storeId, String warehouseId)
  • Registers a store and maps it to exactly one warehouse.
  • Returns true if the mapping is created successfully.
  • Returns false if the warehouse does not exist or the store is already registered.
  • After successful registration, the store must immediately reflect the current inventory of its mapped warehouse, and also receive all future updates.

Method 2

boolean addInventory(String warehouseId, String productId, int quantity)
  • Adds new inventory for the given product to the given warehouse.
  • Returns true if the inventory is added successfully.
  • Returns false if the warehouse does not exist or quantity ≤ 0.
  • All stores mapped to that warehouse must be updated automatically after a successful call.
  • The quantity stored for a product is cumulative.

Method 3

List<String> getWarehouseInventory(String warehouseId)
  • Returns the current inventory view of the given warehouse.
  • Each entry must use the format productId=<value>, quantity=<value>.
  • Return an empty List<String> if the warehouse does not exist or has no inventory.
  • The returned list must be sorted lexicographically by productId for deterministic output.

Method 4

List<String> getStoreInventory(String storeId)
  • Returns the mirrored inventory view of the given store.
  • Each entry must use the format productId=<value>, quantity=<value>.
  • Return an empty List<String> if the store does not exist or its mapped warehouse has no inventory.
  • The returned list must be sorted lexicographically by productId for deterministic output.

Method 5

List<String> getStoresForWarehouse(String warehouseId)
  • Returns all store ids currently mapped to the given warehouse.
  • Return an empty List<String> if the warehouse does not exist or no stores are mapped to it.
  • The returned list must be sorted lexicographically.

Output Format

  • Warehouse inventory and store inventory must be returned as List<String>.
  • Each inventory item must be represented as productId=<value>, quantity=<value>.
  • This avoids using custom objects in the method output.

Constraints

  • 1 ≤ warehouseIds.size() ≤ 10^4
  • 1 ≤ warehouseId.length() ≤ 50
  • 1 ≤ storeId.length() ≤ 50
  • 1 ≤ productId.length() ≤ 50
  • 1 ≤ quantity ≤ 10^9
  • All warehouse ids in the constructor are unique.
  • A store id is unique across the system.
  • A product quantity in a warehouse is the sum of all successful addInventory calls for that product.
  • All method calls should be handled efficiently for repeated updates and lookups.

Notes

  • The focus is on correct mapping and automatic propagation of warehouse inventory updates to stores.
  • The system should behave deterministically, so all returned lists must be sorted as specified.
  • Parameter names are shown in the example calls below only for readability.

Examples

Example 1

WarehouseStoreInventoryUpdate system = new WarehouseStoreInventoryUpdate(warehouseIds=List.of("WH-1", "WH-2"))

system.registerStore(storeId="STORE-1", warehouseId="WH-1")
Output: true

system.registerStore(storeId="STORE-2", warehouseId="WH-1")
Output: true

system.getStoresForWarehouse(warehouseId="WH-1")
Output: List.of("STORE-1", "STORE-2")

system.addInventory(warehouseId="WH-1", productId="PEN", quantity=100)
Output: true

system.getWarehouseInventory(warehouseId="WH-1")
Output: List.of("productId=PEN, quantity=100")

system.getStoreInventory(storeId="STORE-1")
Output: List.of("productId=PEN, quantity=100")

system.getStoreInventory(storeId="STORE-2")
Output: List.of("productId=PEN, quantity=100")

Example 2

system.registerStore(storeId="STORE-3", warehouseId="WH-2")
Output: true

system.addInventory(warehouseId="WH-1", productId="BOOK", quantity=50)
Output: true

system.addInventory(warehouseId="WH-1", productId="PEN", quantity=20)
Output: true

system.addInventory(warehouseId="WH-2", productId="PEN", quantity=10)
Output: true

system.getWarehouseInventory(warehouseId="WH-1")
Output: List.of("productId=BOOK, quantity=50", "productId=PEN, quantity=120")

system.getStoreInventory(storeId="STORE-1")
Output: List.of("productId=BOOK, quantity=50", "productId=PEN, quantity=120")

system.getStoreInventory(storeId="STORE-3")
Output: List.of("productId=PEN, quantity=10")

Example 3

system.registerStore(storeId="STORE-1", warehouseId="WH-2")
Output: false

system.addInventory(warehouseId="WH-9", productId="PENCIL", quantity=40)
Output: false

system.addInventory(warehouseId="WH-1", productId="ERASER", quantity=0)
Output: false

system.getStoreInventory(storeId="STORE-999")
Output: List.of()

system.getStoresForWarehouse(warehouseId="WH-999")
Output: List.of()




Please use Laptop/Desktop or any other large screen to add/edit code.