TryBugHunt
javaType errorMedium

30-day trial length comes out negative

BugFind the bug and fix the code

What it should do

trialMillis(days) converts a trial length in days to milliseconds, so trialMillis(1) returns 86400000 and trialMillis(30) returns 2592000000.

What happens instead

trialMillis(30) returns -1702967296. trialMillis(1) and trialMillis(7) are correct.

public class Main {
    static long trialMillis(int days) {
        return days * 24 * 60 * 60 * 1000;
    }
}

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)

Every number in days * 24 * 60 * 60 * 1000 is an int, so Java multiplies in int and only widens to long at the very end. 30 days is 2,592,000,000 milliseconds, which is more than an int can hold, so the product wraps around to a negative number before it is ever converted. Making one of the early operands a long, such as 24L, switches the whole chain to long arithmetic. The type of an expression is decided by its operands, not by the variable or return type it ends up in. Short periods fit in an int, so this kind of bug only shows up once the numbers get large.

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/e633c41b-5f52-4b19-a2ea-78284382d363" title="Debugging challenge: 30-day trial length comes out negative" 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/e633c41b-5f52-4b19-a2ea-78284382d363">TryBugHunt</a></p>