36. 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.Learn the concepts
- explode · 4 min read. Turn each array element into its own row, and the rows explode silently drops.
- posexplode · 3 min read. Explode an array and keep each element position, plus the _outer variant that keeps empty arrays.
- select · 6 min read. Choose, rename and compute columns, and why select beats a chain of withColumn calls.
PySpark functions you'll practise
- explode_outer
- select
Related problems
- Word Count · Medium · Arrays, Aggregations, Strings, Filtering & Selection
- Item Positions in a Basket (POSEXPLODE) · Medium · Arrays, Filtering & Selection
- Learning Path per Student · Medium · Arrays, Aggregations, Strings, Filtering & Selection
- Sorted Distinct Purchase List · Medium · Arrays, Aggregations
- Data Quality: Null Count per Column · Medium · Null Handling, Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice