Skip to content

44. Item Positions in a Basket (POSEXPLODE)

Difficulty: Medium · Topics: Arrays, Filtering & Selection

Each row of baskets has an order_id and items, an array in the order the items were scanned.

Return one row per item: order_id, position (1 for the first item) and item. Orders with an empty or missing basket produce no rows.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

baskets

order_iditems
1[milk, bread, eggs]
2[tea]

Expected output

order_idpositionitem
11milk
12bread
13eggs
21tea

Hints

Hint 1F.explode loses the position; F.posexplode returns it as well (0-based).
Hint 2Name both outputs: F.posexplode("items").alias("pos", "item").
Hint 3Add 1 to make positions start at 1.

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