Python data engineering interview problem. Difficulty: intermediate. Pattern: Dictionaries. About 16 minutes. Part of the Pro drill bank.
Count events per user per day and per type from out-of-order data that contains replayed events. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Implement daily_counts(events: list[dict]) -> list[dict]. Each event is {"event_id", "user", "ts", "type"} where ts is an ISO timestamp like "2024-05-01T10:00:00". Events arrive in any order, and the same event_id can appear more than once (a replay): count it once. Return one record per (user, date): {"user": ..., "date": "YYYY-MM-DD", "counts": {type: number, ...}}. Sort records by user, then date. Calling the function twice on the same input must give the same output, and the input list must not change.
Input: daily_counts([{"event_id": "e2", "user": "u1", "ts": "2024-05-02T09:00:00", "type": "view"}, {"event_id": "e1", "user": "u1", "ts": "2024-05-01T10:00:00", "type": "click"}, {"event_id": "e1", "user": "u1", "ts": "2024-05-01T10:00:00", "type": "click"}]) Output: [{'user': 'u1', 'date': '2024-05-01', 'counts': {'click': 1}}, {'user': 'u1', 'date': '2024-05-02', 'counts': {'view': 1}}] The second e1 is a replay and is ignored; the two days are reported in date order even though the input was not sorted.
Topics: lakebench, python, dedupe, group by, idempotent.
More Python interview questions · All interview problems · Learn data engineering
Interview-style drill: Count events per user per day and per type from out-of-order data that contains replayed events.
Implement `daily_counts(events: list[dict]) -> list[dict]`. Each event is `{"event_id", "user", "ts", "type"}` where `ts` is an ISO timestamp like `"2024-05-01T10:00:00"`. Events arrive in any order, and the same `event_id` can appear more than once (a replay): count it once. Return one record per `(user, date)`: `{"user": ..., "date": "YYYY-MM-DD", "counts": {type: number, ...}}`. Sort records by user, then date. Calling the function twice on the same input must give the same output, and the input list must not change.