KatInterview

Weekly Totals with 3-Week Rolling Sum in Pacific Time

SQL & DataAuto-graded · 19 test cases
MediumAuto-gradedSQL & Datatime zonesdate bucketingrolling window

Last updated

Bucket timestamped payments into Pacific-time Monday weeks, fill empty weeks with zero, and report weekly and trailing three-week totals.

You are given a list of payment events, each recorded as an absolute instant with a UTC offset. Produce a week-by-week report in America/Los_Angeles local time, containing each week's total and a trailing three-week total.

Input

  • records: a list of rows [user_id, event_ts, amount].
    • user_id is an integer. It plays no role in the computation.
    • event_ts is an ISO-8601 timestamp string that includes a UTC offset, for example "2025-08-05T18:30:00+00:00". A trailing Z also means UTC.
    • amount is a decimal value given as a string, such as "12.00".
  • range_start: a date string "YYYY-MM-DD", the first week of the report.
  • range_end: a date string "YYYY-MM-DD", the last date that a report week may start on.

Task

  1. Report weeks. The report contains one row per week start date: range_start, range_start + 7 days, range_start + 14 days, and so on, up to and including range_end. You may assume range_start is a Monday. If range_start is later than range_end, return an empty list.
  2. Assigning events to weeks. Convert each event_ts to Los Angeles local time (with correct daylight-saving handling) first. Take the local calendar date, then move back to that week's Monday (weeks run Monday through Sunday). That Monday is the event's week. An event is counted only if its Monday is one of the report weeks. Events in any other week are ignored.
  3. Weekly total. For every report week, week_total is the sum of the amounts of its events. A week with no events has total 0.
  4. Rolling total. For every report week, rolling_total is the sum of week_total for that week and the two report weeks directly before it. For the first or second report week, only the report weeks that exist are summed. Events before range_start never contribute.
  5. Only the event timestamp matters. Do not use any other notion of when a row arrived.

Output

Return a list with one row per report week, in ascending date order. Each row is [week_start, week_total, rolling_total], where:

  • week_start is the Monday as an ISO date string "YYYY-MM-DD",
  • week_total and rolling_total are numbers rounded to two decimal places.

Sum the amounts exactly as decimals, then round the totals to two decimals and return them as floats.

Constraints

  • 0 <= len(records) <= 10^5
  • Every event_ts includes a UTC offset.
  • Amounts are decimal strings with at most two fractional digits.
  • Events close to local midnight must land in the correct Los Angeles week, including across daylight-saving changes.
  • The number of report weeks is at most about 10^4.

Examples

Example 1

Input: rolling_weekly_totals([[1, '2025-08-04T16:00:00+00:00', '12.00'], [2, '2025-08-05T18:30:00+00:00', '7.50'], [1, '2025-08-12T20:10:00+00:00', '5.00'], [3, '2025-08-19T15:05:00+00:00', '9.00'], [2, '2025-08-20T02:45:00+00:00', '11.00'], [1, '2025-08-26T12:00:00+00:00', '3.00'], [2, '2025-09-02T21:10:00+00:00', '8.00'], [3, '2025-09-09T14:25:00+00:00', '10.00']], '2025-08-04', '2025-09-15')

Output: [['2025-08-04', 19.5, 19.5], ['2025-08-11', 5, 24.5], ['2025-08-18', 20, 44.5], ['2025-08-25', 3, 28], ['2025-09-01', 8, 31], ['2025-09-08', 10, 21], ['2025-09-15', 0, 18]]

Hints

Hint 1

Convert each timestamp to the Los Angeles zone before doing anything with its date. Taking the UTC date first puts late-Sunday local events in the wrong week.

Hint 2

Once you have the local date, subtracting its weekday index (Monday = 0) gives the Monday of its week. Use a dictionary keyed by Monday dates, pre-filled with zeros for every report week.

Hint 3

Because every report week is present in the dictionary, the rolling total for week i is just the sum of the totals at positions i-2, i-1 and i in the sorted list. Use Decimal to avoid float drift.