Group Sold Products by Date
Imagine you're building a sales reporting system. You need to efficiently organize sales data to analyze trends over time. This challenge asks you to group a list of sold products, each associated with a sale date, into a data structure that organizes them by date.
Problem Description
You are given a list of product sales records. Each record contains the product name and the date of the sale. Your task is to group these sales records by the sale date, creating a data structure where each date is associated with a list of products sold on that date. The final output should be a data structure that allows easy access to all products sold on a specific date.
Key Requirements:
- The input is a list of sales records. Each record is a pair: (product name, sale date).
- The output should be a data structure (e.g., a dictionary/map/hash table) where the keys are dates and the values are lists of product names sold on that date.
- Dates should be treated as strings in "YYYY-MM-DD" format.
- If a product is sold multiple times on the same date, it should appear multiple times in the list for that date.
- The order of products within each date's list is not important.
- The order of dates in the output is not important.
Expected Behavior:
The function should take the list of sales records as input and return the grouped data structure. It should handle empty input lists gracefully, returning an empty data structure in that case.
Edge Cases to Consider:
- Empty input list.
- Duplicate product sales on the same date.
- Dates in various formats (ensure consistent "YYYY-MM-DD" format).
- Large input lists (consider performance implications).
Examples
Example 1:
Input: [("Apple", "2023-10-26"), ("Banana", "2023-10-26"), ("Orange", "2023-10-27"), ("Apple", "2023-10-27")]
Output: {"2023-10-26": ["Apple", "Banana"], "2023-10-27": ["Orange", "Apple"]}
Explanation: On 2023-10-26, Apple and Banana were sold. On 2023-10-27, Orange and Apple were sold.
Example 2:
Input: [("Apple", "2023-10-26"), ("Banana", "2023-10-26"), ("Apple", "2023-10-26")]
Output: {"2023-10-26": ["Apple", "Banana", "Apple"]}
Explanation: Apple was sold twice on 2023-10-26, so it appears twice in the list for that date.
Example 3:
Input: []
Output: {}
Explanation: An empty input list results in an empty output data structure.
Constraints
- The number of sales records in the input list can be up to 10,000.
- Each product name is a string with a maximum length of 50 characters.
- Each sale date is a string in the "YYYY-MM-DD" format.
- The solution should have a time complexity of O(n), where n is the number of sales records. (This is to ensure reasonable performance with larger datasets).
- The solution should use a reasonable amount of memory (avoiding excessive memory allocation).
Notes
Consider using a hash table (dictionary/map) to efficiently group the sales records by date. Iterate through the input list once, adding each product to the appropriate list in the hash table. Think about how to handle the date as a key in the hash table. The key is the date string. The value is a list of product names.