Create a region × quarter revenue matrix for a single calendar year of orders. Return one row per region (excluding NULL regions and NULL dates) with columns Q1, Q2, Q3, Q4 holding the summed amount; a quarter with no sales must show 0. Always output all four quarter columns. 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 | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|
| North | 86500 | 0 | 0 | 0 |
| South | 45000 | 0 | 0 | 0 |
| West | 56000 | 0 | 0 | 0 |
| 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 | Q1 | Q2 | Q3 | Q4 |
|---|---|---|---|---|
| East | 0 | 52000 | 0 | 0 |
| Central | 0 | 24000 | 0 | 0 |
Return exactly region, Q1, Q2, Q3, Q4.
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.