Skip to content

6. Sort Delivery Options

Difficulty: Easy · Topics: Filtering & Selection

options lists delivery options: carrier, days (estimated delivery days) and price. Some carriers have no estimate yet, so days is null.

Return all columns sorted by days from fastest to slowest, with unknown estimates last. Break ties by price (cheapest first), then by carrier name.

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

Sample data

options

carrierdaysprice
SpeedPost380
Rapid1150
NewConull40
Budget530
Swift360

Expected output

carrierdaysprice
Rapid1150
Swift360
SpeedPost380
Budget530
NewConull40

Hints

Hint 1Ascending order puts nulls first in Spark by default.
Hint 2F.col("days").asc_nulls_last() moves them to the end.
Hint 3Pass several sort keys to orderBy for the tie-breakers.

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