Problems

40 problems across three tracks. Filter by the trap you want to practise, or search.

0 of 40 solved Saved in this browser

Pick a trap

Debug the AIEasy

1. Revenue per customer is higher than the ledger

An AI-written PySpark join between orders and payments inflates revenue because some orders have several payment rows. Find the duplicated rows and fix the query.

Fan-out trapjoins · row duplication · aggregation

SQLEasy

2. Keep the latest record per customer

A change feed delivers several versions of each customer, with repeated rows, tied timestamps and missing timestamps. Write the Spark SQL that keeps exactly one current row per customer.

Tie trapNULL trapdeduplication · window functions · nulls

PySparkMedium

3. The incremental load that loses late events

A nightly incremental load filters on event time and silently skips events that arrive late. Work out with PySpark which run should load each event, exactly once.

Late-data trapBoundary trapincremental load · late-arriving data · watermark

SQLMedium

4. Build SCD2 history from repeated saves

Turn a table of customer saves into a slowly changing dimension (SCD type 2) with valid_from, valid_to and is_current, ignoring saves that changed nothing and handling NULL cities.

NULL trapscd2 · slowly changing dimension · window functions · nulls

Debug the AIMedium

5. The running balance that jumps ahead

An AI-written Spark SQL window query computes a running account balance, but transactions on the same date all show the end-of-day balance. Find the window frame bug and fix it.

Tie trapwindow functions · running total · window frame

SQLEasy

6. A data quality report that counts NULLs correctly

Write a Spark SQL data quality report with one row per rule and the number of failing rows. The rules cover NULLs, duplicates, date order and a reference lookup where NOT IN quietly returns nothing.

NULL trapDuplicate trapdata quality · nulls · not in · duplicates

PySparkMedium

7. Sessionize page views with a 30-minute gap

Group raw page views into user sessions with PySpark window functions. A session ends after more than 30 minutes of inactivity. Covers the exact boundary, long sessions and sessions across midnight.

Boundary trapTie trapsessionization · window functions · lag · gaps and islands

Debug the AIEasy

8. The union that swaps two columns

AI-written PySpark code combines two regional sales tables with union() and reports wrong net revenue. The tables have the same columns in a different order. Find the bug and fix it.

Schema trapunion · unionByName · schema · aggregation

SQLHard

9. Merge overlapping coverage periods

Merge overlapping and adjacent date ranges per user into continuous periods with Spark SQL. The classic gaps-and-islands problem, with nested periods that break the LAG-based solution.

Boundary trapgaps and islands · date ranges · window functions · running max

PySparkHard

10. Convert each transaction with the rate valid at the time

A point-in-time (as-of) join in PySpark. Convert foreign-currency transactions to INR using the exchange rate that was valid when each transaction happened, without losing or duplicating rows.

Fan-out trapBoundary trapas-of join · point-in-time join · range join · window functions

SQLEasy

11. Top three products per category, ties included

Return the three best-selling products in each category with Spark SQL, keeping every product that ties. Shows where ROW_NUMBER, RANK and DENSE_RANK give three different answers.

Tie traptop n per group · rank · dense_rank · row_number · window functions

SQLEasy

12. The average rating where NULL is not zero

Compute review counts and the average rating per product with Spark SQL when some reviews have no rating. Covers COUNT(*) versus COUNT(column), and why replacing NULL with 0 ruins an average.

NULL trapaggregation · count · avg · nulls · coalesce

SQLEasy

13. The LEFT JOIN that a WHERE clause quietly undoes

Report every customer with their completed March orders in Spark SQL, including customers with none. A filter on the joined table in WHERE turns the LEFT JOIN into an inner join and the zero rows disappear.

NULL trapleft join · on vs where · count · coalesce

Debug the AIMedium

14. The daily report that is five and a half hours off

AI-written PySpark code builds a daily activity report for India from UTC timestamps. The weekly total is right but every day is wrong. Find the time zone bug and fix it.

Boundary traptime zones · utc · ist · dates · daily aggregation

SQLMedium

15. Daily revenue with the missing days filled in

Produce one row per store per day of March with Spark SQL, showing 0 on days without sales. A GROUP BY can only return days that have data; the missing days have to come from a calendar table.

Boundary trapdate spine · calendar table · cross join · left join · gaps

SQLMedium

16. Month-over-month change when a month is missing

Compute month-over-month revenue change per plan with Spark SQL. LAG returns the previous row, not the previous month, so a plan with a missing month gets compared across the gap.

Boundary traplag · month over month · window functions · gaps · time series

PySparkMedium

17. Order totals that grow after explode

Summarise orders that carry a nested array of items with PySpark. After explode() each order is repeated once per item, so order-level values such as shipping fees get counted several times, and orders with no items vanish.

Fan-out trapexplode · arrays · nested data · grain · aggregation

Debug the AIMedium

18. The regions that sold nothing, according to the join

AI-written PySpark code compares regional sales with targets by joining on country and state. Regions whose state is NULL show zero sales, because NULL never equals NULL in a join. Find the bug and fix it.

NULL trapjoins · null keys · eqNullSafe · composite key

SQLMedium

19. Which tier was the customer in when they ordered?

Join orders to a type 2 slowly changing dimension with Spark SQL so that each order gets the customer tier that was valid on the order date. BETWEEN duplicates orders on change days, and forgetting the open-ended current row loses recent ones.

Boundary trapscd2 · slowly changing dimension · point-in-time join · range join · between

PySparkHard

20. Apply a batch of changes to an SCD2 table

Merge a batch of customer changes into a type 2 slowly changing dimension with PySpark. Close old versions, open new ones, skip saves that changed nothing, handle several changes for one customer, and ignore changes that arrive too late.

Late-data trapscd2 · slowly changing dimension · merge · upsert · late-arriving data

SQLEasy

21. Order statuses as columns, with zeros where there are none

Turn order rows into one row per store with a column per status in Spark SQL. Covers conditional aggregation, 0 versus NULL for empty groups, and statuses that are missing or unexpected.

NULL trappivot · conditional aggregation · case when · nulls

SQLEasy

22. The invoice numbers that sort like words

Invoice numbers are stored as text, some with leading zeros. Find each customer's latest invoice and count the paid ones in Spark SQL without being fooled by text comparison, where '9' is greater than '1000'.

Schema trapdata types · casting · string comparison · joins

SQLEasy

23. Conversion rates when the denominator is zero

Compute click and signup rates per campaign in Spark SQL when some campaigns have zero impressions, zero clicks or missing counts. A rate that cannot be computed must be NULL, not 0 and not an error.

NULL trapdivision by zero · nullif · rates · nulls

SQLMedium

24. Monthly active users are not the sum of the days

Compute monthly active users, total logins and peak daily active users per platform in Spark SQL. Distinct counts cannot be added up across days, which is how one user becomes twenty.

Grain trapdistinct count · active users · grain · aggregation

SQLMedium

25. Revenue by category when a product sits in several

Report revenue per category in Spark SQL when a product can belong to several categories. A plain join counts the same sale once per category, so the category totals add up to more than was sold.

Fan-out trapDuplicate trapmany-to-many · bridge table · allocation · joins

SQLMedium

26. Fill the gaps in sensor readings with the last known value

Forward-fill missing sensor readings per device in Spark SQL, carrying the last known value across gaps of any length. LAG only reaches back one row, so runs of NULLs stay NULL.

NULL trapforward fill · last non-null · window functions · time series

SQLMedium

27. Collapse double-clicks into one click

Remove accidental repeat clicks in Spark SQL by comparing each click with the previous one on the same button. Covers the exact 5-second boundary, chains of clicks and clicks recorded at the same instant.

Boundary trapTie trapdeduplication · lag · time window · window functions

SQLHard

28. The longest login streak

Find each user's longest run of consecutive login days in Spark SQL. A gaps-and-islands problem where several logins on one day, ties between streaks and streaks across a year end all break the usual shortcut.

Duplicate trapBoundary trapgaps and islands · streaks · row_number · window functions · deduplication

SQLHard

29. A three-step funnel, in order, within seven days

Build a view, add-to-cart, purchase funnel in Spark SQL where the steps must happen in order and within 7 days of the first view. Counting each step on its own counts purchases that came before the cart.

Boundary trapfunnel analysis · event sequences · range join · conversion

PySparkEasy

30. Everything except LOW, which quietly excludes the unknowns

Count open support tickets per team that are not low priority, with PySpark. Filtering with != also throws away every ticket whose priority is NULL, which are the ones nobody has looked at yet.

NULL trapfilter · not equal · three-valued logic · nulls

PySparkEasy

31. Amounts that arrive as text

Turn payment amounts typed as text ("₹1,299", "Rs. 300.50", "N/A") into decimals with PySpark. A plain cast fails the job in Spark 4 and silently returns NULL in older versions; neither tells you which rows were bad.

Schema trapdata cleaning · casting · try_cast · regexp_replace · data types

PySparkMedium

32. JSON payloads that changed shape

Extract user, amount and currency from JSON event payloads with PySpark when the producer changed the structure, sends numbers as strings and sometimes sends broken JSON. One fixed schema returns NULL for half the rows.

Schema trapNULL trapjson · schema drift · get_json_object · from_json · semi-structured data

PySparkMedium

33. Customers to date, counted once

Build a daily table of active, new and cumulative unique customers with PySpark. A running sum of daily distinct counts counts every returning customer again, and Spark has no distinct count over a window.

Grain trapcount distinct · running total · window functions · cohort · first seen

PySparkHard

34. Reconcile a target table against its source

Compare a loaded customer table with its source using PySpark and report missing, extra, duplicated and mismatched rows. Row counts can match while the content does not, and a plain != never flags a value that turned into NULL.

NULL trapDuplicate trapreconciliation · data validation · full outer join · null-safe comparison · eqNullSafe

PySparkHard

35. One golden record from three systems

Merge customer records from CRM, billing and support into one row per customer with PySpark, taking each field from the most trusted record that actually has it. Keeping the single best row throws away values the other records had.

NULL trapmaster data · survivorship · deduplication · window functions · first ignore nulls

Debug the AIEasy

36. The bucket no order ever lands in

AI-written PySpark code sorts orders into SMALL, MEDIUM and LARGE with a when() chain, and the report shows no large orders at all. Find the branch-order bug and fix it without breaking the boundaries.

Boundary trapwhen otherwise · case when · bucketing · conditional logic

Debug the AIEasy

37. The city average that no order adds up to

An AI-written Spark SQL query reports the average order value per city by averaging the per-store averages. The order counts are right and the averages are not. Find the bug and fix it.

Grain trapaggregation · average of averages · weighted average · group by

Debug the AIMedium

38. The month that ends a day early

An AI-written Spark SQL query totals March payments per day with BETWEEN on a timestamp column. Every day is right except the last one. Find the date range bug and fix it.

Boundary trapbetween · timestamps · date range · half-open interval · month end

Debug the AIMedium

39. Seven rows are not seven days

AI-written PySpark code computes rolling 7-day revenue per store with rowsBetween(-6, 0). Stores that are closed on some days get a window that reaches back much further than a week. Find the frame bug and fix it.

Boundary trapwindow functions · rolling sum · rowsBetween · rangeBetween · missing days

Debug the AIMedium

40. The load that never updates anything

AI-written PySpark code applies a batch of orders to a target table with a left anti join. New orders arrive, the row count is right, and every changed order keeps its old status. Find the bug and write the load properly.

Late-data trapDuplicate trapupsert · merge · incremental load · left anti join · deduplication