Skip to content

48. How Much Does Each Query Read?

Difficulty: Medium · Topics: Joins, Aggregations, Null Handling, Filtering & Selection

partitions describes a date-partitioned table: partition_date and size_gb. queries lists filters run against it: query_id, start_date and end_date (inclusive).

With partition pruning, a query only reads partitions inside its date range. For every query return query_id, partitions_read, gb_read and pct_of_table (share of the table's total size, rounded to 1 decimal). Queries that match no partition read 0 partitions and 0 GB. Sort by query_id.

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

Sample data

partitions

partition_datesize_gb
2025-03-0140
2025-03-0235
2025-03-0350
2025-03-0445
2025-03-0530

queries

query_idstart_dateend_date
12025-03-022025-03-03
22025-03-052025-03-05
32025-01-012025-12-31
42025-04-012025-04-30

Expected output

query_idpartitions_readgb_readpct_of_table
128542.5
213015
35200100
4000

Hints

Hint 1Join each query to the partitions in its range with a condition: partition_date.between(start_date, end_date). ISO date strings compare correctly.
Hint 2Use a left join so queries matching nothing are kept, then fill their counts with 0.
Hint 3The table total is one more aggregation, cross-joined in.

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