cswork / libraryOpen practice workspace

Coding questions

Count Failing, Passing and Distinction Scores from Percentage Thresholds

Given an array of student exam scores as percentages, count how many students failed with under 50, passed with 50 to 80 inclusive, or passed with distinction above 80, and return the three counts in order. It tests careful handling of inclusive and exclusive boundaries and empty input.

You are given an array of integers scores, where the index is a student ID and the value is that student's exam score as a percentage. Each student is graded as follows:

  • below 50: failed,
  • from 50 to 80 inclusive: passed,
  • above 80: passed with distinction.

Return an array of three integers: the number of students who failed, the number who passed, and the number who passed with distinction, in that order.

Function Signature

def grade_counts(scores: list[int]) -> list[int]:

Rules

  • A score of exactly 50 or exactly 80 counts as passed; a score of exactly 81 counts as passed with distinction.
  • Every student falls into exactly one of the three groups, so the three counts sum to len(scores).
  • An empty array returns [0, 0, 0].

Constraints

  • 0 <= len(scores) <= 100000
  • 0 <= scores[i] <= 100

Examples

Example 1

  • Input: scores = [30, 50, 65, 80, 81, 95, 49]
  • Output: [2, 3, 2]
  • Explanation: 30 and 49 fail; 50, 65 and 80 pass; 81 and 95 pass with distinction.

Example 2

  • Input: scores = [100, 100]
  • Output: [0, 0, 2]
  • Explanation: Both students scored above 80.

Example 3

  • Input: scores = []
  • Output: [0, 0, 0]
  • Explanation: With no students, every count is zero.

Exam Grade Band Tally

You are given an array of integers scores, where the index is a student ID and the value is that student's exam score as a percentage. Each student lands in exactly one grade band:

  • a score below 50 is failed;
  • a score from 50 to 80 inclusive is passed;
  • a score above 80 is passed with distinction.

Return an array of exactly three integers [failed, passed, distinction]: the number of students who failed, the number who passed, and the number who passed with distinction, in that order.

Rules

  • A score of exactly 50 or exactly 80 counts as passed; a score of exactly 81 counts as passed with distinction.
  • The three counts always sum to len(scores).
  • An empty array returns [0, 0, 0].

Constraints

  • 0 <= len(scores) <= 100000
  • 0 <= scores[i] <= 100
  • Every count is at most 100000, so all values fit in a 32-bit signed integer.

Example 1

  • Input: scores = [30, 50, 65, 80, 81, 95, 49]
  • Output: [2, 3, 2]
  • Explanation: 30 and 49 fail; 50, 65 and 80 pass; 81 and 95 pass with distinction.

Example 2

  • Input: scores = [100, 100]
  • Output: [0, 0, 2]
  • Explanation: Both students scored above 80.

Example 3

  • Input: scores = []
  • Output: [0, 0, 0]
  • Explanation: With no students, every count is zero.

Hints

  1. Every student belongs to exactly one band, so a single pass over the array with three counters is enough.
  2. Pin down the two cut points precisely: 49 and 50 fall on different sides, and so do 80 and 81.
  3. Check the lower band first, then the middle band's upper edge; whatever remains must be the top band.

Approach

Walk the array once and keep three counters. For each score s: if s < 50 the student failed; otherwise, if s <= 80 the student passed (this covers 50 through 80 inclusive); otherwise s >= 81 and the student passed with distinction. Because the bands are checked in increasing order, each score increments exactly one counter, so the three counts sum to the number of students and the empty array naturally yields [0, 0, 0]. The counters are returned in the required order [failed, passed, distinction].

  • Time complexity: O(n)
  • Space complexity: O(1)