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.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
Table: Employees
+-------------+----------+ | Column Name | Type | +-------------+----------+ | employee_id | int | | name | varchar | | reports_to | int | | age | int | +-------------+----------+ employee_id is the column with unique values for this table. This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null).
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
Write a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
Return the result table ordered by employee_id.
The result format is in the following example.
Example 1:
Input: Employees table: +-------------+---------+------------+-----+ | employee_id | name | reports_to | age | +-------------+---------+------------+-----+ | 9 | Hercy | null | 43 | | 6 | Alice | 9 | 41 | | 4 | Bob | 9 | 36 | | 2 | Winston | null | 37 | +-------------+---------+------------+-----+ Output: +-------------+-------+---------------+-------------+ | employee_id | name | reports_count | average_age | +-------------+-------+---------------+-------------+ | 9 | Hercy | 2 | 39 | +-------------+-------+---------------+-------------+ Explanation: Hercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.
Example 2:
Input: Employees table: +-------------+---------+------------+-----+ | employee_id | name | reports_to | age | |-------------|---------|------------|-----| | 1 | Michael | null | 45 | | 2 | Alice | 1 | 38 | | 3 | Bob | 1 | 42 | | 4 | Charlie | 2 | 34 | | 5 | David | 2 | 40 | | 6 | Eve | 3 | 37 | | 7 | Frank | null | 50 | | 8 | Grace | null | 48 | +-------------+---------+------------+-----+ Output: +-------------+---------+---------------+-------------+ | employee_id | name | reports_count | average_age | | ----------- | ------- | ------------- | ----------- | | 1 | Michael | 2 | 40 | | 2 | Alice | 2 | 37 | | 3 | Bob | 1 | 37 | +-------------+---------+---------------+-------------+
Problem summary: Table: Employees +-------------+----------+ | Column Name | Type | +-------------+----------+ | employee_id | int | | name | varchar | | reports_to | int | | age | int | +-------------+----------+ employee_id is the column with unique values for this table. This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null). For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them. Write a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer. Return the result table ordered by employee_id. 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":{"Employees":["employee_id","name","reports_to","age"]},"rows":{"Employees":[[9,"Hercy",null,43],[6,"Alice",9,41],[4,"Bob",9,36],[2,"Winston",null,37]]}}{"headers":{"Employees":["employee_id","name","reports_to","age"]},"rows":{"Employees":[[1,"Michael",null,45],[2,"Alice",1,38],[3,"Bob",1,42],[4,"Charlie",2,34],[5,"David",2,40],[6,"Eve",3,37],[7,"Frank",null,50],[8,"Grace",null,48]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// # Write your MySQL query statement below
// SELECT
// e2.employee_id,
// e2.name,
// COUNT(1) AS reports_count,
// ROUND(AVG(e1.age)) AS average_age
// FROM
// Employees AS e1
// JOIN Employees AS e2 ON e1.reports_to = e2.employee_id
// GROUP BY 1
// ORDER BY 1;
// "#
// }
// Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// # Write your MySQL query statement below
// SELECT
// e2.employee_id,
// e2.name,
// COUNT(1) AS reports_count,
// ROUND(AVG(e1.age)) AS average_age
// FROM
// Employees AS e1
// JOIN Employees AS e2 ON e1.reports_to = e2.employee_id
// GROUP BY 1
// ORDER BY 1;
// "#
// }
# Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
# # Write your MySQL query statement below
# SELECT
# e2.employee_id,
# e2.name,
# COUNT(1) AS reports_count,
# ROUND(AVG(e1.age)) AS average_age
# FROM
# Employees AS e1
# JOIN Employees AS e2 ON e1.reports_to = e2.employee_id
# GROUP BY 1
# ORDER BY 1;
# "#
# }
// Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
# Write your MySQL query statement below
SELECT
e2.employee_id,
e2.name,
COUNT(1) AS reports_count,
ROUND(AVG(e1.age)) AS average_age
FROM
Employees AS e1
JOIN Employees AS e2 ON e1.reports_to = e2.employee_id
GROUP BY 1
ORDER BY 1;
"#
}
// Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1731: The Number of Employees Which Report to Each Employee
// # Write your MySQL query statement below
// SELECT
// e2.employee_id,
// e2.name,
// COUNT(1) AS reports_count,
// ROUND(AVG(e1.age)) AS average_age
// FROM
// Employees AS e1
// JOIN Employees AS e2 ON e1.reports_to = e2.employee_id
// GROUP BY 1
// 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.