Find the Start and End Number of Continuous Ranges - Problem

Find the Start and End Number of Continuous Ranges

You are given a database table Logs containing unique log IDs. Your task is to identify continuous ranges of log IDs and return the start and end points of each range.

Table Schema:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| log_id        | int     |
+---------------+---------+

log_id is the column of unique values for this table.

Problem: Write a SQL solution to find the start and end number of continuous ranges in the table. A continuous range is a sequence of consecutive integers (e.g., 1,2,3,4 or 7,8,9).

Output: Return the result table ordered by start_id. Each row should contain the starting and ending log_id of a continuous range.

Example: If you have log_ids [1,2,3,7,8,15], the continuous ranges are: [1,3], [7,8], and [15,15].

Input & Output

example_1.sql โ€” Basic Case
$ Input: Logs table: +--------+ | log_id | +--------+ | 1 | | 2 | | 3 | | 7 | | 8 | | 15 | +--------+
โ€บ Output: +----------+--------+ | start_id | end_id | +----------+--------+ | 1 | 3 | | 7 | 8 | | 15 | 15 | +----------+--------+
๐Ÿ’ก Note: The log_ids 1,2,3 form a continuous range [1,3]. The log_ids 7,8 form range [7,8]. The single log_id 15 forms range [15,15].
example_2.sql โ€” Single Elements
$ Input: Logs table: +--------+ | log_id | +--------+ | 1 | | 3 | | 5 | +--------+
โ€บ Output: +----------+--------+ | start_id | end_id | +----------+--------+ | 1 | 1 | | 3 | 3 | | 5 | 5 | +----------+--------+
๐Ÿ’ก Note: Each log_id is isolated, so each forms its own range where start_id equals end_id.
example_3.sql โ€” Large Continuous Range
$ Input: Logs table: +--------+ | log_id | +--------+ | 10 | | 11 | | 12 | | 13 | | 14 | | 20 | +--------+
โ€บ Output: +----------+--------+ | start_id | end_id | +----------+--------+ | 10 | 14 | | 20 | 20 | +----------+--------+
๐Ÿ’ก Note: Log_ids 10,11,12,13,14 form one large continuous range [10,14], while 20 forms a single-element range [20,20].

Visualization

Tap to expand
๐Ÿ“š Library Book Sequence FinderBooks Found: [7, 1, 3, 15, 2, 8]Step 1: Sort books by number1237815Step 2: Calculate book_number - positionPos 11-1=0Pos 22-2=0Pos 33-3=0Pos 47-4=3Pos 58-5=3Pos 615-6=9Step 3: Group by difference โ†’ Extract rangesGroup 0Range: [1, 3]Group 3Range: [7, 8]Group 9Range: [15, 15]SQL Solution:SELECT MIN(log_id) AS start_id, MAX(log_id) AS end_idFROM (SELECT log_id, log_id - ROW_NUMBER() OVER(ORDER BY log_id) grp๐ŸŽฏ Key Insight: Same difference = Same continuous sequence!
Understanding the Visualization
1
Sort Books
Arrange all book numbers in ascending order
2
Number Positions
Assign position numbers (1, 2, 3, ...) to each book
3
Find Pattern
Calculate book_number - position for each book
4
Group Sequences
Books with same difference value form continuous sequences
5
Extract Ranges
Find first and last book in each sequence group
Key Takeaway
๐ŸŽฏ Key Insight: The ROW_NUMBER() window function creates a perfect grouping mechanism - consecutive numbers will always have the same log_id - row_number difference!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

Each row is compared against every other row to find consecutive sequences

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Temporary space for join operations and result storage

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Number of rows โ‰ค 104
  • 1 โ‰ค log_id โ‰ค 107
  • All log_id values are unique
  • Result must be ordered by start_id
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 15
78.2K Views
Medium-High Frequency
~18 min Avg. Time
1.8K 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