Skip to content

49. Pick the Join Strategy

Difficulty: Medium · Topics: Joins, Conditional Logic, Filtering & Selection

tables lists table sizes (name, size_mb). joins lists planned joins: join_id, left_table, right_table, join_type ('inner', 'left', 'right' or 'full').

Decide the strategy Spark would pick with a broadcast threshold of 10 MB:

Return join_id and strategy, sorted by join_id.

Row order matters for this problem. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

tables

namesize_mb
orders50000
customers8000
countries1
currencies0.5
promos9

joins

join_idleft_tableright_tablejoin_type
1orderscountriesinner
2orderscustomersleft
3countriesordersleft
4promosordersright
5orderscurrenciesfull
6currenciespromosinner

Expected output

join_idstrategy
1broadcast right
2sort-merge
3sort-merge
4broadcast left
5sort-merge
6broadcast left

Hints

Hint 1Join tables twice, once for each side; rename its columns first so the two copies do not clash.
Hint 2The rule for outer joins: the side whose unmatched rows must be kept cannot be broadcast. A full outer join cannot broadcast either side.
Hint 3Build the result with a when chain in the order of the rules.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · Learn · All problems