Update Joins With data.table := in R: Add Prices, Impute Defaults and Total Revenue by Category
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement three functions in `/app/solution.R` that enrich orders with `data.table` update joins. Each function adds columns by reference with `:=`, and it does so on a copy: the caller's `orders` must be unchanged after the call.
- `update_join_revenue(orders, prices)`: on a copy of `orders` (`order_id`, `product`, `qty`), add `unit_price` with an update join on `product` (`NA` if there is no match) and `revenue = qty * unit_price`. Return the data.table.
- `fill_and_flag(orders, prices, default_price)`: after the same join, add a logical column `imputed` (`TRUE` where `unit_price` is `NA`), replace those prices with `default_price` by reference, then compute `revenue`.
- `revenue_by_category(orders, prices, product_category)`: update-join `unit_price` and `category`, compute `revenue`, and return a data.table of `category` and `total_revenue` (the sum of `revenue` with `na.rm = TRUE`) sorted ascending by `category`.
Example:
```r
library(data.table)
orders <- data.table(order_id = 1:3, product = c("a", "b", "z"), qty = c(2L, 1L, 4L))
prices <- data.table(product = c("a", "b"), unit_price = c(10, 20))
update_join_revenue(orders, prices)
# order_id product qty unit_price revenue
# 1: 1 a 2 10 20
# 2: 2 b 1 20 20
# 3: 3 z 4 NA NA
fill_and_flag(orders, prices, default_price = 5)
# order_id product qty unit_price imputed revenue
# 1: 1 a 2 10 FALSE 20
# 2: 2 b 1 20 FALSE 20
# 3: 3 z 4 5 TRUE 20
names(orders) # the caller's table is untouched
# [1] "order_id" "product" "qty"
```
```hint Where := writes
`:=` changes the table it is called on, wherever that table came from. Think about which object each function is allowed to modify, and what `as.data.table()` returns when its input is already a data.table.
```
```hint Step order in fill_and_flag
The flag, the fill and the revenue all read `unit_price`. Work out which of them needs to see the original `NA` values.
```
### Constraints & Assumptions
- `orders` has columns `order_id`, `product` and `qty`. It may arrive as a data.frame or a data.table.
- `prices` has `product` and `unit_price`; `product_category` has `product` and `category`.
- Some ordered products can be missing from `prices` or from `product_category`.
- All three functions return data.tables, and `orders` must be identical to its original state after each call.
### Clarifying Questions to Ask
- Can `prices` or `product_category` list the same product more than once, and if so which row should win?
- Where should a group with a missing category (`NA`) appear in the sorted output of `revenue_by_category`?
- Can `unit_price` arrive as an integer column while `default_price` has decimals?
- Should the output of the first two functions keep the original row order of `orders`?
### What a Strong Answer Covers
- Uses the update-join form `x[i, col := i.col, on = ...]` so every order row is kept and unmatched rows get `NA`.
- Copies the input before the first `:=` and explains why `as.data.table()` alone does not protect a data.table argument.
- Orders the steps in `fill_and_flag` so `imputed` records the original missing prices and `revenue` uses the filled ones.
- Aggregates with `na.rm = TRUE`, sorts by `category`, and says how an `NA` category and an all-`NA` group come out.
- Names the risks in the lookup tables: duplicate keys and type coercion on assignment.
### Follow-up Questions
- Why use an update join (`orders[prices, unit_price := i.unit_price, on = "product"]`) instead of `merge()`?
- How do you guarantee the caller's `orders` is not mutated, and how would you test it?
- What happens if `prices` contains the same product twice, and how would you catch it?
- If each price were valid only for a date range, how would the join change?
Overview: Write three R functions that use data.table update joins to add unit prices, impute missing prices with a flag, and total revenue by category without changing the caller's table. It tests := by reference, copy(), join semantics and NA handling in grouped sums.