Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Strings
Matrices
Students And Examinations

Compute each student's exam attendance across multiple subjects, including subjects with zero attempts.

Acceptance 100%
Problem Statement

Students and Examinations

You are given three tables describing students, subjects, and examination records. For every student, report how many times they took each subject exam.

A student should appear once for every subject, even if they never took that exam. The result should include the student's ID, name, subject name, and the number of attended exams.

Return the rows ordered by student ID, then subject name.

Input Format

  • A table of students with columns such as student_id and student_name.
  • A table of subjects with columns such as subject_name.
  • A table of examination records with columns such as student_id and subject_name.

Each row in the exam table represents one exam attendance.

Output Format

Return one row per (student, subject) pair with:

  1. student ID
  2. student name
  3. subject name
  4. attended exam count

Constraints

  • Every student must be paired with every subject.
  • Counts may be zero.
  • Sorting should be deterministic by student ID and subject name.
  • Assume input tables may contain multiple exam rows for the same student and subject.
Examples
Sample cases returned by the problem API.

Example 1

Input

Students
| student_id | student_name |
|-----------:|--------------|
| 1          | Alice        |
| 2          | Bob          |

Subjects
| subject_name |
|--------------|
| Math         |
| Physics      |

Examinations
| student_id | subject_name |
|-----------:|--------------|
| 1          | Math         |
| 1          | Math         |
| 1          | Physics      |
| 2          | Math         |

Output

| student_id | student_name | subject_name | attended_exams |
|-----------:|--------------|--------------|----------------:|
| 1          | Alice        | Math         | 2               |
| 1          | Alice        | Physics      | 1               |
| 2          | Bob          | Math         | 1               |
| 2          | Bob          | Physics      | 0               |

Explanation

Generate all student-subject combinations, then count how many examination rows match each pair. Bob never took Physics, so his count is 0.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.