Parse sales CSV text and compute grouped revenue summaries with R data.table

Quick Overview

Parse a sales CSV string with fread and use the data.table DT[i, j, by] form to total revenue by region, pick the top product with an alphabetical tie rule and summarize orders above a price threshold. It tests filtering in i, grouped aggregates with .N, deterministic ordering and edge cases.

Parse sales CSV text and compute grouped revenue summaries with R data.table

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

In `/app/solution.R` (with `library(data.table)` at the top), parse sales CSV text into a `data.table` and compute grouped aggregations using the `DT[i, j, by]` form. The CSV header is `region`, `product`, `quantity`, `price`. - `read_sales(text)` parses a length-1 character CSV string with `data.table::fread(text = ...)` and returns a data.table with columns `region, product, quantity, price`. - `revenue_by_region(dt)` returns one row per region with `region`, `total_revenue = sum(quantity * price)`, `n_orders = .N` and `avg_quantity = mean(quantity)`, ordered by `region` ascending. - `top_product_by_revenue(dt)` returns the length-1 character `product` with the greatest total revenue, breaking ties alphabetically ascending. - `high_value_summary(dt, min_price)` filters in `i` to `price >= min_price`, then returns one row per remaining region with `region`, `total_revenue = sum(quantity * price)` and `n = .N`, ordered by `region` ascending. For example: ```r text <- "region,product,quantity,price\nWest,A,2,10\nWest,B,1,50\nEast,A,3,10" dt <- read_sales(text) revenue_by_region(dt) # region total_revenue n_orders avg_quantity # 1: East 30 1 3.0 # 2: West 70 2 1.5 top_product_by_revenue(dt) # "A" (A and B both total 50, so the tie goes to A) high_value_summary(dt, 40) # one row: West, 50, 1 ``` ```hint Which slot does what In `DT[i, j, by]`, decide which slot holds the price filter, which computes the aggregates and which defines the groups, so the aggregation only sees qualifying rows. ``` ```hint Ties Sort the per-product totals on two columns at once so the tie rule comes out of the ordering. ``` ### Constraints & Assumptions - `data.table` is the only package loaded. - The CSV arrives as a single string whose first line is the header; `quantity` and `price` are numeric. - `n_orders` and `n` are integer counts, and `region` and `product` come back as character. ### What a Strong Answer Covers - How the CSV string is parsed and what column types result. - How the filter, the aggregates and the grouping map onto `i`, `j` and `by`, with `.N` for row counts. - How each result is ordered by region, and how the top product is chosen deterministically when revenues tie. - The edge cases: no rows passing the filter, an empty table, revenue ties with decimal prices and integer overflow in `quantity * price`. ### Follow-up Questions - Why is `fread` faster than `read.csv` on large files? - How would setting a key with `setkey()` affect the `by` aggregations? - How would you return the top product within each region instead of overall?

Overview: Parse a sales CSV string with fread and use the data.table DT[i, j, by] form to total revenue by region, pick the top product with an alphabetical tie rule and summarize orders above a price threshold. It tests filtering in i, grouped aggregates with .N, deterministic ordering and edge cases.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

In /app/solution.R (with library(data.table) at the top), parse sales CSV text into a data.table and compute grouped aggregations using the DT[i, j, by] form. The CSV header is region, product, quantity, price.

  • read_sales(text) parses a length-1 character CSV string with data.table::fread(text = ...) and returns a data.table with columns region, product, quantity, price .
  • revenue_by_region(dt) returns one row per region with region , total_revenue = sum(quantity * price) , n_orders = .N and avg_quantity = mean(quantity) , ordered by region ascending.
  • top_product_by_revenue(dt) returns the length-1 character product with the greatest total revenue, breaking ties alphabetically ascending.
  • high_value_summary(dt, min_price) filters in i to price >= min_price , then returns one row per remaining region with region , total_revenue = sum(quantity * price) and n = .N , ordered by region ascending.

For example:

text <- "region,product,quantity,price\nWest,A,2,10\nWest,B,1,50\nEast,A,3,10"
dt <- read_sales(text)

revenue_by_region(dt)
#    region total_revenue n_orders avg_quantity
# 1:   East            30        1          3.0
# 2:   West            70        2          1.5

top_product_by_revenue(dt)   # "A" (A and B both total 50, so the tie goes to A)
high_value_summary(dt, 40)   # one row: West, 50, 1

Constraints & Assumptions

  • data.table is the only package loaded.
  • The CSV arrives as a single string whose first line is the header; quantity and price are numeric.
  • n_orders and n are integer counts, and region and product come back as character.

What a Strong Answer Covers Guidance

  • How the CSV string is parsed and what column types result.
  • How the filter, the aggregates and the grouping map onto i , j and by , with .N for row counts.
  • How each result is ordered by region, and how the top product is chosen deterministically when revenues tie.
  • The edge cases: no rows passing the filter, an empty table, revenue ties with decimal prices and integer overflow in quantity * price .

Follow-up Questions Guidance

  • Why is fread faster than read.csv on large files?
  • How would setting a key with setkey() affect the by aggregations?
  • How would you return the top product within each region instead of overall?