KatInterview

Seven-Day Spend Grid per Shopper Using Only String Dates

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashinghashingaggregationstring-dates

Last updated

Aggregate (shopper, date, amount) records into per-shopper totals for a 7-day window, using hand-written ISO date stepping and hashing instead of date libraries.

Seven-Day Spend Grid per Shopper

You receive a list of purchase records. Each record is a list [shopper_id, date, amount], where date is an ISO string "YYYY-MM-DD" (zero-padded). You also receive a window given by window_start and window_end, both ISO date strings. The window is inclusive and covers exactly 7 consecutive calendar days.

Build a table with one row per shopper. Each row has the form:

[shopper_id, t0, t1, t2, t3, t4, t5, t6]

where ti is the total amount over all of that shopper's records dated exactly window_start + i days.

Rules

  • Records may come in any order. Several records may share the same shopper and date; add all of them together. Do not treat them as duplicates to discard.
  • A day with no records for a shopper contributes 0.
  • Records dated outside the window are ignored.
  • Only shoppers with at least one record inside the window appear in the output. A shopper whose records all fall outside the window is omitted.
  • Output rows are sorted in ascending order by shopper_id (natural ordering of the ids; within one input all ids have the same type).
  • Amounts may be negative (refunds) and are summed as given.
  • You may not use any date/time library. You need to work out the 7 consecutive dates yourself, including month ends, year ends, and leap years (Gregorian rule: divisible by 4, except centuries, unless divisible by 400).
  • If there are no records inside the window, return an empty list.

Constraints

  • 0 <= len(records) <= 10^5
  • window_start and window_end are valid ISO dates and window_end is exactly 6 days after window_start.
  • Every record's date is a valid zero-padded YYYY-MM-DD string.
  • shopper_id is an int or a string; amount is a number.
  • The window may cross a month boundary, a year boundary, or contain February 29.

Examples

Example 1

Input: weekly_spend_table([[1, '2024-02-01', 10], [1, '2024-02-03', 5], [2, '2024-02-02', 7], [1, '2024-02-01', 2]], '2024-02-01', '2024-02-07')

Output: [[1, 12, 0, 5, 0, 0, 0, 0], [2, 0, 7, 0, 0, 0, 0, 0]]

Example 2

Input: weekly_spend_table([['s2', '2023-12-31', 4], ['s1', '2024-01-01', 3], ['s2', '2024-01-01', 6]], '2023-12-31', '2024-01-06')

Output: [['s1', 0, 3, 0, 0, 0, 0, 0], ['s2', 4, 6, 0, 0, 0, 0, 0]]

Hints

Hint 1

ISO dates with zero padding compare in the same order as chronological order, so you never need to parse a date to compare two of them.

Hint 2

Generate the seven date strings once by writing a small 'next day' function (days in month, leap year, month/year rollover), then map each date string to its column index 0..6 in a dictionary.

Hint 3

Keep a dictionary from shopper_id to a list of seven zeros, created the first time you see that shopper in the window, add each amount at its column, and sort the keys at the end.