Imagine you're building an analytics dashboard for an e-commerce platform! ๐ You have access to two crucial tables: Sales (tracking every transaction) and Product (containing pricing information).
Your mission is to calculate the total spending for each user by combining sales data with product prices. This is a common business intelligence task that helps companies understand customer purchasing behavior.
Tables:
Sales table contains:
sale_id: Unique transaction identifierproduct_id: References the product boughtuser_id: The customer who made the purchasequantity: How many units were purchased
Product table contains:
product_id: Unique product identifierprice: Price per unit of the product
Goal: Calculate each user's total spending (quantity ร price for all their purchases) and return results ordered by spending (descending), with ties broken by user_id (ascending).
Input & Output
Visualization
Time & Space Complexity
JOIN operation is O(nรm) where n,m are table sizes, plus O(k log k) for sorting where k is number of users
Space for k unique users in the result set
Constraints
- 1 โค sales.length โค 105
- 1 โค products.length โค 104
- 1 โค sale_id, product_id, user_id โค 108
- 1 โค quantity โค 103
- 1 โค price โค 106
- All product_ids in Sales table exist in Product table