TryBugHunt
javaOff-by-oneHard

Merged schedule ends with a 0 instead of the latest time

BugFind the bug and fix the code

What it should do

a and b are two sorted lists of meeting start times (in minutes). mergeSorted(a, b) returns all the times in one sorted array, so mergeSorted([1, 4], [2, 3]) returns [1, 2, 3, 4].

What happens instead

mergeSorted([1, 4], [2, 3]) returns [1, 2, 3, 0], and mergeSorted([5, 9], [1]) returns [1, 5, 0].

public class Main {
    static int[] mergeSorted(int[] a, int[] b) {
        int[] out = new int[a.length + b.length];
        int i = 0, j = 0, k = 0;
        while (i < a.length && j < b.length) {
            if (a[i] <= b[j]) {
                out[k++] = a[i++];
            } else {
                out[k++] = b[j++];
            }
        }
        while (i < a.length - 1) {
            out[k++] = a[i++];
        }
        while (j < b.length) {
            out[k++] = b[j++];
        }
        return out;
    }
}

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 off-by-one errors pattern

The loop that runs one time too many, the slice that drops the last item, the index that starts in the wrong place.

What you'll practise

A hard Java debugging exercise about off-by-one errors: the loop that runs one time too many, the slice that drops the last item, the index that starts in the wrong place.

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 merge has two phases: take the smaller head while both arrays have items, then copy whatever is left of the array that did not run out. The leftover loop for a stops at a.length - 1, so the last remaining element of a is never copied. Java fills a new int array with zeros, which is why the missing value shows up as a trailing 0 instead of an error. The bug only appears when a is the array with leftovers; if b runs out last, its loop is correct and the result looks fine. When two loops are meant to be mirror images, compare them side by side: their bounds should be identical apart from the variable names.

The pattern behind it: off-by-one errors.

More off-by-one errors to practise

Related errors

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/4021c54b-683f-4be8-9eed-2b474a2e29cb" title="Debugging challenge: Merged schedule ends with a 0 instead of the latest time" 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/4021c54b-683f-4be8-9eed-2b474a2e29cb">TryBugHunt</a></p>