Consecutive Transactions with Increasing Amounts - Problem

Imagine you're a financial analyst tracking customer spending patterns to identify potential high-value customers. You need to find customers who demonstrate consistent growth in their spending over consecutive days.

Problem: Given a Transactions table with customer transactions, find customers who have made transactions with increasing amounts for at least 3 consecutive days.

Table Schema:

Transactions
+------------------+------+
| Column Name | Type |
+------------------+------+
| transaction_id | int |
| customer_id | int |
| transaction_date | date |
| amount | int |
+------------------+------+

Key Rules:

  • Each (customer_id, transaction_date) combination is unique
  • Consecutive means consecutive calendar days
  • Amounts must be strictly increasing
  • Minimum 3 consecutive days required

Output: Return customer_id, start_date, and end_date of each consecutive period, ordered by customer_id, start_date, end_date.

Input & Output

example_1.sql โ€” Basic Consecutive Increasing
$ Input: Transactions table: | transaction_id | customer_id | transaction_date | amount | |---------------|-------------|------------------|--------| | 1 | 101 | 2023-05-01 | 100 | | 2 | 101 | 2023-05-02 | 150 | | 3 | 101 | 2023-05-03 | 200 | | 4 | 102 | 2023-05-01 | 50 | | 5 | 102 | 2023-05-02 | 25 |
โ€บ Output: | customer_id | consecutive_start | consecutive_end | |-------------|-------------------|------------------| | 101 | 2023-05-01 | 2023-05-03 |
๐Ÿ’ก Note: Customer 101 has consecutive transactions on 05-01 ($100), 05-02 ($150), 05-03 ($200) with increasing amounts. Customer 102 doesn't qualify as amounts decrease from $50 to $25.
example_2.sql โ€” Multiple Consecutive Periods
$ Input: Transactions table: | transaction_id | customer_id | transaction_date | amount | |---------------|-------------|------------------|--------| | 1 | 201 | 2023-06-01 | 100 | | 2 | 201 | 2023-06-02 | 120 | | 3 | 201 | 2023-06-03 | 140 | | 4 | 201 | 2023-06-05 | 200 | | 5 | 201 | 2023-06-06 | 250 | | 6 | 201 | 2023-06-07 | 300 |
โ€บ Output: | customer_id | consecutive_start | consecutive_end | |-------------|-------------------|------------------| | 201 | 2023-06-01 | 2023-06-03 | | 201 | 2023-06-05 | 2023-06-07 |
๐Ÿ’ก Note: Customer 201 has two separate consecutive increasing periods: 06-01 to 06-03 (3 days) and 06-05 to 06-07 (3 days), with a gap on 06-04.
example_3.sql โ€” Edge Case: Exactly 3 Days
$ Input: Transactions table: | transaction_id | customer_id | transaction_date | amount | |---------------|-------------|------------------|--------| | 1 | 301 | 2023-07-01 | 80 | | 2 | 301 | 2023-07-02 | 90 | | 3 | 301 | 2023-07-03 | 100 | | 4 | 301 | 2023-07-04 | 95 | | 5 | 302 | 2023-07-01 | 100 | | 6 | 302 | 2023-07-02 | 110 |
โ€บ Output: | customer_id | consecutive_start | consecutive_end | |-------------|-------------------|------------------| | 301 | 2023-07-01 | 2023-07-03 |
๐Ÿ’ก Note: Customer 301 qualifies with exactly 3 consecutive increasing days, then amount drops on 07-04. Customer 302 only has 2 consecutive days, which doesn't meet the minimum requirement of 3.

Visualization

Tap to expand
Customer Spending Growth Pattern DetectionCustomer Transaction TimelineDay 1$10005-01Day 2$15005-02+$50 โ†—Day 3$20005-03+$50 โ†—Day 5$18005-05Gap + Decrease3-Day Consecutive Increasing Pattern โœ“Algorithm Steps VisualizationStep 1-2LAG() WindowCompare withPrevious DayStep 3Mark StreakStart Pointsis_streak_startStep 4Running SumAssign StreakGroup IDsStep 5Filter GroupsLength โ‰ฅ 3HAVING COUNTSQL Window Function Logic1. LAG(transaction_date) and LAG(amount) โ†’ Get previous day's data2. Check: DATEDIFF(current_date, prev_date) = 1 AND current_amount > prev_amount3. Mark streak breaks: When gap > 1 day OR amount doesn't increase4. GROUP BY streak_id HAVING COUNT(*) >= 3 โ†’ Filter valid patternsResult: Customer 101consecutive_start: 2023-05-01, consecutive_end: 2023-05-03
Understanding the Visualization
1
Sort by Timeline
Arrange all transactions chronologically for each customer
2
Compare with Yesterday
For each transaction, check if it's consecutive and amount increased from previous day
3
Track Streak Boundaries
Mark where increasing streaks start and end
4
Group Consecutive Days
Assign unique IDs to each streak of consecutive increasing days
5
Filter Valid Patterns
Keep only streaks with 3 or more consecutive increasing days
Key Takeaway
๐ŸŽฏ Key Insight: Use LAG() window function to compare consecutive days, then employ running sum technique to group consecutive increasing streaks efficiently in a single SQL pass.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single sort by customer and date, then linear pass with window functions

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for final result set and window function intermediate calculations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Number of transactions โ‰ค 104
  • 1 โ‰ค customer_id โ‰ค 1000
  • 1 โ‰ค amount โ‰ค 106
  • Each (customer_id, transaction_date) pair is unique
  • transaction_date is a valid date in format YYYY-MM-DD
  • Amounts must be strictly increasing (not equal)
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
42.0K Views
Medium-High Frequency
~25 min Avg. Time
1.9K 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