Compute each customer's total_amount (sum of amount; ignore NULL amounts and customers whose total is NULL). Sort customers by total_amount descending, then customer_id ascending. cum_share = running total of total_amount in that order divided by the grand total, rounded to 4 decimals. is_core is true when the running total BEFORE this customer is less than 80% of the grand total (i.e. the customer is needed to reach 80%). Sort the output in that same order.
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 101 | N | A | 1 | 5000.0 | 2024-01-01 |
| 2 | 102 | N | A | 1 | 3000.0 | 2024-01-02 |
| 3 | 103 | N | A | 1 | 1500.0 | 2024-01-03 |
| 4 | 104 | N | A | 1 | 500.0 | 2024-01-04 |
| customer_id | total_amount | cum_share | is_core |
|---|---|---|---|
| 101 | 5000 | 0.5 | True |
| 102 | 3000 | 0.8 | True |
| 103 | 1500 | 0.95 | False |
| 104 | 500 | 1 | False |
| order_id | customer_id | region | product | qty | amount | order_date |
|---|---|---|---|---|---|---|
| 1 | 1 | S | A | 1 | 600.0 | 2024-01-01 |
| 2 | 2 | S | A | 1 | 200.0 | 2024-01-01 |
| 3 | 2 | S | A | 1 | 200.0 | 2024-01-02 |
| customer_id | total_amount | cum_share | is_core |
|---|---|---|---|
| 1 | 600 | 0.6 | True |
| 2 | 400 | 1 | True |
Return columns customer_id, total_amount, cum_share, is_core, sorted by total_amount desc, customer_id asc.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Month-over-Month Product Rank Change · Extract the Email Domain ›
{{ 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.