Customers who rented in two consecutive weeks (SQL)
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's technical phone screen, last asked Sep 2025. It tests sql, dates and window functions.
A table records rental purchases. Find the customers who bought in two consecutive weeks of 2024, and among those week pairs, return the customer with the most copies rented in the first week of the pair. Write a single SQL query.
Interface
Unless you agree on a different interface with the interviewer, assume:
- Table
purchases(customer_id INT, purchase_date DATE, rented_copies INT). - Only purchases dated in 2024 count. A week runs Monday to Sunday and is identified by its Monday (
week_start_date). - Two weeks are consecutive when their Mondays are exactly 7 days apart, and the customer bought at least once in each.
- For every such pair, take the customer's total
rented_copiesin the first week. Returncustomer_id,week_start_dateandtotal_rented_copiesfor the highest total. Return every row tied for the highest.
Example
customer_id purchase_date rented_copies
1 2024-01-02 2
1 2024-01-04 3
1 2024-01-10 1
2 2024-03-04 6
2 2024-03-18 2 # two weeks apart: not consecutive
3 2024-05-06 4
3 2024-05-13 1
3 2024-05-20 7
4 2024-02-12 5
4 2024-02-19 1
5 2023-12-27 9 # outside 2024
5 2024-01-03 1
customer_id week_start_date total_rented_copies
1 2024-01-01 5 # 2 + 3, followed by the week of 2024-01-08
4 2024-02-12 5 # tie, so both rows
Customer 3's pairs give first-week totals 4 and 1. Customer 3's 7 copies fall in a week with no following week.
Clarifying questions
- Which week convention: ISO weeks starting Monday?
- The week of 2024-12-30 runs into 2025. Does it count, and do its 2025 days count?
- Ties: return all, or one?
- Is it the total in the first week, or across both weeks?
What a strong answer covers
- A first CTE that aggregates purchases to one row per customer and week, using
DATE_TRUNC('week', purchase_date)or an equivalent. -
LEAD(week_start) OVER (PARTITION BY customer_id ORDER BY week_start)to find the next active week, and a filter on a 7-day difference. - Ties kept with
RANK()or a comparison to theMAX, not withLIMIT 1. - Filtering to 2024 before aggregating, with a half-open date range that can use an index.
- Edge cases: weeks that span the year boundary, several purchases on one day, customers with only one week.
Follow-ups
- Find customers active in
nconsecutive weeks (a gaps-and-islands problem). - Return the longest streak of consecutive active weeks per customer.
- Use calendar weeks that start on Sunday instead.
Where it's asked
- Meta · Technical phone screen · Last asked Sep 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted
What it tests
sql · dates · window functions · data engineering
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
