Skip to content

40. Fill Missing Dates (Calendar Spine)

Difficulty: Hard · Topics: Joins, Arrays, Aggregations, Dates, Null Handling, Filtering & Selection

daily_signups only has rows for days with at least one signup. For charts you need every day from the first to the last date in the table, with 0 on days without signups.

Return day and signups, ordered by day.

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

Sample data

daily_signups

daysignups
2024-01-015
2024-01-032
2024-01-064

Expected output

daysignups
2024-01-015
2024-01-020
2024-01-032
2024-01-040
2024-01-050
2024-01-064

Hints

Hint 1Find the first and last day with F.min/F.max.
Hint 2F.sequence(start_date, end_date) builds an array of every date in between; F.explode turns it into rows.
Hint 3Left-join the data onto this calendar and replace missing counts with 0.

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

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