Seasonal Sales Analysis - Problem

You are given two tables: sales and products.

The sales table contains information about product sales including sale_id, product_id, sale_date, quantity sold, and price per unit.

The products table contains product information including product_id, product_name, and category.

Write a solution to find the most popular product category for each season. The seasons are defined as:

  • Winter: December, January, February
  • Spring: March, April, May
  • Summer: June, July, August
  • Fall: September, October, November

The popularity of a category is determined by the total quantity sold in that season. If there is a tie, select the category with the highest total revenue (quantity × price). If there is still a tie, return the lexicographically smaller category.

Return the result table ordered by season in ascending order.

Table Schema

sales
Column Name Type Description
sale_id PK int Unique identifier for each sale
product_id int Foreign key to products table
sale_date date Date when the sale occurred
quantity int Number of units sold
price decimal Price per unit
Primary Key: sale_id
products
Column Name Type Description
product_id PK int Unique identifier for each product
product_name varchar Name of the product
category varchar Product category
Primary Key: product_id

Input & Output

Example 1 — Basic Seasonal Analysis
Input Tables:
sales
sale_id product_id sale_date quantity price
1 101 2023-03-15 50 25
2 102 2023-04-10 30 40
3 103 2023-06-20 80 15
4 101 2023-07-05 40 25
5 104 2023-09-12 60 30
products
product_id product_name category
101 Laptop Electronics
102 T-shirt Clothing
103 Soccer Ball Sports
104 Running Shoes Sports
Output:
season category
Spring Electronics
Summer Sports
Fall Sports
💡 Note:

In Spring (March-May), Electronics has 50 units vs Clothing's 30 units. In Summer (June-August), Sports has 80 units vs Electronics' 40 units. In Fall (September-November), Sports has the only sales with 60 units. Winter has no sales data.

Example 2 — Tiebreaker by Revenue
Input Tables:
sales
sale_id product_id sale_date quantity price
1 101 2023-01-15 20 100
2 102 2023-02-10 20 50
3 103 2023-03-20 15 80
products
product_id product_name category
101 Phone Electronics
102 Jacket Clothing
103 Book Books
Output:
season category
Spring Books
Winter Electronics
💡 Note:

In Winter, both Electronics and Clothing have 20 units sold, but Electronics has higher revenue ($2000 vs $1000). In Spring, Books is the only category. The tiebreaker logic prioritizes quantity first, then revenue.

Constraints

  • 1 ≤ sale_id ≤ 10000
  • 1 ≤ product_id ≤ 1000
  • sale_date is a valid date
  • 1 ≤ quantity ≤ 1000
  • 0.01 ≤ price ≤ 10000.00
  • product_name and category are non-empty strings

Visualization

Tap to expand
Seasonal Sales Analysis INPUT Season Category Qty Revenue Winter Electronics 150 $45000 Winter Clothing 200 $30000 Spring Garden 300 $15000 Spring Sports 300 $18000 Summer Beach 400 $20000 Fall Books 250 $12500 Sales Records Array sales: Sale[] season category quantity revenue ALGORITHM STEPS 1 Group by Season Partition data into 4 seasons 2 Aggregate by Category Sum qty and revenue per cat 3 Apply Tiebreakers Qty --> Revenue --> Alpha 4 Select Winner Pick top category per season Season Compare Winner Spring 300=300 Sports (18k > 15k revenue) Sort Priority: 1. Total Quantity (DESC) 2. Total Revenue (DESC) 3. Category Name (ASC) FINAL RESULT Winter Clothing (Qty: 200) $30,000 Spring Sports (Qty: 300) $18,000 Summer Beach (Qty: 400) $20,000 Fall Books (Qty: 250) $12,500 Output Format: { "Winter": "Clothing", "Spring": "Sports", ... OK Key Insight: Use HashMap with (season, category) as composite key for O(n) aggregation. The tiebreaker chain (quantity --> revenue --> lexicographical) ensures deterministic results. Group-by-season with max-heap per season achieves O(n log k) where k = categories. Time: O(n), Space: O(n). Time: O(n) Space: O(n) TutorialsPoint - Seasonal Sales Analysis | Optimal Solution
Asked in
Amazon 28 Microsoft 15 Google 22
23.4K Views
Medium Frequency
~18 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen