Parcel Label Numbers

MediumdesignAlso known as Snowflake ID Generator

A courier prints a barcode number on every parcel at up to 16 depots. Numbers must never repeat, depots cannot call a central server per label (the warehouse network drops out), and sorting by number should roughly sort by time. So each depot builds the number from three parts: the time in milliseconds, its machine number, and a counter for labels printed in the same millisecond.

This is the Snowflake-style generator from Unique IDs. Real layouts use 64 bits; this one fits in 53 bits so every ID is an exact JavaScript number and survives JSON in a browser.

Your task

An ID is t × 4096 + machine × 256 + seq, which corresponds to this bit layout:

  • The top 41 bits hold t, the milliseconds since the courier's chosen epoch (0 to 241 − 1, about 69 years).
  • The next 4 bits hold machine, the depot number, from 0 to 15.
  • The lowest 8 bits hold seq, from 0 to 255, which counts the IDs made in the same millisecond.

Write a class LabelIds with these methods:

  • constructor(machine) creates a generator for one depot.
  • next(t): return a new ID for time t. If t is later than the last ID's time (or nothing was issued yet), use seq = 0. If t equals it, use the last seq plus 1. Return -1 and change nothing if the seq would reach 256 (this millisecond is used up) or t is earlier than the last ID's time (the clock moved backwards).
  • parts(id): split any valid ID into [t, machine, seq].

JavaScript's bit operators (<<, >>, &) work on 32 bits only, so they cannot handle a 53-bit ID. Use multiplication, division and remainder.

class LabelIds

Examples

Example 1
Input
new LabelIds(5)
.next(1000)
.next(1000)
.next(1001)
.parts(4097281)
Output
[null, 4097280, 4097281, 4101376, [1000, 5, 1]]
Why

Machine 5 adds 5 × 256 = 1,280. The first ID at t = 1000 is 4,096,000 + 1,280 = 4,097,280 (seq 0); the second in that millisecond is 4,097,281 (seq 1). At t = 1001 the seq restarts at 0: 4,100,096 + 1,280 = 4,101,376. Splitting 4,097,281: ÷ 4096 rounds down to 1000, remainder 1,281, and 1,281 ÷ 256 = 5 remainder 1.

Example 2
Input
new LabelIds(0)
.next(2000)
.next(1999)
.next(2000)
.next(2001)
Output
[null, 8192000, -1, 8192001, 8196096]
Why

After an ID at t = 2000, the clock reports 1999. An ID at 1999 could repeat one already issued, so the generator returns -1 and keeps its state. Back at 2000 it continues with seq 1; at 2001 it restarts from 0.

Constraints

  • 0 ≤ machine ≤ 15
  • 0 ≤ t ≤ 2,199,023,255,551 (2⁴¹ − 1)
  • parts is only called with IDs that this layout can produce, so 0 ≤ id ≤ 9,007,199,254,740,991
  • At most 1,000 calls per test
Start with brute force

The simplest unique number is a shared counter in one database. That needs a network call per label and stops every depot when the counter is unreachable, which the courier cannot accept.

Random 53-bit numbers need no coordination, but they do not sort by time, and with enough labels two will collide. Time, machine and sequence give uniqueness and rough time order with no network call.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code