Biggest Window Between Visits - Problem
Biggest Window Between Visits

You're given a table UserVisits containing user visit logs for a retail store. Your task is to find the largest gap between consecutive visits for each user.

Table Structure:
UserVisits
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| visit_date | date |
+-------------+------+

The Challenge:
For each user, calculate the maximum number of days between any two consecutive visits. If a user's last visit was before today (2021-1-1), include the gap from their last visit to today.

Key Points:
• The table may contain duplicate rows
• Consider today's date as '2021-1-1'
• Return results ordered by user_id
• Calculate the biggest window between consecutive visits

Input & Output

example_1.sql — Basic Example
$ Input: UserVisits: | user_id | visit_date | |---------|------------| | 1 | 2020-11-28 | | 1 | 2020-10-20 | | 1 | 2020-12-03 | | 2 | 2020-10-05 | | 2 | 2020-12-09 | | 3 | 2020-11-11 |
Output: | user_id | biggest_window | |---------|----------------| | 1 | 39 | | 2 | 65 | | 3 | 51 |
💡 Note: User 1: gaps are 39 days (Oct 20 to Nov 28), 5 days (Nov 28 to Dec 3), and 29 days (Dec 3 to today). Maximum is 39. User 2: gaps are 65 days (Oct 5 to Dec 9) and 23 days (Dec 9 to today). Maximum is 65. User 3: only one visit, so gap is 51 days (Nov 11 to today).
example_2.sql — With Duplicates
$ Input: UserVisits: | user_id | visit_date | |---------|------------| | 1 | 2020-01-01 | | 1 | 2020-01-01 | | 1 | 2020-06-01 | | 2 | 2020-12-31 |
Output: | user_id | biggest_window | |---------|----------------| | 1 | 214 | | 2 | 1 |
💡 Note: User 1 has duplicate visits on 2020-01-01 (removed), then gaps of 152 days (Jan 1 to Jun 1) and 214 days (Jun 1 to today). Maximum is 214. User 2 has only one visit with 1 day gap to today.
example_3.sql — Single Visit Users
$ Input: UserVisits: | user_id | visit_date | |---------|------------| | 1 | 2020-12-31 | | 2 | 2020-01-01 | | 3 | 2020-06-15 |
Output: | user_id | biggest_window | |---------|----------------| | 1 | 1 | | 2 | 366 | | 3 | 200 |
💡 Note: All users have only one visit each. The biggest window is the gap from their single visit to today (2021-01-01). User 1: 1 day, User 2: 366 days (leap year), User 3: 200 days.

Visualization

Tap to expand
Timeline for User 1Oct 20Nov 28Dec 3Today39 days5 days29 daysResult: MAX(39, 5, 29) = 39 daysUser 1's biggest window between visits is 39 daysTimeline for User 3 (Single Visit)Nov 11Today51 days
Understanding the Visualization
1
Collect Visit Data
Gather all customer visit dates, removing any duplicate entries
2
Sort Chronologically
Order visits by date for each customer to identify consecutive visits
3
Calculate Gaps
Measure days between each pair of consecutive visits
4
Include Today Gap
Add the gap from last visit to today's date (2021-01-01)
5
Find Maximum
Select the largest gap for each customer
Key Takeaway
🎯 Key Insight: Window functions like LAG() and LEAD() allow us to access adjacent rows efficiently, making consecutive difference calculations optimal with O(n log n) complexity instead of O(n²) nested queries.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

Sorting for window function, then single pass calculation

n
2n
Linearithmic
Space Complexity
O(n)

Storage for window function intermediate results

n
2n
Linearithmic Space

Constraints

  • 1 ≤ Number of rows ≤ 105
  • 1 ≤ user_id ≤ 1000
  • visit_date is between '2020-01-01' and '2020-12-31'
  • Today's date is fixed as '2021-01-01'
  • Table may contain duplicate rows
Asked in
Google 35 Amazon 42 Meta 28 Microsoft 31
28.4K Views
Medium Frequency
~18 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen