TryBugHunt
goType errorHard

Average file size goes negative for large videos

BugFind the bug and fix the code

What it should do

averageFileSize(sizes) returns the average size in bytes of a non-empty list of files, rounded down. averageFileSize([2000000000, 2000000000]) returns 2000000000.

What happens instead

averageFileSize([2000000000, 2000000000]) returns -147483648.

package main

func averageFileSize(sizes []int) int {
    var total int32
    for _, s := range sizes {
        total += int32(s)
    }
    return int(total) / len(sizes)
}

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 that drops the fraction, overflow, and conversions that change the value.

What you'll practise

A hard Go debugging exercise about type errors: integer division that drops the fraction, overflow, and conversions that change the value.

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)

An int32 holds values up to 2147483647. Two 2 GB files add up to 4000000000, which does not fit, and Go integer overflow does not panic: the value silently wraps around to -294967296, and half of that is -147483648. Small test files never get near the limit, so the code looks fine until real videos arrive. Use int64 (or int, which is 64 bits on the runner) for sizes, money in cents and counters that can grow, and only narrow to int32 when a format requires it.

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/0da22f19-724f-441c-82d8-86f3a12c7730" title="Debugging challenge: Average file size goes negative for large videos" 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/0da22f19-724f-441c-82d8-86f3a12c7730">TryBugHunt</a></p>