Gangnam Style Broke the Counter — Java Bug Hunt

Inspired by 2014, when PSY's "Gangnam Style" blew past 2,147,483,647 views and YouTube publicly admitted their view counter needed an upgrade to 64 bits.

  • Language: Java
  • Layer: Backend
  • Difficulty: Easy
  • Concepts: Overflow, Counters
  • Modelled on: YouTube · 2014
  • Visible tests: ordinary counting works; crossing two billion keeps counting up
  • Reward: 50 XP for a complete fix

Briefing

Inspired by 2014, when PSY's "Gangnam Style" blew past 2,147,483,647 views and YouTube publicly admitted their view counter needed an upgrade to 64 bits.

ViewCounter.java is still on the old math.

Bug report

BUG-PSY · Reported by: analytics

addViews(current, delta) must keep counting past Integer.MAX_VALUE. Observed: the most-watched video on the platform reports -2,147,473,648 views.

Logs

[stats] video=9bZkp7q19f0 views=-2147473648

The code as shipped

ViewCounter.java (editable)

class ViewCounter {
    static long addViews(long current, long delta) {
        int next = (int) current + (int) delta;
        return next;
    }
}

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More Java bug hunts.