Count Students Who Completed All Assignments - Problem

You are given a 2D integer array submissions where submissions[i] = [studentId, assignmentId] represents that a student submitted an assignment, and an integer totalAssignments representing the total number of assignments.

A student has completed all assignments if they have submissions for every assignment from 0 to totalAssignments - 1.

Return the number of students who completed all assignments.

Input & Output

Example 1 — Basic Case
$ Input: submissions = [[1,0],[1,1],[1,2],[2,0],[2,1],[3,0],[3,1],[3,2]], totalAssignments = 3
Output: 2
💡 Note: Student 1 submitted {0,1,2} ✅, Student 2 submitted {0,1} ❌ missing 2, Student 3 submitted {0,1,2} ✅. Two students completed all 3 assignments.
Example 2 — No One Completed
$ Input: submissions = [[5,0],[5,2],[8,1],[8,2]], totalAssignments = 3
Output: 0
💡 Note: Student 5 is missing assignment 1, Student 8 is missing assignment 0. Neither completed all 3 assignments.
Example 3 — Duplicate Submissions Ignored
$ Input: submissions = [[1,0],[1,0],[1,1],[2,0],[2,1]], totalAssignments = 2
Output: 2
💡 Note: Student 1's duplicate submission for assignment 0 doesn't matter. Both students submitted assignments 0 and 1, completing all required.

Constraints

  • 1 ≤ submissions.length ≤ 10⁴
  • 1 ≤ totalAssignments ≤ 26
  • 0 ≤ studentId ≤ 10⁴
  • 0 ≤ assignmentId < totalAssignments
  • submissions[i].length == 2

Visualization

Tap to expand
Count Students Who Completed All Assignments INPUT submissions: [1,0] [1,1] [1,2] [2,0] [2,1] [3,0] [3,1] [3,2] totalAssignments: 3 (assignments 0, 1, 2) Question: How many students submitted ALL assignments (0, 1, 2)? GROUP BY STUDENT 1 Build HashMap of Sets Student 1 → {0, 1, 2} Student 2 → {0, 1} Student 3 → {0, 1, 2} Duplicates auto-removed by Set 2 Check set.size == 3 Student 1: 3 == 3 ✅ Student 2: 2 != 3 ❌ Student 3: 3 == 3 ✅ RESULT Output: 2 students completed all ✅ Student 1 — all 3 ✅ Student 3 — all 3 ❌ Student 2 — only 2/3 Key Insight: Use a HashMap<studentId, Set<assignmentId>> to group and deduplicate in one pass. Then count students whose set size equals totalAssignments. Time: O(n), Space: O(n). TutorialsPoint - Count Students Who Completed All Assignments
Asked in
Amazon 12 Google 8 Microsoft 6
28.0K Views
Medium Frequency
~10 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen