TryBugHunt
javaType errorMedium

Back-to-back duplicate orders go unnoticed

BugFind the bug and fix the code

What it should do

countRepeats(orderIds) counts how many times an order ID is the same as the one right before it (a double submission), so countRepeats([500, 500, 600]) returns 1.

What happens instead

countRepeats([500, 500, 600]) returns 0. countRepeats([5, 5, 7]) correctly returns 1.

import java.util.*;

public class Main {
    static int countRepeats(List<Integer> orderIds) {
        int count = 0;
        for (int i = 1; i < orderIds.size(); i++) {
            if (orderIds.get(i) == orderIds.get(i - 1)) {
                count++;
            }
        }
        return count;
    }
}

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 type errors pattern

Integer division, autoboxing surprises, and == comparing references instead of values.

What you'll practise

A medium Java debugging exercise about type errors: integer division, autoboxing surprises, and == comparing references instead of values.

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)

A List<Integer> holds Integer objects, not int values. For objects, == checks whether both sides are the same object, not whether they hold the same number. Java caches Integer objects from -128 to 127, so small IDs happen to share an object and == looks correct in testing. Real order IDs above 127 are separate objects, and == is false even when the numbers match. Use .equals() for boxed values, or unbox one side first: orderIds.get(i).intValue() == orderIds.get(i - 1). This is one of the classic Java bugs that passes every test with small numbers and then fails in production.

The pattern behind it: type errors.

More type 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/afb3d766-380d-47ed-820e-387ad0d91f31" title="Debugging challenge: Back-to-back duplicate orders go unnoticed" 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/afb3d766-380d-47ed-820e-387ad0d91f31">TryBugHunt</a></p>