Chunk transactions into daily batches by cutoff time
Python data engineering interview problem. Difficulty: intermediate. Pattern: Date Functions. About 15 minutes. Part of the Pro drill bank.
Transactions have timestamps. Finance wants them bucketed into days using a cutoff hour so a 1am sale still belongs to the previous business day if the cutoff is 4am.
A bank closes its books at a cutoff hour. A transaction made before the cutoff on a day belongs to the previous business day's batch. Group transaction ids by business day. For each row, read its timestamp. If the hour is before cutoff_hour, the business day is the day before; otherwise it is the same day. Return a dictionary from the day (YYYY-MM-DD) to the list of ids, with days in the order they first appear. Keep ids in input order. Write bucket_by_cutoff(rows, cutoff_hour). With cutoff 6, a transaction at 2026-08-10T09:00 belongs to 2026-08-10, and one at 2026-08-10T03:00 belongs to the previous day, 2026-08-09. The result is {'2026-08-10': ['a'], '2026-08-09': ['b']}. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Parse each ts as a timestamp.
- If the hour is before the cutoff, use the previous day.
- Group the ids by business day.
- Keep the first-seen order of days and the input order of ids.
Constraints
- rows is a list of dictionaries with ts (ISO text) and id.
- cutoff_hour is a whole number from 0 to 23.
Examples
Input: bucket_by_cutoff([{'ts': '2026-08-10T09:00:00', 'id': 'a'}, {'ts': '2026-08-10T03:00:00', 'id': 'b'}], 6) Output: {'2026-08-10': ['a'], '2026-08-09': ['b']} This input follows the stated rules and produces this output.
Topics: lakebench, python, data-transformation.
More Python interview questions · All interview problems · Learn data engineering
Chunk transactions into daily batches by cutoff time
Interview-style drill: Group timestamped transactions into business days using a non-midnight cutoff time.
Chunk transactions into daily batches by cutoff time.