Explode Order Items Array into Individual Rows
PYSPARK coding challenge · Difficulty: easy · Topic: Explode Nested JSON Arrays · +50 XP
An e-commerce platform stores each order with a nested array of items; every item is a struct with item_id and quantity. Flatten the orders so that each output row is one item of one order.
Return a DataFrame df_result with columns order_id, item_id, quantity, ordered by order_id and then item_id, and call df_result.show(). Orders whose items array is empty or NULL have nothing to ship and must not appear in the result.
Example Input
`
orders DataFrame (already defined -- do not redefine it):
order_id | items
1 | [{A, 2}, {B, 1}]
2 | [{C, 5}]
3 | []
4 | [{A, 1}, {D, 3}, {B, 2}]
5 | NULL
`
Expected Output
`
+--------+-------+--------+
|order_id|item_id|quantity|
+--------+-------+--------+
| 1| A| 2| | 1| B| 1| | 2| C| 5| | 4| A| 1| | 4| B| 2| | 4| D| 3|
+--------+-------+--------+
`
What this PYSPARK challenge teaches you
“Explode Order Items Array into Individual Rows” is a easy-level PYSPARK challenge focused on Explode Nested JSON Arrays. Working through it gives you hands-on practice with explode, array, struct, nested data — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- explode
- array
- struct
- nested data
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- explode("items") produces one row per array element.
- Each element is a struct: read its fields with col("item.item_id") and col("item.quantity").
- explode drops rows whose array is empty or NULL -- which is exactly what this problem wants (explode_outer would keep them).
- Select only order_id, item_id and quantity, then orderBy("order_id", "item_id").
How to practise it on PySpark.in
Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYSPARK challenges
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
- Parse Apache Logs with Regex
- Optimize Small DataFrame Join with Broadcast
- Optimize Average Rating Calculation for Products
- Deduplicate and Aggregate User Actions with Latest Session
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Explode Nested JSON Arrays.