Walmart: Black Friday Sales War Room — Find the Products Driving Revenue (PySpark)
PYSPARK coding challenge · Difficulty: medium · Topic: GroupBy Aggregations · +100 XP
Topics: filter · withColumn · groupBy / agg · countDistinct · conditional counts · share of total with a window · sorting
*A fictional Walmart-inspired retail analytics case study, not a verified Walmart interview question.*
The Production Incident
It's 8:30 AM on Black Friday. Orders are pouring in from stores across the United States, and the retail analytics team has to put a sales-performance dashboard in front of the regional managers before the 10 AM stand-up.
The managers want four answers for every product:
1. How much revenue did it generate?
2. How many units sold, and what share of total revenue does it bring in?
3. How many high-value orders (over $1,000) did it receive?
4. How many different stores sold it?
Yesterday's dry run went badly. The raw feed contains cancelled and returned orders, orders with zero quantity and orders with missing prices. The old job simply multiplied quantity by price and added everything up, so returned laptops counted as revenue and the dashboard overstated sales.
Your mission: build a reliable PySpark pipeline that cleans the data, calculates revenue, identifies high-value orders and produces a product-level performance report.
Input Data
You receive a DataFrame named orders (all amounts in US dollars):
| Column | Data type | |---|---| | order_id | Integer | | store | String | | product | String | | quantity | Integer (may be NULL, 0 or negative) | | unit_price | Integer (may be NULL, 0 or negative) | | status | String (may be NULL) |
Some feeds also carry an order_date column. You don't need it.
Your Tasks
Task 1: Explore and clean
Inspect the data first with printSchema(), count() and show(). Then keep only valid sales, which meet all three rules:
statusis exactlyCompleted(case-sensitive:completed,Pending,Cancelled,Returnedand NULL are not valid);quantityis greater than 0;unit_priceis not NULL and greater than 0.
Do not replace missing prices with 0: an order without a price is excluded, not counted as free.
Task 2: Derive the revenue
Add total_sales = quantity × unit_price to every valid order with withColumn(). Order 101: 2 × 1200 = 2,400.
Task 3: Find the high-value orders
Create df_high_value with filter(): the valid orders with total_sales strictly greater than 1,000.
| Order value | High-value? | |---|---| | 2,400 | Yes | | 1,200 | Yes | | 1,000 | No | | 125 | No |
Task 4: Build the product performance report
Group the valid sales by product:
| Column | Meaning | |---|---| | product | Product name | | total_quantity | Sum of the valid quantities | | total_sales | Revenue of **all** valid orders (not only the high-value ones) | | revenue_share_pct | The product's share of the total valid revenue of all products, as a percentage rounded to 1 decimal place | | high_value_orders | Number of valid **orders** over 1,000 (orders, not units; 0 if none) | | store_count | Number of **distinct** stores with a valid sale of the product |
A product with no valid sale does not appear at all. The share's denominator is the revenue of valid orders only, so the shares of the products in the report add up to 100 (give or take rounding).
Task 5: Sort and show
Assign the report to df_result with exactly the columns product, total_quantity, total_sales, revenue_share_pct, high_value_orders, store_count, sorted by total_sales descending, then product ascending to break ties. Finish with df_result.show().
Example 1
Input: orders
| order_id | store | product | quantity | unit_price | status | | --- | --- | --- | --- | --- | --- | | 101 | Austin | Laptop | 2 | 1200 | Completed | | 102 | Dallas | Mouse | 5 | 25 | Completed | | 103 | Seattle | Keyboard | 3 | 75 | Completed | | 104 | Austin | Monitor | 2 | 350 | Completed | | 105 | Dallas | Laptop | 1 | 1200 | Completed | | 106 | Seattle | Mouse | 4 | 25 | Completed | | 107 | Austin | Tablet | 2 | 500 | Completed | | 108 | Dallas | Monitor | 3 | 350 | Cancelled | | 109 | Austin | Laptop | 1 | 1300 | Returned | | 110 | Seattle | Keyboard | 2 | 75 | Completed | | 111 | Dallas | Mouse | 0 | 25 | Completed | | 112 | Austin | Headphones | 1 | NULL | Completed | | 113 | Dallas | Tablet | 3 | 500 | Completed | | 114 | Seattle | Monitor | 4 | 350 | Completed | | 115 | Seattle | Laptop | 1 | 1100 | Completed |
Expected output:
`
+--------+--------------+-----------+-----------------+-----------------+-----------+
| product|total_quantity|total_sales|revenue_share_pct|high_value_orders|store_count|
+--------+--------------+-----------+-----------------+-----------------+-----------+
| Laptop| 4| 4700| 47.5| 3| 3| | Tablet| 5| 2500| 25.3| 1| 2| | Monitor| 6| 2100| 21.2| 1| 2| |Keyboard| 5| 375| 3.8| 0| 1| | Mouse| 9| 225| 2.3| 0| 2|
+--------+--------------+-----------+-----------------+-----------------+-----------+
`
Understanding Your Result
- Laptop (orders 101, 105, 115): 2,400 + 1,200 + 1,100 = 4,700, which is 4,700 / 9,900 = 47.5% of the valid revenue. All three orders are over 1,000. Returned order 109 is excluded. Sold in Austin, Dallas and Seattle:
store_count3. - Tablet (107, 113): 1,000 + 1,500 = 2,500 (25.3%). Only 113 is high-value: exactly 1,000 is not.
- Monitor (104, 114): 700 + 1,400 = 2,100 (21.2%). Cancelled order 108 is excluded.
- Keyboard (103, 110): 225 + 150 = 375 (3.8%). Both orders come from Seattle, so
store_countis 1. - Mouse (102, 106): 125 + 100 = 225 (2.3%). Order 111 has quantity 0. The most units (9), the least revenue.
- Headphones: order 112 has no price, so the product has no valid sale and is not in the report.
- The rounded shares add up to 100.1: each one is rounded on its own.
Constraints
orders,sparkandF(pyspark.sql.functions) are ready. Do not recreateordersand do not hard-code the result.- Your code also runs on hidden datasets with other products, stores, statuses and boundary values, in any order.
- Use DataFrame operations: no Python UDFs, Pandas or
.collect(). - The grader reads the last table you show, so finish with
df_result.show().
Interview Bonus
1. Why must invalid orders be filtered before aggregating?
2. Why can a product with fewer units generate more revenue?
3. What is the difference between count() and countDistinct()?
4. Why must total_sales > 1000 be evaluated per order, not after aggregating by product?
5. How would Spark execute these aggregations on 100 million rows?
What this PYSPARK challenge teaches you
“Walmart: Black Friday Sales War Room — Find the Products Driving Revenue (PySpark)” is a medium-level PYSPARK challenge focused on GroupBy Aggregations. Working through it gives you hands-on practice with filter, groupBy / agg, countDistinct, Conditional aggregation, Data cleaning — 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
- filter
- groupBy / agg
- countDistinct
- Conditional aggregation
- Data cleaning
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:
- Filter first: orders.filter((F.col('status') == 'Completed') & (F.col('quantity') > 0) & F.col('unit_price').isNotNull() & (F.col('unit_price') > 0)). The comparison with 'Completed' is case-sensitive.
- Add the revenue per ORDER: .withColumn('total_sales', F.col('quantity') * F.col('unit_price')). df_high_value is that DataFrame filtered on total_sales > 1000 (strictly greater).
- In one groupBy('product').agg(...): F.sum('quantity'), F.sum('total_sales'), F.sum(F.when(F.col('total_sales') > 1000, 1).otherwise(0)) for the high-value order count, and F.countDistinct('store').
- For the share, divide by the grand total: F.sum('total_sales').over(Window.partitionBy()) is the total of every row in the report. revenue_share_pct = F.round(F.col('total_sales') / grand_total * 100, 1).
- Sort with .orderBy(F.col('total_sales').desc(), F.col('product').asc()) and select the six columns in order before df_result.show().
Interview practice
This problem is good practice for data engineering interviews at companies such as Walmart. 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
- Calculate Total Sales and Average Order Value by Customer
- Analyze Sales Data to Find Top Performers by Region
- Count Rows per City
- Analyze Product Sales by Region (PySpark)
- Paytm: Find Double-Charged Payments and the Refund Amount (PySpark)
- Fix the Broken Pipeline
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 medium and covers GroupBy Aggregations.