An analytics platform stores order data in a warehouse where each record is a row of strings. The first row holds the column headers; every row after that is an order entry with string values. At minimum, the following columns exist in each record: order_id, cost, sell_price, product, and date (written as "YYYY-MM-DD"). Extra columns (for instance state) may appear as well.
Build a DataWarehouse class that lets you query a pivot-style summary: compute the net profit (sell_price − cost) for every distinct value of a chosen column (pivotColumn), considering only orders whose date is on or after a given startDate. The pivotColumn could be "state", "product", or any other column present in the header.
The class must support:
class DataWarehouse {
public DataWarehouse(List<List<String>> data)
public String getMostProfitable(String pivotColumn, String startDate)
}
getMostProfitable – Looks at records with date >= startDate. For each distinct value in pivotColumn, totals the net profit and returns the value with the largest total.
"".Constraints
data always has at least the header row."YYYY-MM-DD" format.1 ≤ data.size() ≤ 10^51 ≤ data[0].size() ≤ 20Example
Input:
["DataWarehouse", "getMostProfitable", "getMostProfitable", "getMostProfitable", "getMostProfitable"]
[
[["order_id", "cost", "sell_price", "product", "date", "state"],
["23", "12", "18", "cheese", "2023-12-04", "CA"],
["24", "5", "12", "melon", "2023-12-04", "OR"],
["25", "25", "31", "cheese", "2023-12-05", "OR"],
["26", "4", "12", "bread", "2023-12-05", "CA"],
["25", "10", "14", "cheese", "2023-12-06", "CA"],
["26", "5", "6", "bread", "2023-12-06", "OR"]],
["state", "2023-12-05"],
["product", "2023-12-05"],
["product", "2025-12-05"],
["color", "2023-12-01"]
]
Output:
[null, "CA", "cheese", "", ""]
Explanation
| order_id | cost | sell_price | product | date | state |
|---|---|---|---|---|---|
| 23 | 12 | 18 | cheese | 2023-12-04 | CA |
| 24 | 5 | 12 | melon | 2023-12-04 | OR |
| 25 | 25 | 31 | cheese | 2023-12-05 | OR |
| 26 | 4 | 12 | bread | 2023-12-05 | CA |
| 25 | 10 | 14 | cheese | 2023-12-06 | CA |
| 26 | 5 | 6 | bread | 2023-12-06 | OR |
DataWarehouse warehouse = new DataWarehouse(data);warehouse.getMostProfitable("state", "2023-12-05"); → "CA". Rows on or after 2023-12-05: CA net profit = (12−4) + (14−10) = 8 + 4 = 12; OR net profit = (31−25) + (6−5) = 6 + 1 = 7.warehouse.getMostProfitable("product", "2023-12-05"); → "cheese". Profits after that date: cheese = 10, bread = 9, melon = 0.warehouse.getMostProfitable("product", "2025-12-05"); → "", because no date qualifies.warehouse.getMostProfitable("color", "2023-12-01"); → "", because the column "color" does not exist in the header.