Find Cutoff Score for Each School - Problem

You are given two tables: Schools and Exam.

The Schools table contains information about school capacities - the maximum number of students each school can accept.

The Exam table shows exam score distribution - for each score, it tells you how many students achieved at least that score.

Goal: For each school, find the minimum score requirement that:

  • Ensures the school can accept all students who meet the requirement (even if they all apply)
  • Maximizes the number of students who can potentially apply
  • Uses a score that exists in the Exam table

If multiple scores satisfy these conditions, choose the smallest score. If no valid score exists, return -1.

Table Schema

Schools
Column Name Type Description
school_id PK int Unique identifier for each school
capacity int Maximum number of students the school can accept
Primary Key: school_id
Exam
Column Name Type Description
score PK int Exam score threshold
student_count int Number of students who scored at least this score
Primary Key: score

Input & Output

Example 1 — Basic School Cutoff Selection
Input Tables:
Schools
school_id capacity
1 2
2 3
Exam
score student_count
500 10
600 5
700 2
Output:
school_id score
1 700
2 700
💡 Note:

School 1 (capacity=2): Score 500 has 10 students (>2), score 600 has 5 students (>2), score 700 has 2 students (≤2). Choose 700.

School 2 (capacity=3): Score 500 has 10 students (>3), score 600 has 5 students (>3), score 700 has 2 students (≤3). Choose 700 to maximize students.

Example 2 — No Valid Score
Input Tables:
Schools
school_id capacity
1 1
Exam
score student_count
400 5
500 3
Output:
school_id score
1 -1
💡 Note:

School 1 (capacity=1): All scores have more than 1 student. Score 400 has 5 students, score 500 has 3 students. No valid cutoff exists, so return -1.

Example 3 — Tie Breaking by Minimum Score
Input Tables:
Schools
school_id capacity
1 3
Exam
score student_count
300 3
400 3
500 1
Output:
school_id score
1 300
💡 Note:

School 1 (capacity=3): Scores 300 and 400 both have exactly 3 students (maximum that fits). Score 500 has only 1 student. To maximize students while breaking ties, choose minimum score 300.

Constraints

  • 1 ≤ school_id ≤ 1000
  • 1 ≤ capacity ≤ 100000
  • 1 ≤ score ≤ 1000
  • 1 ≤ student_count ≤ 100000
  • score values are unique in Exam table
  • Higher scores have same or fewer students than lower scores

Visualization

Tap to expand
Find Cutoff Score for Each School INPUT Schools (capacity): School A: 3 School B: 2 Applicants (school, score): Student 1: A, 85 Student 2: A, 90 Student 3: A, 75 Student 4: A, 80 Student 5: B, 95 Student 6: B, 70 Student 7: B, 88 Goal: Find min score per school to maximize accepted students ALGORITHM STEPS 1 Group by School Collect applicants per school 2 Sort Descending Sort scores high to low School A: [90, 85, 80, 75] School B: [95, 88, 70] Sorted by score (desc) 3 Apply Capacity Take top N students A(cap=3): [90, 85, 80] | 75 B(cap=2): [95, 88] | 70 Accepted | Rejected 4 Get Min Score Cutoff = last accepted score FINAL RESULT Cutoff Scores: School A Capacity: 3 | Applicants: 4 Cutoff: 80 School B Capacity: 2 | Applicants: 3 Cutoff: 88 Output: {A: 80, B: 88} OK - Max students accepted 5/7 Key Insight: Sort applicants by score in descending order, then take the top N (capacity) students. The cutoff score is simply the score of the last accepted student (Nth highest). Time: O(n log n) for sorting | Space: O(n) for grouping by school TutorialsPoint - Find Cutoff Score for Each School | Optimal Solution
Asked in
Amazon 15 Microsoft 12 Google 8
28.5K Views
Medium Frequency
~18 min Avg. Time
890 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