Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Table: Stadium
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | visit_date | date | | people | int | +---------------+---------+ visit_date is the column with unique values for this table. Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit. As the id increases, the date increases as well.
Write a solution to display the records with three or more rows with consecutive id's, and the number of people is greater than or equal to 100 for each.
Return the result table ordered by visit_date in ascending order.
The result format is in the following example.
Example 1:
Input: Stadium table: +------+------------+-----------+ | id | visit_date | people | +------+------------+-----------+ | 1 | 2017-01-01 | 10 | | 2 | 2017-01-02 | 109 | | 3 | 2017-01-03 | 150 | | 4 | 2017-01-04 | 99 | | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-09 | 188 | +------+------------+-----------+ Output: +------+------------+-----------+ | id | visit_date | people | +------+------------+-----------+ | 5 | 2017-01-05 | 145 | | 6 | 2017-01-06 | 1455 | | 7 | 2017-01-07 | 199 | | 8 | 2017-01-09 | 188 | +------+------------+-----------+ Explanation: The four rows with ids 5, 6, 7, and 8 have consecutive ids and each of them has >= 100 people attended. Note that row 8 was included even though the visit_date was not the next day after row 7. The rows with ids 2 and 3 are not included because we need at least three consecutive ids.
Problem summary: Table: Stadium +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | visit_date | date | | people | int | +---------------+---------+ visit_date is the column with unique values for this table. Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit. As the id increases, the date increases as well. Write a solution to display the records with three or more rows with consecutive id's, and the number of people is greater than or equal to 100 for each. Return the result table ordered by visit_date in ascending order. The result format is in the following example.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers": {"Stadium": ["id", "visit_date", "people"]}, "rows": {"Stadium": [[1, "2017-01-01", 10], [2, "2017-01-02", 109], [3, "2017-01-03", 150], [4, "2017-01-04", 99], [5, "2017-01-05", 145], [6, "2017-01-06", 1455], [7, "2017-01-07", 199], [8, "2017-01-09", 188]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #601: Human Traffic of Stadium
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #601: Human Traffic of Stadium
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #601: Human Traffic of Stadium
// # Write your MySQL query statement below
// WITH
// S AS (
// SELECT
// *,
// id - (ROW_NUMBER() OVER (ORDER BY id)) AS rk
// FROM Stadium
// WHERE people >= 100
// ),
// T AS (SELECT *, COUNT(1) OVER (PARTITION BY rk) AS cnt FROM S)
// SELECT id, visit_date, people
// FROM T
// WHERE cnt >= 3
// ORDER BY 1;
// "#
// }
// Accepted solution for LeetCode #601: Human Traffic of Stadium
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #601: Human Traffic of Stadium
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #601: Human Traffic of Stadium
// # Write your MySQL query statement below
// WITH
// S AS (
// SELECT
// *,
// id - (ROW_NUMBER() OVER (ORDER BY id)) AS rk
// FROM Stadium
// WHERE people >= 100
// ),
// T AS (SELECT *, COUNT(1) OVER (PARTITION BY rk) AS cnt FROM S)
// SELECT id, visit_date, people
// FROM T
// WHERE cnt >= 3
// ORDER BY 1;
// "#
// }
# Accepted solution for LeetCode #601: Human Traffic of Stadium
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #601: Human Traffic of Stadium
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #601: Human Traffic of Stadium
# # Write your MySQL query statement below
# WITH
# S AS (
# SELECT
# *,
# id - (ROW_NUMBER() OVER (ORDER BY id)) AS rk
# FROM Stadium
# WHERE people >= 100
# ),
# T AS (SELECT *, COUNT(1) OVER (PARTITION BY rk) AS cnt FROM S)
# SELECT id, visit_date, people
# FROM T
# WHERE cnt >= 3
# ORDER BY 1;
# "#
# }
// Accepted solution for LeetCode #601: Human Traffic of Stadium
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #601: Human Traffic of Stadium
# Write your MySQL query statement below
WITH
S AS (
SELECT
*,
id - (ROW_NUMBER() OVER (ORDER BY id)) AS rk
FROM Stadium
WHERE people >= 100
),
T AS (SELECT *, COUNT(1) OVER (PARTITION BY rk) AS cnt FROM S)
SELECT id, visit_date, people
FROM T
WHERE cnt >= 3
ORDER BY 1;
"#
}
// Accepted solution for LeetCode #601: Human Traffic of Stadium
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #601: Human Traffic of Stadium
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #601: Human Traffic of Stadium
// # Write your MySQL query statement below
// WITH
// S AS (
// SELECT
// *,
// id - (ROW_NUMBER() OVER (ORDER BY id)) AS rk
// FROM Stadium
// WHERE people >= 100
// ),
// T AS (SELECT *, COUNT(1) OVER (PARTITION BY rk) AS cnt FROM S)
// SELECT id, visit_date, people
// FROM T
// WHERE cnt >= 3
// ORDER BY 1;
// "#
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.