Merge Inventories with Conflict Resolution

Medium ⏱ 12 min 62% acceptance ★★★★★ 4.7
Write a function merge_inventories(warehouse_a, warehouse_b) that merges two stock-count dicts (product name -> quantity). When a product appears in both, sum the quantities rather than letting one override the other. Products unique to either warehouse keep their original quantity.

Examples

Example 1
Input
merge_inventories({'pen': 10, 'bag': 5}, {'bag': 3, 'mug': 7})
Output
{'pen': 10, 'bag': 8, 'mug': 7}
Explanation

bag: 5 + 3 = 8; pen and mug come from a single side.

Constraints

  • Neither input dict should be mutated.
  • Shared keys must be summed, not overwritten.

Topics

Dictionariesmergingconflict resolution

Companies

WalmartZomato

Hints

Hint 1

Start from a copy of warehouse_a, then for each key in warehouse_b add to the existing value using `result[k] = result.get(k, 0) + v`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.