Skip to content

12. Label Late Shipments

Difficulty: Easy · Topics: Dates, Null Handling, Conditional Logic

shipments has order_id, ordered_on and shipped_on (both 'YYYY-MM-DD'; shipped_on is null if not shipped yet).

Add two columns and return everything:

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

Sample data

shipments

order_idordered_onshipped_on
12025-03-012025-03-03
22025-03-012025-03-09
32025-03-02null
42025-03-102025-03-15

Expected output

order_idordered_onshipped_ondays_to_shipstatus
12025-03-012025-03-032on time
22025-03-012025-03-098late
32025-03-02nullnullnot shipped
42025-03-102025-03-155on time

Hints

Hint 1F.datediff(end, start) returns whole days; it is null when either date is null.
Hint 2Check the null case first in your when chain: a null comparison is not true, so it would fall through to otherwise.
Hint 3Exactly 5 days is still on time.

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

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