Skip to content

29. Consecutive Status Runs (Gaps and Islands)

Difficulty: Hard · Topics: Window Functions, Aggregations

server_status has exactly one row per server per day with that day's status ('up' or 'down').

Collapse consecutive days with the same status into runs. Return server, status, start_day, end_day and days (length of the run).

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

server_status

serverdaystatus
web12024-01-01up
web12024-01-02up
web12024-01-03down
web12024-01-04up
web12024-01-05up
db12024-01-01down
db12024-01-02down

Expected output

serverstatusstart_dayend_daydays
web1up2024-01-012024-01-022
web1down2024-01-032024-01-031
web1up2024-01-042024-01-052
db1down2024-01-012024-01-022

Hints

Hint 1Number each server's days in order, and separately number days within each (server, status).
Hint 2Inside one run both numbers go up together, so their difference is constant, and it changes when a run ends.
Hint 3Group by server, status and that difference; take min/max day and the count.

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