Second Highest Sales
Start Timer
0:00:00
Let’s say at Merck we have a list of sales records. Write a function second_highest_sales that finds, for each region, the product with the second-highest total sales.
Each record is a dict with:
- region
- product
- amount
Return a dict mapping each region to that product.
Rules - Rank the products in each region by total sales descending, breaking ties by product name ascending, and return the product in position 2 of that ordering. - If a region has fewer than two distinct products, skip it.
Example:
Input:
sales = [
{'region': 'East', 'product': 'A', 'amount': 100},
{'region': 'East', 'product': 'A', 'amount': 50},
{'region': 'East', 'product': 'B', 'amount': 120},
{'region': 'East', 'product': 'C', 'amount': 30},
{'region': 'West', 'product': 'A', 'amount': 200},
{'region': 'West', 'product': 'B', 'amount': 90},
]
Output:
second_highest_sales(sales) -> {'East': 'B', 'West': 'B'}
In East the totals are A=150, B=120, C=30, so the second-highest is B (120). In West, A=200 and B=90, so the second-highest is B (90).
.
.
.
.
.
.
.
.
.
Comments