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
| carrier | days | price |
|---|---|---|
| SpeedPost | 3 | 80 |
| Rapid | 1 | 150 |
| NewCo | null | 40 |
| Budget | 5 | 30 |
| Swift | 3 | 60 |
Expected output
| carrier | days | price |
|---|---|---|
| Rapid | 1 | 150 |
| Swift | 3 | 60 |
| SpeedPost | 3 | 80 |
| Budget | 5 | 30 |
| NewCo | null | 40 |
Hints
Hint 1
Ascending order puts nulls first in Spark by default.Hint 2
F.col("days").asc_nulls_last() moves them to the end.Hint 3
Pass several sort keys toorderBy for the tie-breakers.Learn the concepts
- orderBy · 3 min read. Sort rows ascending or descending, control where nulls go, and know what a global sort costs.
PySpark functions you'll practise
- orderBy
Related problems
- Filter High Earners · Easy · Filtering & Selection
- Find Partitions That Need Compaction · Easy · Aggregations
- Word Count · Medium · Arrays
- Latest Order per Customer · Medium · Window Functions
- Top Two Salary Levels per Department · Medium · Window Functions
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