Code Logo

Passing Score Count with count()

Published at22 Apr 2026
Stream API Easy 0 views
Like0

A teacher has a list of quiz scores and wants to know how many students reached the passing mark. A passing score is any value greater than or equal to 75.

Your task is to return how many scores meet that rule. The result should be a number, not a list.

For example, if the input is [60, 75, 80, 70, 90], the answer should be 3 because 75, 80, and 90 all count as passing. If the input is [50, 55, 60], the answer should be 0.

This challenge focuses on the count() terminal operation in a Java stream. The stream should filter the passing scores first, then count how many remain.

Example Input & Output

Example 1
Input
scores = [60, 75, 80, 70, 90]
Output
3
Explanation

Three scores meet the passing rule.

Example 2
Input
scores = [50, 55, 60]
Output
0
Explanation

No score reaches 75 or above.

Example 3
Input
scores = [75, 75, 75]
Output
3
Explanation

Scores equal to 75 still count as passing.

Algorithm Flow

Recommendation Algorithm Flow for Passing Score Count with count()
Recommendation Algorithm Flow for Passing Score Count with count()

Solution Approach

This problem naturally splits into two Stream API steps. First, keep only the scores that satisfy the passing rule. Second, count how many values passed that filter.

The passing rule is simple: a score counts if it is at least 75. That means the filtering step can be written as score -> score >= 75. Once that is in place, Java's count() method gives the size of the filtered stream as a long.

A direct stream pipeline looks like this:

return scores.stream()
    .filter(score -> score >= 75)
    .count();

This reads clearly because each stage mirrors the problem statement. The filter() stage decides who passes, and the count() stage answers how many such scores exist.

The runtime is O(n) for n scores since each value is checked once, and the extra space is minimal because no new collection is built.

Best Answers

java - Approach 1
import java.util.List;

class Solution {
    public static long countPassingScores(List<Integer> scores) {
        return scores.stream()
                .filter(score -> score >= 75)
                .count();
    }
}