For each region count how many different products were sold, as product_count. Repeat purchases of the same product count once and NULL products are not counted. Ignore orders with a NULL region. Row order does not matter.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 1 | North | Laptop | 1 | 55000.0 | 2024-01-05 |
| 2 | 2 | South | Phone | 2 | 30000.0 | 2024-01-18 |
| 3 | 1 | North | Mouse | 3 | 1500.0 | 2024-02-02 |
| 4 | 3 | West | Laptop | 1 | 55000.0 | 2024-02-15 |
| 5 | 4 | South | Phone | 1 | 15000.0 | 2024-03-01 |
| 6 | 2 | West | Mouse | 2 | 1000.0 | 2024-03-09 |
| 7 | 1 | North | Phone | 1 | 30000.0 | 2024-03-20 |
| region | product_count |
|---|---|
| North | 3 |
| South | 1 |
| West | 2 |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 11 | 5 | East | Tablet | 1 | 20000.0 | 2024-04-12 |
| 12 | 5 | East | Tablet | 1 | 20000.0 | 2024-04-14 |
| 13 | 6 | East | Desk | 1 | 12000.0 | 2024-05-02 |
| 14 | 7 | Central | Desk | 2 | 24000.0 | 2024-05-30 |
| region | product_count |
|---|---|
| East | 2 |
| Central | 1 |
Return exactly region, product_count.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
{{ totalCount }} questions · {{ shHardPct }}% Hard. Real PySpark DataFrame and Spark SQL interview questions. Write PySpark in your browser on TechTown’s Spark engine, run sample tests, then submit against hidden tests.
Guests can solve {{ freeLimit }} problems for free. Log in or sign up (free) to unlock everything, keep your streak and appear on the leaderboard. New to a topic? Learn it first in the Spark course.
{{ p.statement }}
{{ p.constraints }}
No submissions yet. Press Submit to judge your code against all tests.
The model solution and step-by-step explanation unlock when you solve this problem or after 3 unsuccessful submissions ({{ shFailsLabel }}). Struggling? Try the hints on the Question tab first.
{{ p.solution }}
Write real PySpark: from pyspark.sql import functions as F, Window, spark.sql(...). It runs in your browser on a lightweight Spark engine, and the same code runs unchanged on Databricks or any Spark cluster. Use df.show() or print() to debug.
Create your free TechTown account to unlock every problem, track your progress, bookmark questions and keep preparing for interviews.