Back to Practice
#0228
Inventory Delta
MediumPython25 min20 XP
Problem
Given previous and current inventory dictionaries, return item deltas as current - previous for every item seen in either dictionary.
Why This Matters
Deltas are common in dashboards, sync systems, stock tracking, and audit logs.
Function Signature
def inventory_delta(previous, current):
Examples
Example 1
Inputprevious = {"apple": 5, "banana": 2}, current = {"apple": 3, "pear": 4}
Output{"apple": -2, "banana": -2, "pear": 4}
apple decreased by 2, banana disappeared, and pear is new.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Prefer readable Python before micro-optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputprevious = {"apple": 5, "banana": 2}, current = {"apple": 3, "pear": 4}
Expected{"apple": -2, "banana": -2, "pear": 4}
apple decreased by 2, banana disappeared, and pear is new.
Hidden Test Categories
same dictionariesnew items onlyremoved items only