Skip to content

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_idinterests
1[spark, sql]
2[]
3[ml]

Expected output

user_idinterest
1spark
1sql
2null
3ml

Hints

Hint 1F.explode silently drops rows whose array is empty or null.
Hint 2F.explode_outer keeps them and emits a single null.
Hint 3Alias the exploded column to interest.

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 · All problems