The Share Price That Outgrew Its Integer — Java Bug Hunt

Modelled on Nasdaq and Berkshire Hathaway (May 2021): Nasdaq's systems stored prices as an unsigned 32-bit integer with four implied decimal places, so the…

  • Language: Java
  • Layer: Backend
  • Difficulty: Easy
  • Concepts: Overflow, Money
  • Modelled on: Nasdaq · 2021
  • Visible tests: ordinary prices parse; a Berkshire Class A price parses exactly
  • Reward: 50 XP for a complete fix

Briefing

Modelled on Nasdaq and Berkshire Hathaway (May 2021): Nasdaq's systems stored prices as an unsigned 32-bit integer with four implied decimal places, so the largest price they could represent was $429,496.7295. Berkshire Hathaway's Class A shares were approaching that figure, and Nasdaq had to upgrade its systems to a wider price field.

This project is a reconstruction in Java, which has no unsigned int. Price.parse turns a decimal price into ticks of 1/10,000 — computing in int before widening to long, so large prices wrap long before the old 32-bit ceiling.

Fix Price.parse so any realistic price is represented exactly.

Bug report

BUG-BRKA · Priority: High · Reported by: market data

Price.parse(text) returns the price in ticks (1 tick = 0.0001) as a long:

  • "150.25" -> 1502500, "437131" -> 4371310000
  • up to four decimal places; missing places are zeros
  • the whole-dollar part may be any non-negative value up to at least 100,000,000 — the result must never wrap

Price.format(ticks) prints whole dollars, ".", then exactly four digits. Price.notional(ticks, quantity) = ticks * quantity, as a long.

Observed: a quote of 437131.00 parsed to 76342704 ticks — a price of 7634.2704.

Logs

[quotes] BRK.A last=437131.00 ticks=76342704
[quotes] BRK.A print rejected: move of -98% vs previous close

The code as shipped

src/quotes/Price.java (editable)

class Price {
    static final int SCALE = 10000;

    static long parse(String text) {
        String[] parts = text.split("\\.");
        int whole = Integer.parseInt(parts[0]);
        int frac = 0;
        if (parts.length > 1) {
            frac = Integer.parseInt((parts[1] + "0000").substring(0, 4));
        }
        return whole * SCALE + frac;
    }

    static String format(long ticks) {
        return (ticks / SCALE) + "." + String.format("%04d", ticks % SCALE);
    }

    static long notional(long ticks, int quantity) {
        return ticks * quantity;
    }
}

Read-only context: src/quotes/LegacyFeed.java.

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