45. Keep Users with No Interests (EXPLODE_OUTER)
Difficulty: Medium · Topics: Arrays, Filtering & Selection
users has user_id and interests (an array of strings, possibly empty or null).
Return one row per (user_id, interest). Users without interests must still appear once, with interest null.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
users
| user_id | interests |
|---|---|
| 1 | [spark, sql] |
| 2 | [] |
| 3 | [ml] |
Expected output
| user_id | interest |
|---|---|
| 1 | spark |
| 1 | sql |
| 2 | null |
| 3 | ml |
Hints
Hint 1
F.explode silently drops rows whose array is empty or null.Hint 2
F.explode_outer keeps them and emits a single null.Hint 3
Alias the exploded column tointerest.PySpark functions you'll practise
- explode_outer
- select
Related problems
- Word Count · Medium · Arrays
- Fill Missing Dates (Calendar Spine) · Hard · Joins
- Item Positions in a Basket (POSEXPLODE) · Medium · Arrays
- Filter High Earners · Easy · Filtering & Selection
- Distinct User Actions · Easy · Filtering & Selection
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