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: Students
+---------------+---------+ | Column Name | Type | +---------------+---------+ | student_id | int | | student_name | varchar | +---------------+---------+ student_id is the primary key (column with unique values) for this table. Each row of this table contains the ID and the name of one student in the school.
Table: Subjects
+--------------+---------+ | Column Name | Type | +--------------+---------+ | subject_name | varchar | +--------------+---------+ subject_name is the primary key (column with unique values) for this table. Each row of this table contains the name of one subject in the school.
Table: Examinations
+--------------+---------+ | Column Name | Type | +--------------+---------+ | student_id | int | | subject_name | varchar | +--------------+---------+ There is no primary key (column with unique values) for this table. It may contain duplicates. Each student from the Students table takes every course from the Subjects table. Each row of this table indicates that a student with ID student_id attended the exam of subject_name.
Write a solution to find the number of times each student attended each exam.
Return the result table ordered by student_id and subject_name.
The result format is in the following example.
Example 1:
Input: Students table: +------------+--------------+ | student_id | student_name | +------------+--------------+ | 1 | Alice | | 2 | Bob | | 13 | John | | 6 | Alex | +------------+--------------+ Subjects table: +--------------+ | subject_name | +--------------+ | Math | | Physics | | Programming | +--------------+ Examinations table: +------------+--------------+ | student_id | subject_name | +------------+--------------+ | 1 | Math | | 1 | Physics | | 1 | Programming | | 2 | Programming | | 1 | Physics | | 1 | Math | | 13 | Math | | 13 | Programming | | 13 | Physics | | 2 | Math | | 1 | Math | +------------+--------------+ Output: +------------+--------------+--------------+----------------+ | student_id | student_name | subject_name | attended_exams | +------------+--------------+--------------+----------------+ | 1 | Alice | Math | 3 | | 1 | Alice | Physics | 2 | | 1 | Alice | Programming | 1 | | 2 | Bob | Math | 1 | | 2 | Bob | Physics | 0 | | 2 | Bob | Programming | 1 | | 6 | Alex | Math | 0 | | 6 | Alex | Physics | 0 | | 6 | Alex | Programming | 0 | | 13 | John | Math | 1 | | 13 | John | Physics | 1 | | 13 | John | Programming | 1 | +------------+--------------+--------------+----------------+ Explanation: The result table should contain all students and all subjects. Alice attended the Math exam 3 times, the Physics exam 2 times, and the Programming exam 1 time. Bob attended the Math exam 1 time, the Programming exam 1 time, and did not attend the Physics exam. Alex did not attend any exams. John attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.
Problem summary: Table: Students +---------------+---------+ | Column Name | Type | +---------------+---------+ | student_id | int | | student_name | varchar | +---------------+---------+ student_id is the primary key (column with unique values) for this table. Each row of this table contains the ID and the name of one student in the school. Table: Subjects +--------------+---------+ | Column Name | Type | +--------------+---------+ | subject_name | varchar | +--------------+---------+ subject_name is the primary key (column with unique values) for this table. Each row of this table contains the name of one subject in the school. Table: Examinations +--------------+---------+ | Column Name | Type | +--------------+---------+ | student_id | int | | subject_name | varchar | +--------------+---------+ There is no primary key (column with unique values) for this table. It may contain duplicates. Each
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"Students":["student_id","student_name"],"Subjects":["subject_name"],"Examinations":["student_id","subject_name"]},"rows":{"Students":[[1,"Alice"],[2,"Bob"],[13,"John"],[6,"Alex"]],"Subjects":[["Math"],["Physics"],["Programming"]],"Examinations":[[1,"Math"],[1,"Physics"],[1,"Programming"],[2,"Programming"],[1,"Physics"],[1,"Math"],[13,"Math"],[13,"Programming"],[13,"Physics"],[2,"Math"],[1,"Math"]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1280: Students and Examinations
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1280: Students and Examinations
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1280: Students and Examinations
// # Write your MySQL query statement below
// SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
// FROM
// Students
// JOIN Subjects
// LEFT JOIN Examinations AS e USING (student_id, subject_name)
// GROUP BY 1, 3
// ORDER BY 1, 3;
// "#
// }
// Accepted solution for LeetCode #1280: Students and Examinations
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1280: Students and Examinations
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1280: Students and Examinations
// # Write your MySQL query statement below
// SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
// FROM
// Students
// JOIN Subjects
// LEFT JOIN Examinations AS e USING (student_id, subject_name)
// GROUP BY 1, 3
// ORDER BY 1, 3;
// "#
// }
# Accepted solution for LeetCode #1280: Students and Examinations
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1280: Students and Examinations
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1280: Students and Examinations
# # Write your MySQL query statement below
# SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
# FROM
# Students
# JOIN Subjects
# LEFT JOIN Examinations AS e USING (student_id, subject_name)
# GROUP BY 1, 3
# ORDER BY 1, 3;
# "#
# }
// Accepted solution for LeetCode #1280: Students and Examinations
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1280: Students and Examinations
# Write your MySQL query statement below
SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
FROM
Students
JOIN Subjects
LEFT JOIN Examinations AS e USING (student_id, subject_name)
GROUP BY 1, 3
ORDER BY 1, 3;
"#
}
// Accepted solution for LeetCode #1280: Students and Examinations
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1280: Students and Examinations
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1280: Students and Examinations
// # Write your MySQL query statement below
// SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
// FROM
// Students
// JOIN Subjects
// LEFT JOIN Examinations AS e USING (student_id, subject_name)
// GROUP BY 1, 3
// ORDER BY 1, 3;
// "#
// }
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.