Optimize Small DataFrame Join with Broadcast

PYSPARK coding challenge · Difficulty: easy · Topic: Broadcast Join Optimisation · +50 XP

Problem

DataFrames

----------

transactions (large):

+----------------+---------+

| Column         | Type    |

+----------------+---------+

| transaction_id | INT     |
| product_id     | INT     |
| quantity       | INT     |
| price          | DOUBLE  |

+----------------+---------+

products (small, ~100 rows):

+------------+---------+

| Column     | Type    |

+------------+---------+

| product_id | INT     |
| name       | VARCHAR |
| category   | VARCHAR |

+------------+---------+

Problem

-------

Join transactions with the small products

lookup table.

Use broadcast join to avoid shuffle of

the large transactions DataFrame.

Return: transaction_id, product_name,

category, total_value

(quantity × price)

Order: transaction_id ASC

Example Input

-------------

transactions:

+----+------------+----------+-------+

| id | product_id | quantity | price |

+----+------------+----------+-------+

|  1 |    101     |    3     | 25.00 |
|  2 |    102     |    1     | 99.99 |
|  3 |    101     |    2     | 25.00 |

+----+------------+----------+-------+

products:

+------------+----------+----------+

| product_id | name     | category |

+------------+----------+----------+

|    101     | Widget A | Gadgets  |
|    102     | Pro Pen  | Office   |

+------------+----------+----------+

Expected Output

+----+----------+----------+-------+

| id | name     | category | total |

+----+----------+----------+-------+

|  1 | Widget A | Gadgets  | 75.00 |
|  2 | Pro Pen  | Office   | 99.99 |
|  3 | Widget A | Gadgets  | 50.00 |

+----+----------+----------+-------+

What this PYSPARK challenge teaches you

“Optimize Small DataFrame Join with Broadcast” is a easy-level PYSPARK challenge focused on Broadcast Join Optimisation. Working through it gives you hands-on practice with broadcast, join, optimization — 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

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:

  1. Your result should return: transaction_id, product_name,.
  2. Compare your output to the Expected Output — the columns, values and row order must match exactly.
  3. from pyspark.sql.functions import broadcast df = transactions.join( broadcast(products), "product_id" ) ## Example Input - `customers` | customer_id | region_code | | --- | --- | | 1001 | NA | | 1002 | EU | | 1003 | NA | | 1004 | APAC | | 1005 | EU | | 1006 | NA | ## Example Input - `regions` | region_code | region_name | | --- | --- | | NA | North America | | EU | Europe | | APAC | Asia Pacific | ## Expected Output | customer_id | region_name | | --- | --- | | 1001 | North America | | 1002 | Europe | | 1003 | North America | | 1004 | Asia Pacific | | 1005 | Europe | | 1006 | North America | ## Notes - The DataFrame is created for you - do not recreate it - Build a DataFrame called `df_result` and finish with `df_result.show()`

Where this comes up

Variations of this problem have been reported in interviews at Databricks, Spotify, Twitter. Interviewers use 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

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 Broadcast Join Optimisation.

Solve this challenge free on PySpark.in