Watch 10 video solutions for Product Price at a Given Date, a medium level problem involving Database. This walkthrough by Learn With Chirag has 13,548 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Products
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | new_price | int | | change_date | date | +---------------+---------+ (product_id, change_date) is the primary key (combination of columns with unique values) of this table. Each row of this table indicates that the price of some product was changed to a new price at some date.
Write a solution to find the prices of all products on 2019-08-16. Assume the price of all products before any change is 10.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Products table: +------------+-----------+-------------+ | product_id | new_price | change_date | +------------+-----------+-------------+ | 1 | 20 | 2019-08-14 | | 2 | 50 | 2019-08-14 | | 1 | 30 | 2019-08-15 | | 1 | 35 | 2019-08-16 | | 2 | 65 | 2019-08-17 | | 3 | 20 | 2019-08-18 | +------------+-----------+-------------+ Output: +------------+-------+ | product_id | price | +------------+-------+ | 2 | 50 | | 1 | 35 | | 3 | 10 | +------------+-------+
Problem Overview: You are given a table of product price changes where each record stores product_id, new_price, and the change_date. The task is to return the price of every product on a specific date. If a product has no recorded price change before that date, its price defaults to 10. The challenge is determining the most recent price update that occurred on or before the given date for each product.
Approach 1: Sort and Group by Product ID and Change Date (O(n log n) time, O(n) space)
This approach processes the data similarly to how you would handle event timelines. First, sort all records by product_id and change_date. Then iterate through the sorted list while grouping entries by product. For each product, keep updating the current price whenever the change_date is less than or equal to the target date. The last valid update becomes the product's price. If no valid update exists, return the default price of 10. Sorting ensures chronological processing, while grouping makes it easy to track the latest valid change for each product.
This technique is straightforward in languages like Python or JavaScript using sorting plus dictionary/group structures. It works well when records must be processed in memory and mirrors patterns used in sorting and timeline-style data problems.
Approach 2: Use SQL-like Logic to Handle Dates (O(n) time, O(n) space)
A more database-oriented solution directly models the problem as a query. For each product, you want the row with the maximum change_date that is still less than or equal to the target date. This can be implemented using filtering plus aggregation logic similar to SQL's MAX(change_date) with grouping. First filter records where change_date <= target_date, then determine the latest change per product. Join or map that result back to retrieve the corresponding price. If a product has no qualifying record, assign the default value.
This mirrors patterns used in database and SQL queries where you compute the latest event before a timestamp. In languages like C++ or C#, you can replicate this with hash maps that track the best date seen per product while iterating through the dataset.
Recommended for interviews: The SQL-style aggregation approach is typically preferred. It directly models the requirement: "latest price change before a given date." Interviewers expect candidates to recognize the max-date-per-group pattern and implement it efficiently. The sorting and grouping approach still demonstrates solid reasoning and works well when the dataset is processed sequentially.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sort and Group by Product ID and Change Date | O(n log n) | O(n) | When processing records sequentially in application code or when sorting simplifies timeline tracking. |
| SQL-like Date Filtering with Latest Change | O(n) | O(n) | Best for database-style problems where you need the latest record before a timestamp for each group. |