Salting a Skewed Join: Customer Bucket Load Report (PySpark)
PYSPARK coding challenge · Difficulty: hard · Topic: Performance Optimization · +150 XP
Topics: Data Skew · Salting · Shuffle Joins · Data Replication · Aggregation
The Production Incident
It is Monday morning. You are a Data Engineer at ShopStream, a large e-commerce platform. Your team processes millions of customer orders a day with Apache Spark, and a critical customer analytics pipeline has suddenly become very slow.
The pipeline joins two datasets on customer_id:
orders: a large transaction dataset with millions of orders.customers: a small reference dataset with customer information.
What went wrong?
The Spark UI shows that one enterprise customer, MegaMart (customer_id = 999), generates most of the orders. In this simulated incident the distribution looks like this:
| Customer | Number of orders | |---|---| | Asha (101) | 1,000 | | Bilal (102) | 2,000 | | Chen (103) | 5,000 | | MegaMart (999) | 500,000 |
In a shuffle join, rows that share a join key go to the same shuffle partition. One task receives MegaMart's 500,000 rows while the other tasks finish early and sit idle. This is data skew. The overloaded task becomes the bottleneck for the whole Spark stage, and it may run out of memory.
Your mission
Reduce the work concentrated on the hot customer key with a technique called salting: spread the orders over several salted join keys, keep every order matched to the right customer, and produce a load report that proves how the data is distributed.
For this exercise, assume a shuffle join is required (no broadcast join).
Your Tasks
Task 1: Salt the large orders dataset
Add a column salt to orders with four buckets: salt = order_id % 4, so the valid salt values are 0, 1, 2, 3.
| order_id | customer_id | salt | |---|---|---| | 1 | 999 | 1 | | 2 | 999 | 2 | | 3 | 999 | 3 | | 4 | 999 | 0 | | 5 | 999 | 1 |
Instead of one hot join key 999, MegaMart's orders now carry the keys (999, 0), (999, 1), (999, 2) and (999, 3).
Task 2: Replicate the small customers dataset
Create one copy of every customer for each salt value. MegaMart becomes:
| customer_id | customer_name | salt | |---|---|---| | 999 | MegaMart | 0 | | 999 | MegaMart | 1 | | 999 | MegaMart | 2 | | 999 | MegaMart | 3 |
Why is this necessary? Without replication, an order with the key (999, 2) has no customer row with salt 2 to match. The replicated customer rows let every salted order find its customer.
Task 3: Perform the salted join
Join the salted orders to the replicated customers on both customer_id and salt. Keep every customer–salt combination, including those with no matching orders: start from the replicated customers and use a left join.
Task 4: Build the bucket load report
Return one row per customer per salt bucket with:
order_count: the number of orders in the bucket.total_amount: the totalamountof those orders.
An empty bucket shows 0 for both values, never null. With this report an engineer can check that a heavily skewed customer's orders really are spread over several salted keys.
Input DataFrames
customers (small)
| Column | Data type | |---|---| | customer_id | Integer | | customer_name | String |
orders (large, skewed)
| Column | Data type | |---|---| | order_id | Integer | | customer_id | Integer | | amount | Integer |
Both DataFrames are already available in your PySpark environment.
Example 1
Input: customers
| customer_id | customer_name | | --- | --- | | 101 | Asha | | 102 | Bilal | | 104 | Divya | | 999 | MegaMart |
Input: orders
| order_id | customer_id | amount | | --- | --- | --- | | 1 | 999 | 120 | | 2 | 999 | 80 | | 3 | 999 | 200 | | 4 | 999 | 50 | | 5 | 999 | 75 | | 6 | 999 | 300 | | 7 | 999 | 60 | | 8 | 999 | 90 | | 9 | 999 | 110 | | 10 | 999 | 40 | | 11 | 999 | 150 | | 12 | 999 | 25 | | 13 | 101 | 500 | | 15 | 102 | 70 | | 17 | 101 | 250 | | 18 | 101 | 100 | | 20 | 102 | 30 | | 21 | 555 | 999 |
Expected output:
`
+-----------+-------------+----+-----------+------------+
|customer_id|customer_name|salt|order_count|total_amount|
+-----------+-------------+----+-----------+------------+
| 101| Asha| 0| 0| 0| | 101| Asha| 1| 2| 750| | 101| Asha| 2| 1| 100| | 101| Asha| 3| 0| 0| | 102| Bilal| 0| 1| 30| | 102| Bilal| 1| 0| 0| | 102| Bilal| 2| 0| 0| | 102| Bilal| 3| 1| 70| | 104| Divya| 0| 0| 0| | 104| Divya| 1| 0| 0| | 104| Divya| 2| 0| 0| | 104| Divya| 3| 0| 0| | 999| MegaMart| 0| 3| 165| | 999| MegaMart| 1| 3| 305| | 999| MegaMart| 2| 3| 420| | 999| MegaMart| 3| 3| 410|
+-----------+-------------+----+-----------+------------+
`
Understanding Your Result
MegaMart has 12 orders. Before salting, all 12 share one join key. After salting:
| Salt | Matching order IDs | Order count | |---|---|---| | 0 | 4, 8, 12 | 3 | | 1 | 1, 5, 9 | 3 | | 2 | 2, 6, 10 | 3 | | 3 | 3, 7, 11 | 3 |
The distribution is 3 / 3 / 3 / 3. This example happens to be perfectly even; in real workloads neither deterministic nor random salting guarantees equal buckets. The point is that the salted keys can go to different shuffle partitions, so no single task has to process every MegaMart order.
The other edge cases in the example:
- Asha (101) has orders in only two buckets (1 and 2); buckets 0 and 3 are zero.
- Bilal (102) has orders in buckets 0 and 3 only.
- Divya (104) has no orders, but she still appears four times, all zeros.
- Customer 555 is not in
customers, so order 21 is ignored.
Constraints
- Exactly four salts:
0, 1, 2, 3, withsalt = order_id % 4. - Replicate every customer across all four salts, and join on
customer_idandsalt. customer_idis unique incustomers;order_idis unique and positive.amountis non-negative and may be 0: an order with amount 0 still counts inorder_count.- Empty buckets show
0, not null. Orders of unknown customers are excluded. - Columns:
customer_id,customer_name,salt,order_count,total_amount, sorted bycustomer_idascending, thensaltascending. customers,orders,spark,F(pyspark.sql.functions) andWindoware ready. Do not recreate the DataFrames and do not hard-code the example output.- Your code also runs on hidden datasets with other customers, other hot keys and other order ids, so it must compute everything from
customersandorders. The row order is graded. - Assign the result to
df_resultand finish withdf_result.show().
Production Engineering Note
Manual salting is not always the best fix. If the small side fits in memory, a broadcast join avoids the skewed shuffle completely, and Spark's Adaptive Query Execution (spark.sql.adaptive.skewJoin.enabled) can split some skewed join partitions automatically. Check those first in a real pipeline.
This challenge is about how manual salting works: why the small side must be replicated, why the join needs both keys, and how to check the resulting distribution. The goal is not just the right numbers, but understanding why spreading a hot join key lets Spark process it in parallel.
What this PYSPARK challenge teaches you
“Salting a Skewed Join: Customer Bucket Load Report (PySpark)” is a hard-level PYSPARK challenge focused on Performance Optimization. Working through it gives you hands-on practice with Salting, Data skew, Skewed join, explode, Performance — 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
- Salting
- Data skew
- Skewed join
- explode
- Performance
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:
- Step 1: add the salt to the big table: orders.withColumn('salt', F.col('order_id') % 4).
- Step 2: replicate the small table once per salt: customers.withColumn('salt', F.explode(F.array([F.lit(s) for s in range(4)]))).
- Step 3: join on BOTH keys, ['customer_id', 'salt'], and keep the replicated customers side (left join) so empty buckets survive.
- Count F.count('order_id') (not count('*')), wrap the sum in F.coalesce(..., F.lit(0)), then orderBy('customer_id', 'salt').
Interview practice
This problem is good practice for data engineering interviews at companies such as Accenture, Deloitte, IBM. Interviewers use problems like it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
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
- Handle Data Skew from High-Frequency Vehicles (PySpark)
- Amazon Logistics: Rescue a Slow Shipment Validation Pipeline (PySpark)
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
- Parse Apache Logs with Regex
- Optimize Small DataFrame Join with Broadcast
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 hard and covers Performance Optimization.