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.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: Accounts
+-------------+------+ | Column Name | Type | +-------------+------+ | account_id | int | | income | int | +-------------+------+ account_id is the primary key (column with unique values) for this table. Each row contains information about the monthly income for one bank account.
Write a solution to calculate the number of bank accounts for each salary category. The salary categories are:
"Low Salary": All the salaries strictly less than $20000."Average Salary": All the salaries in the inclusive range [$20000, $50000]."High Salary": All the salaries strictly greater than $50000.The result table must contain all three categories. If there are no accounts in a category, return 0.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Accounts table: +------------+--------+ | account_id | income | +------------+--------+ | 3 | 108939 | | 2 | 12747 | | 8 | 87709 | | 6 | 91796 | +------------+--------+ Output: +----------------+----------------+ | category | accounts_count | +----------------+----------------+ | Low Salary | 1 | | Average Salary | 0 | | High Salary | 3 | +----------------+----------------+ Explanation: Low Salary: Account 2. Average Salary: No accounts. High Salary: Accounts 3, 6, and 8.
Problem summary: Table: Accounts +-------------+------+ | Column Name | Type | +-------------+------+ | account_id | int | | income | int | +-------------+------+ account_id is the primary key (column with unique values) for this table. Each row contains information about the monthly income for one bank account. Write a solution to calculate the number of bank accounts for each salary category. The salary categories are: "Low Salary": All the salaries strictly less than $20000. "Average Salary": All the salaries in the inclusive range [$20000, $50000]. "High Salary": All the salaries strictly greater than $50000. The result table must contain all three categories. If there are no accounts in a category, return 0. Return the result table in any 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":{"Accounts":["account_id","income"]},"rows":{"Accounts":[[3,108939],[2,12747],[8,87709],[6,91796]]}}create-a-session-bar-chart)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1907: Count Salary Categories
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1907: Count Salary Categories
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1907: Count Salary Categories
// # Write your MySQL query statement below
// WITH
// S AS (
// SELECT 'Low Salary' AS category
// UNION
// SELECT 'Average Salary'
// UNION
// SELECT 'High Salary'
// ),
// T AS (
// SELECT
// CASE
// WHEN income < 20000 THEN "Low Salary"
// WHEN income > 50000 THEN 'High Salary'
// ELSE 'Average Salary'
// END AS category,
// COUNT(1) AS accounts_count
// FROM Accounts
// GROUP BY 1
// )
// SELECT category, IFNULL(accounts_count, 0) AS accounts_count
// FROM
// S
// LEFT JOIN T USING (category);
// "#
// }
// Accepted solution for LeetCode #1907: Count Salary Categories
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1907: Count Salary Categories
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1907: Count Salary Categories
// # Write your MySQL query statement below
// WITH
// S AS (
// SELECT 'Low Salary' AS category
// UNION
// SELECT 'Average Salary'
// UNION
// SELECT 'High Salary'
// ),
// T AS (
// SELECT
// CASE
// WHEN income < 20000 THEN "Low Salary"
// WHEN income > 50000 THEN 'High Salary'
// ELSE 'Average Salary'
// END AS category,
// COUNT(1) AS accounts_count
// FROM Accounts
// GROUP BY 1
// )
// SELECT category, IFNULL(accounts_count, 0) AS accounts_count
// FROM
// S
// LEFT JOIN T USING (category);
// "#
// }
# Accepted solution for LeetCode #1907: Count Salary Categories
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1907: Count Salary Categories
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1907: Count Salary Categories
# # Write your MySQL query statement below
# WITH
# S AS (
# SELECT 'Low Salary' AS category
# UNION
# SELECT 'Average Salary'
# UNION
# SELECT 'High Salary'
# ),
# T AS (
# SELECT
# CASE
# WHEN income < 20000 THEN "Low Salary"
# WHEN income > 50000 THEN 'High Salary'
# ELSE 'Average Salary'
# END AS category,
# COUNT(1) AS accounts_count
# FROM Accounts
# GROUP BY 1
# )
# SELECT category, IFNULL(accounts_count, 0) AS accounts_count
# FROM
# S
# LEFT JOIN T USING (category);
# "#
# }
// Accepted solution for LeetCode #1907: Count Salary Categories
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1907: Count Salary Categories
# Write your MySQL query statement below
WITH
S AS (
SELECT 'Low Salary' AS category
UNION
SELECT 'Average Salary'
UNION
SELECT 'High Salary'
),
T AS (
SELECT
CASE
WHEN income < 20000 THEN "Low Salary"
WHEN income > 50000 THEN 'High Salary'
ELSE 'Average Salary'
END AS category,
COUNT(1) AS accounts_count
FROM Accounts
GROUP BY 1
)
SELECT category, IFNULL(accounts_count, 0) AS accounts_count
FROM
S
LEFT JOIN T USING (category);
"#
}
// Accepted solution for LeetCode #1907: Count Salary Categories
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1907: Count Salary Categories
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1907: Count Salary Categories
// # Write your MySQL query statement below
// WITH
// S AS (
// SELECT 'Low Salary' AS category
// UNION
// SELECT 'Average Salary'
// UNION
// SELECT 'High Salary'
// ),
// T AS (
// SELECT
// CASE
// WHEN income < 20000 THEN "Low Salary"
// WHEN income > 50000 THEN 'High Salary'
// ELSE 'Average Salary'
// END AS category,
// COUNT(1) AS accounts_count
// FROM Accounts
// GROUP BY 1
// )
// SELECT category, IFNULL(accounts_count, 0) AS accounts_count
// FROM
// S
// LEFT JOIN T USING (category);
// "#
// }
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.