TryBugHunt
javaLogic errorMedium

Goal streak ignores a run that lasts until today

BugFind the bug and fix the code

What it should do

longestStreak(steps, goal) returns the longest run of consecutive days where steps reached the goal, so longestStreak([5, 1, 6, 7, 8], 5) returns 3.

What happens instead

longestStreak([5, 1, 6, 7, 8], 5) returns 1 instead of 3, and longestStreak([6, 6, 6], 5) returns 0.

public class Main {
    static int longestStreak(int[] steps, int goal) {
        int best = 0;
        int current = 0;
        for (int s : steps) {
            if (s >= goal) {
                current++;
            } else {
                best = Math.max(best, current);
                current = 0;
            }
        }
        return best;
    }
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the logic errors pattern

The wrong operator, an inverted condition, a formula the right way round but backwards.

What you'll practise

A medium Java debugging exercise about logic errors: the wrong operator, an inverted condition, a formula the right way round but backwards.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

The best streak is only updated when a streak ends, which the code detects as a day below the goal. A streak that continues to the last day never meets such a day, so it is never compared with best. That is why a user on a 3-day streak right now is told their best is 1. Loops that track runs or groups almost always need one final step after the loop to handle the last group. A test where the input ends in the middle of a run, and one where every item qualifies, will catch this every time.

The pattern behind it: logic errors.

More logic errors to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/6b2d5129-13e5-4b8e-9625-2711f44471d2" title="Debugging challenge: Goal streak ignores a run that lasts until today" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/6b2d5129-13e5-4b8e-9625-2711f44471d2">TryBugHunt</a></p>