Ctrl K

Two Warehouses, One Report

Easy
Two warehouses each keep their own stock list: first and second. Every entry is a pair [code, qty]: an item code and how many units of it sit in that warehouse. Codes never repeat within one list, but the same item can be stocked in both warehouses. Build a combined report: for every code that appears in at least one warehouse, return the pair [code, total], where total is the quantity across both warehouses. Sort the pairs by code in ascending order.
Example 1
first=[[7, 2], [3, 10]], second=[[3, 5], [12, 1], [7, 4]] → [[3, 15], [7, 6], [12, 1]]

Item 3 is in both warehouses: 10 + 5 = 15. Item 7: 2 + 4 = 6. Item 12 is only in the second one. Codes go in ascending order.

Example 2
first=[[5, 9]], second=[[2, 9]] → [[2, 9], [5, 9]]

No item is shared, so the lists are just joined and sorted by code.

Example 3
first=[[1, 1], [2, 2]], second=[[2, 3], [1, 4]] → [[1, 5], [2, 5]]
Constraints
  • 1 ≤ len(first), len(second) ≤ 1000
  • Each entry is a pair [code, qty]
  • 1 ≤ code, qty ≤ 1000
  • Codes are distinct within first, and within second
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsKitchen Order Board MediumFlowers in Bloom on Visit Day HardClimbing Along Rows and Columns HardThe Candidate With Most Votes EasyDoes Any Value Repeat Easy
loading Python…
Input
first=[[7, 2], [3, 10]], second=[[3, 5], [12, 1], [7, 4]]
Expected
[[3, 15], [7, 6], [12, 1]]

Run checks the first 3 tests in your browser. Submit checks all 98 tests on the server, hidden ones included.