Find Cutoff Score for Each School - Problem

๐Ÿซ School Admission Cutoff Calculator

Every year, schools face a crucial challenge: setting the perfect minimum score requirement for student applications. Set it too high, and they miss out on qualified students. Set it too low, and they get overwhelmed with applications they can't accommodate!

You're given two tables:

  • Schools table: Contains each school's school_id and maximum capacity (number of students they can accept)
  • Exam table: Contains score values and student_count (number of students who achieved at least that score)

Each school wants to:

  1. ๐Ÿ“ˆ Maximize the number of potential applicants
  2. ๐ŸŽฏ Ensure they can accept everyone who meets the requirement (even if all qualified students apply)
  3. ๐Ÿ“Š Use only scores that exist in the Exam table
  4. โฌ‡๏ธ Choose the smallest score if multiple scores work

Goal: Find the optimal minimum score requirement for each school. Return -1 if no suitable score exists.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Schools: [[1,2],[2,3]] Exam: [[500,4],[600,3],[700,1]]
โ€บ Output: [[1,700],[2,600]]
๐Ÿ’ก Note: School 1 (capacity 2): Can accept scores 700(1 student) or 600(3 students). Since 3>2, only 700 works. School 2 (capacity 3): Can accept 700(1 student) or 600(3 students). Choose 600 for maximum students.
example_2.sql โ€” No Valid Score
$ Input: Schools: [[1,1]] Exam: [[500,5],[600,3],[700,2]]
โ€บ Output: [[1,-1]]
๐Ÿ’ก Note: School 1 has capacity 1, but all exam scores have student counts โ‰ฅ 2. No valid cutoff score exists, so return -1.
example_3.sql โ€” Tie Breaking
$ Input: Schools: [[1,3],[2,5]] Exam: [[400,3],[500,3],[600,1]]
โ€บ Output: [[1,400],[2,400]]
๐Ÿ’ก Note: Both schools can accept scores 400 and 500 (both have 3 students). Since both give same max students, choose the smaller score (400).

Constraints

  • 1 โ‰ค number of schools โ‰ค 103
  • 1 โ‰ค school capacity โ‰ค 106
  • 1 โ‰ค number of exam records โ‰ค 103
  • 1 โ‰ค score โ‰ค 106
  • 0 โ‰ค student_count โ‰ค 106
  • Important: Higher scores always have โ‰ค student counts than lower scores

Visualization

Tap to expand
๐Ÿซ School ACapacity: 3Cutoff: 600๐Ÿซ School BCapacity: 2Cutoff: 700๐Ÿ“Š Student Score Distribution5004 students6003 students7001 studentA picks 600: 3โ‰ค3 โœ“B picks 700: 1โ‰ค2 โœ“๐ŸŽฏ Optimization Logic1. Filter: student_count โ‰ค capacity2. Maximize: highest valid student_count3. Minimize: lowest score among tiesResult: Maximum qualified applicants!
Understanding the Visualization
1
Analyze Demand
Cross-reference school capacities with student score distributions
2
Find Sweet Spot
Identify scores where student count โ‰ค school capacity
3
Maximize Efficiency
Choose the score that accepts the most students possible
4
Optimize Selection
When tied, pick the lowest score to be more inclusive
Key Takeaway
๐ŸŽฏ Key Insight: Find the highest student count that fits within capacity, then pick the lowest score that achieves it - this maximizes both accessibility and enrollment!
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
67.2K Views
Medium Frequency
~25 min Avg. Time
1.8K 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