Merge sales by region

dictionaries

Merge sales by region

Target Python Interview Question

Target reports online sales and in-store sales as two separate dictionaries that map a region to its sales. Finance wants one combined report.

Write a function merge_totals(online, in_store) that returns a new dictionary with every region from both inputs. Where a region appears in both, add the two amounts together.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

online = {"West": 120, "East": 60}, in_store = {"West": 80, "South": 45}

Output

{"West": 200, "East": 60, "South": 45}

Example 2

Input

online = {}, in_store = {"North": 10}

Output

{"North": 10}

Explanation

In the first example, the West region appears in both reports, so its total is 120 + 80 = 200. East only has online sales and South only has in-store sales, so their amounts are copied as they are.

Submit also runs 3 hidden test cases that check edge cases.