Bucket order values

Pandas data engineering interview problem. Difficulty: beginner. Pattern: Aggregation. About 12 minutes. Free to practice.

Bin order amounts into Low, Mid, and High brackets and summarize order counts and total revenue per bucket. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Given orders with columns order_id and amount: Categorize each order's amount into three price brackets: Low: (0, 50] Mid: (50, 200] High: (200, inf) Aggregate the results to return: bucket: category name (Low, Mid, High) order_count: count of orders in that bucket total_revenue: sum of order amounts in that bucket Keep all 3 buckets even if a bucket has zero orders. Sort by bucket in definition order (Low, Mid, High) and reset the index. Assign the final DataFrame to result.

Requirements

  • Preserve all three categorical tiers.
  • Columns must be bucket, order_count, total_revenue.

Constraints

  • amount > 0.
  • order_id is unique.

Examples

Input: orders order_id | amount 101 | 25.0 102 | 75.0 103 | 45.0 104 | 150.0 105 | 250.0 106 | 500.0 107 | 80.0 Output: bucket | order_count | total_revenue Low | 2 | 70 Mid | 3 | 305 High | 2 | 750 Orders are distributed into brackets and summed by volume and revenue.

Topics: lakebench, pandas, cut, groupby.

More interview problems · All interview problems · Learn data engineering

beginner

Bucket order values

Interview-style drill: Bin order amounts into Low, Mid, and High brackets and summarize order counts and total revenue per bucket.

Given `orders` with columns `order_id` and `amount`: Categorize each order's `amount` into three price brackets: - `Low`: `(0, 50]` - `Mid`: `(50, 200]` - `High`: `(200, inf)` Aggregate the results to return: - `bucket`: category name (`Low`, `Mid`, `High`) - `order_count`: count of orders in that bucket - `total_revenue`: sum of order amounts in that bucket Keep all 3 buckets even if a bucket has zero orders. Sort by bucket in definition order (`Low`, `Mid`, `High`) and reset the index. Assign the final DataFrame to `result`.