Skip to content

49. Late Data in the Raw Zone

Difficulty: Medium · Topics: Data Lake, Aggregations, Dates

The raw zone stores events in folders by the day they arrived. raw_events has event_id, event_ts ('YYYY-MM-DD HH:MM:SS', when the event happened, may be null) and ingest_date ('YYYY-MM-DD', the folder it landed in).

An event is late when its event date is more than 1 day before its ingest_date. For each ingest_date return events (all rows), late_events, oldest_event_date ('YYYY-MM-DD', the earliest event date in that folder) and reprocess_days: how many different event dates the late events belong to, which is how many downstream daily partitions must be rebuilt. Sort by ingest_date.

Row order matters for this problem. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

raw_events

event_idevent_tsingest_date
12025-03-05 10:00:002025-03-05
22025-03-04 23:50:002025-03-05
32025-03-02 08:00:002025-03-05
42025-03-01 12:00:002025-03-05
52025-03-02 09:30:002025-03-05
62025-03-06 01:00:002025-03-06
7null2025-03-06

Expected output

ingest_dateeventslate_eventsoldest_event_datereprocess_days
2025-03-05532025-03-012
2025-03-06202025-03-060

Hints

Hint 1F.to_date("event_ts") gives the event date; F.datediff(F.to_date("ingest_date"), event_date) gives how many days late it is.
Hint 2Count late rows with F.sum(F.when(late, 1).otherwise(0)); a null event date is never late.
Hint 3F.countDistinct(F.when(late, event_date)) counts distinct dates among late rows only, because the when gives null for the others.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Spark Performance

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · Learn · All problems