Problem overview
A shop keeps its stock in inventory, an object used as a dictionary from item names to counts. The method restock receives the inventory, an item and an amount. A delivery brings amount more of item, so the method adds it to that item's count and returns the updated dictionary. Every other entry stays as it is.
With {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas" and amount = 3, the bananas go from 2 to 5 and the result is {"apples": 5, "bananas": 5, "cherries": 10}. With {"milk": 0}, "milk" and 12, the result is {"milk": 12}.
Approach
An entry of an object can be read and written with square brackets, and inventory[item] works on the left of an assignment just as a variable does. The compound assignment inventory[item] += amount reads the old count, adds amount, and stores the sum under the same key. No other key is touched.
The item is always a key of the inventory, so the old count exists and the addition is safe. Because the object is updated where it stands, returning it afterwards hands back every entry with only one count changed.
Step by step
- Add
amountto the stored count with+=, usinginventory[item]as the target. The brackets use the value of the variableitemas the key. - Return
inventory. It is the same object, now holding the new count.
The solution in TypeScript
class Solution {
restock(inventory: { [key: string]: number }, item: string, amount: number): { [key: string]: number } {
inventory[item] += amount;
return inventory;
}
}
Details worth knowing:
- The object is changed in place. The method does not build a copy. A caller who keeps a reference to the object they passed in sees the new count too, and the returned value is that same object. After
restock(inv, "a", 2)on{a: 1}, bothinvand the result hold{"a": 3}. - The type describes the shape.
{ [key: string]: number }means any string key with a number value. TypeScript erases it when it compiles to JavaScript, so nothing checks the values at run time. - A missing key gives
NaN. If the key did not exist,inventory[item]would beundefined, andundefined + amountisNaN. Runningmiss["zz"] += 2on{a: 1}storesNaNunder"zz". This lesson promises the item is present, so it does not come up. - Numbers are plain numbers. TypeScript has one
numbertype.0 + 12is 12, and a count of 0 needs no special case.
Complexity
Time: O(1) on average. One lookup and one store, whatever the number of items.
Space: O(1). No new object is created.
Edge cases and common mistakes
- Zero stock. A count of 0 is a valid count.
{"milk": 0}with 12 gives{"milk": 12}. - Writing
=instead of+=.inventory[item] = amountreplaces the count instead of adding to it, and the bananas would become 3. - Using the dot.
inventory.item += amountworks on a key literally nameditem, not on the key that the variableitemholds. Use brackets. - Returning only the count. The method returns the whole dictionary, not the new number.
- Other entries. They must keep their counts. With
{"pens": 7, "paper": 3}and"pens","paper"stays at 3.
Testing
{"apples": 5, "bananas": 2, "cherries": 10},"bananas", 3 gives{"apples": 5, "bananas": 5, "cherries": 10}.{"milk": 0},"milk", 12 gives{"milk": 12}.{"pens": 7, "paper": 3},"pens", 1 gives{"pens": 8, "paper": 3}.
