Start learning free

LESSON 132 OF 289 · DICTIONARIES - CHALLENGES

Restock in TypeScript: Updating a Count in an Object

Practice Read the full explanation

A shop keeps its stock in inventory, a dictionary from item names to how many of each item it has. A delivery brings amount more of item. Add amount to the count of item and return the updated dictionary.

Only the count of item changes: every other entry stays as it is. The order of the entries doesn't matter.

 

Example 1:

Input: inventory = {"apples": 5, "bananas": 2, "cherries": 10}, item = "bananas", amount = 3
Output: {"apples": 5, "bananas": 5, "cherries": 10}
Explanation: "bananas" had a count of 2, and 2 + 3 = 5. The apples and the cherries keep their counts.

Example 2:

Input: inventory = {"milk": 0}, item = "milk", amount = 12
Output: {"milk": 12}
Explanation: A count of 0 means the milk was sold out. The delivery brings it to 0 + 12 = 12.

Example 3:

Input: inventory = {"pens": 7, "paper": 3}, item = "pens", amount = 1
Output: {"pens": 8, "paper": 3}
Explanation: One more pen: 7 + 1 = 8.

 

Constraints:

  • item is always a key of inventory.
  • amount is at least 1.
  • Every count is 0 or more.
  • Item names are single lowercase words, and they are all different.
STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · DICTIONARIES - CHALLENGES

    Restock in TypeScript, explained

    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

    1. Add amount to the stored count with +=, using inventory[item] as the target. The brackets use the value of the variable item as the key.
    2. 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}, both inv and 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 be undefined, and undefined + amount is NaN. Running miss["zz"] += 2 on {a: 1} stores NaN under "zz". This lesson promises the item is present, so it does not come up.
    • Numbers are plain numbers. TypeScript has one number type. 0 + 12 is 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] = amount replaces the count instead of adding to it, and the bananas would become 3.
    • Using the dot. inventory.item += amount works on a key literally named item, not on the key that the variable item holds. 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}.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free