KatInterview

Compute Per-Group Running Revenue Share with Reset

SQL & DataAuto-graded · 15 test cases
HardAuto-gradedSQL & Datawindow functionsrunning totalsaggregation

Last updated

Tests window-style running totals and per-group aggregation by computing a cumulative percentage-of-group-total that resets for each new group, given pre-sorted row data.

Running Revenue Share Within Groups

You are given a table of sales transactions represented as a list of rows. Each row is a dict with three keys:

  • "group" — a string identifying the sales region (e.g. "North", "South")
  • "order" — a non-negative integer giving the position of this row within its group (unique per group, but not necessarily contiguous or starting at 0)
  • "revenue" — a non-negative integer

Your task is to simulate a window function that computes, for every row, the cumulative revenue share of that row within its group.

Concretely, for each group independently:

  1. Sort the rows by "order" ascending.
  2. Compute group_total = sum of all "revenue" values in the group.
  3. For the i-th row (0-indexed after sorting), compute the running sum of revenue through and including row i (i.e., rows 0 … i).
  4. The cumulative share for that row = running_sum / group_total, rounded to 4 decimal places.
    • Special case: if group_total == 0, every row in that group gets a cumulative share of 0.0.

Return a new list of dicts, one per input row, each containing exactly the keys:

  • "group" — same as input
  • "order" — same as input
  • "revenue" — same as input
  • "cumulative_share" — the float value described above (rounded to 4 decimal places)

The output list must be sorted first by "group" ascending (lexicographic), then by "order" ascending within each group.

Constraints

  • 0 <= len(rows) <= 2000
  • Each "group" value is a non-empty string containing only uppercase and lowercase ASCII letters.
  • Within a single group, all "order" values are distinct non-negative integers (0 <= order <= 10^6).
  • 0 <= revenue <= 10^6
  • The input list may arrive in any order; do not assume it is pre-sorted.
  • Group names are case-sensitive ("north" and "North" are different groups).
  • If rows is empty, return [].

Examples

Example 1

Input: running_revenue_share([{'group': 'North', 'order': 1, 'revenue': 100}, {'group': 'North', 'order': 2, 'revenue': 200}, {'group': 'North', 'order': 3, 'revenue': 200}, {'group': 'South', 'order': 10, 'revenue': 400}, {'group': 'South', 'order': 20, 'revenue': 100}])

Output: [{'group': 'North', 'order': 1, 'revenue': 100, 'cumulative_share': 0.2}, {'group': 'North', 'order': 2, 'revenue': 200, 'cumulative_share': 0.6}, {'group': 'North', 'order': 3, 'revenue': 200, 'cumulative_share': 1}, {'group': 'South', 'order': 10, 'revenue': 400, 'cumulative_share': 0.8}, {'group': 'South', 'order': 20, 'revenue': 100, 'cumulative_share': 1}]

Example 2

Input: running_revenue_share([{'group': 'A', 'order': 5, 'revenue': 0}, {'group': 'A', 'order': 3, 'revenue': 0}, {'group': 'A', 'order': 8, 'revenue': 0}])

Output: [{'group': 'A', 'order': 3, 'revenue': 0, 'cumulative_share': 0}, {'group': 'A', 'order': 5, 'revenue': 0, 'cumulative_share': 0}, {'group': 'A', 'order': 8, 'revenue': 0, 'cumulative_share': 0}]

Example 3

Input: running_revenue_share([{'group': 'West', 'order': 0, 'revenue': 1000}])

Output: [{'group': 'West', 'order': 0, 'revenue': 1000, 'cumulative_share': 1}]

Hints

Hint 1

First, bucket the rows by group name using a dict, then sort each bucket by 'order' — this mirrors how SQL partitions and orders a window frame.

Hint 2

Compute the group total in one pass, then iterate again keeping a running sum; divide running_sum by group_total at each step (handle the zero-total edge case separately).

Hint 3

Collect all output dicts into one list and sort by (group, order) at the end; round with Python's built-in round(x, 4) to get exactly 4 decimal places.