The Off-by-One Loop

~9 mindebugging

Fix count_above so it checks every value.

Examples

The last value is counted too

Input
count_above([5, 10], 1)
Output
2

Values equal to the threshold do not count, only strictly above

Input
count_above([1, 2, 3], 2)
Output
1

Nothing above the threshold gives 0

Input
count_above([1, 1, 1], 5)
Output
0

Hints

Hint 1

Walk the input once and accumulate as you go.

Hint 2

Watch for this: still skips last element.

Requirements

  • : a list of numbers

  • threshold: the number to compare against

  • Return How many values are above the threshold.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~9 min

3 employers weight this skill

2 frontier labs, 1 AI product company. Top match scores 80.

Python
def count_above(values, threshold):
    """
    Count how many values are strictly greater than threshold.

    This currently misses the last value. Fix it.

    Args:
        values: a list of numbers
        threshold: the number to compare against

    Returns:
        How many values are above the threshold.
    """
    count = 0
    for i in range(len(values) - 1):
        if values[i] > threshold:
            count += 1
    return count
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.