Top Customer per Region

Medium
Joins
Aggregation
Window Functions

Top Customer per Region

The customers table stores each customer's region, and orders stores their purchases.

Return region, customer_id, customer_name, total_spend for the highest-spending customer in each region. Break ties by the smaller customer_id. Sort by region ascending.

Browser Engine: Evaluates PySpark DataFrame & SQL logic locally in Pyodide WebAssembly.

Input data

2 DataFrames

These DataFrames are passed to solve(spark, dfs). Access one with dfs['name'].

df.customers5 rows
customer_id (int)customer_name (string)region (string)
1MayaNorth
2LeoNorth
3NinaSouth
4OmarSouth
5SaraWest
df.orders6 rows
order_id (int)customer_id (int)amount (double)
11100
2280
3130
4360
5490
6540

Return these columns

region(string)
customer_id(int)
customer_name(string)
total_spend(double)

Column names and order must match. Sort order is specified in the question.

Browser engine
Loading Python editor…
Click Run to try the sample cases, or Submit to check the hidden cases. First run caches the browser Python engine (~8–15s once); all code stays on your device.