Design a Game Inventory with Slot Capacity, Stackable Consumables and Weapon Durability
Company: Epic Games
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a player inventory system that manages different types of items. The inventory has a fixed number of slots (capacity), where each unique item name occupies one slot regardless of stack size.
The system supports two item types. Weapons are unique, non-stackable items with durability and attack power. Using a weapon reduces its durability by 1, and it is destroyed when durability reaches 0. Consumables are stackable items with a heal amount and a max stack size. Using a consumable removes one from the stack and returns the heal amount.
The inventory should enforce capacity limits when adding new item types, support stacking for consumables, and clean up item slots when items are fully consumed or destroyed.
Implement a class `Inventory(capacity: int)` with the following methods:
- `addWeapon(name: str, durability: int, attackPower: int) -> bool`: add a weapon to the inventory. Weapons are unique and non-stackable. Returns `True` if added successfully, `False` if the inventory is full or an item with the same name (weapon or consumable) already exists.
- `addConsumable(name: str, healAmount: int, quantity: int, maxStack: int) -> bool`: add consumable items to the inventory. If the consumable already exists, adds to the existing stack (capped at `maxStack`). If new, creates a slot if capacity allows. Units that do not fit are discarded. Returns `True` if any items were added, and `False` if the name belongs to a weapon.
- `removeItem(name: str, quantity: int) -> bool`: remove a specified quantity of an item. A weapon counts as a quantity of 1. Returns `True` if removed successfully. If the item does not exist or holds fewer than `quantity` units, returns `False` and changes nothing. The item slot is freed when quantity reaches 0.
- `useItem(name: str) -> int`: use an item. For weapons, reduces durability by 1 and returns attack power (the weapon is destroyed at 0 durability). For consumables, consumes one and returns the heal amount. Returns -1 if the item is not found.
- `getItem(name: str) -> str`: get a formatted string describing the item. Returns `"Not found"` if the item does not exist.
- `getItems() -> List[str]`: get all items in the inventory sorted alphabetically by name, each as a formatted string.
- `getSlotCount() -> int`: return the number of occupied inventory slots.
- `getRemainingCapacity() -> int`: return the number of available inventory slots.
`getItem` and `getItems` describe an item with one of these two lines, where `maxDurability` is the durability the weapon had when it was added:
```text
<name>: weapon [atk=<attackPower>, dur=<durability>/<maxDurability>]
<name>: consumable [heal=<healAmount>, qty=<quantity>/<maxStack>]
```
Example 1:
```text
inv = Inventory(2)
inv.addWeapon("Sword", 3, 25) # True
inv.addConsumable("Potion", 50, 5, 10) # True (new slot, qty 5)
inv.addConsumable("Potion", 50, 8, 10) # True (only 5 more fit, qty 10)
inv.addConsumable("Potion", 50, 1, 10) # False (stack is full)
inv.addWeapon("Axe", 5, 40) # False (no free slot)
inv.getItems()
# ["Potion: consumable [heal=50, qty=10/10]", "Sword: weapon [atk=25, dur=3/3]"]
inv.getRemainingCapacity() # 0
```
The second `Potion` call succeeds even though both slots are taken, because `Potion` already has a slot.
Example 2:
```text
inv = Inventory(3)
inv.addWeapon("Dagger", 2, 15) # True
inv.addConsumable("Elixir", 120, 2, 5) # True
inv.useItem("Dagger") # 15 (durability 2 -> 1)
inv.getItem("Dagger") # "Dagger: weapon [atk=15, dur=1/2]"
inv.useItem("Dagger") # 15 (durability 0, Dagger is destroyed)
inv.getItem("Dagger") # "Not found"
inv.useItem("Elixir") # 120 (qty 2 -> 1)
inv.removeItem("Elixir", 2) # False (only 1 left, nothing removed)
inv.removeItem("Elixir", 1) # True (slot freed)
inv.useItem("Elixir") # -1
inv.getSlotCount() # 0
inv.getRemainingCapacity() # 3
```
```hint Model one slot
Decide what a single dictionary entry holds, and which fields belong only to weapons, only to consumables, or to both.
```
```hint Order of checks
Walk through adding more of an existing consumable when every slot is already taken. Then list every operation that can bring a count or a durability to zero.
```
### Constraints & Assumptions
- `1 <= capacity <= 100`
- Item names are non-empty strings containing only alphanumeric characters and spaces.
- `1 <= durability <= 1000`
- `1 <= attackPower <= 500`
- `1 <= healAmount <= 200`
- `1 <= maxStack <= 99`
- `1 <= quantity` for add and remove operations.
- Weapons are unique (non-stackable) items.
- Consumables are stackable up to `maxStack`.
### Clarifying Questions to Ask
- When more units are added to an existing stack and the call passes a different `healAmount` or `maxStack`, which values apply?
- Does "alphabetically" mean plain character-code order, or a case-insensitive order?
- Are more item types likely later (armor, ammunition, quest items)? The answer decides how much structure the item model needs.
### What a Strong Answer Covers
- A data model where one entry per name is one slot, with weapon state and consumable state kept separate.
- The order of checks in each add method (name already present, existing stack, free slot) and the return value for each outcome.
- Freeing the slot on every path that reaches zero: using up a stack, breaking a weapon, and removing items.
- The exact output strings and the sort order used by `getItems`.
- The cost of each operation and how the design extends to new item types.
### Follow-up Questions
- How would you add a third item type, such as armor, without editing every method of `Inventory`?
- How would you change the design if units beyond `maxStack` spilled into a new stack in another slot?
- How would you add a total weight limit on top of the slot limit?
- If a game server and several clients can change the same inventory, how do you keep it consistent?
Overview: Design a game inventory with a fixed number of slots, unique weapons that lose durability with each use, and consumables that stack up to a limit. It tests object modeling, capacity and stacking rules, freeing slots when items run out, and exact output formatting.