Parcel Label Numbers
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 timet. Iftis later than the last ID's time (or nothing was issued yet), useseq = 0. Iftequals it, use the last seq plus 1. Return-1and change nothing if the seq would reach 256 (this millisecond is used up) ortis 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
new LabelIds(5) .next(1000) .next(1000) .next(1001) .parts(4097281)
[null, 4097280, 4097281, 4101376, [1000, 5, 1]]
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.
new LabelIds(0) .next(2000) .next(1999) .next(2000) .next(2001)
[null, 8192000, -1, 8192001, 8196096]
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- Hint 1
If two depots print a label in the same millisecond, what keeps their IDs apart? And if one depot prints two?
- Hint 2
The machine number separates depots; the seq separates one depot's labels within a millisecond. So the generator only remembers the last ID's time and its seq.
- Hint 3
Multiplying by 4096 (212) shifts the time left 12 bits; multiplying the machine by 256 (28) shifts it left 8. To split, divide by 4096 and round down for
t; the remainder holds the rest. - Hint 4
In
next: ift < lastreturn -1; ift === lastand the seq is already 255 return -1; otherwise set the seq (0 or last + 1), storet, and build the ID. Store nothing before both checks pass.
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.
How to solve it
Each field gets its own range of bits, so IDs that differ in time, depot or seq can never be equal. Multiplying by a power of two is a left shift; division and remainder undo it.
Trace the first example, machine 5:
next(1000): nothing issued yet, so seq 0. ID = 1000 × 4096 + 5 × 256 + 0 = 4,097,280.next(1000): same millisecond, seq 1. ID = 4,097,281.next(1001): later millisecond, seq back to 0. ID = 4,100,096 + 1,280 = 4,101,376.parts(4097281): 4,097,281 ÷ 4096 = 1000 remainder 1,281; 1,281 ÷ 256 = 5 remainder 1. Answer[1000, 5, 1].
Both refusals must leave the state untouched, or a refused call would burn a seq or move the clock. Also, IDs sort by time only roughly: depot clocks differ by a few milliseconds, so order across depots within that skew is arbitrary. Real generators wait for the next millisecond instead of failing; here the caller decides.
const TIME_SHIFT = 4096; // 2^12: room for 4 machine bits + 8 seq bits
const MACHINE_SHIFT = 256; // 2^8: room for 8 seq bits
const MAX_SEQ = 255;
class LabelIds {
constructor(machine) {
this.machine = machine;
this.lastT = -1; // nothing issued yet
this.seq = 0;
}
next(t) {
if (t < this.lastT) return -1; // clock moved backwards
if (t === this.lastT && this.seq === MAX_SEQ) return -1; // millisecond used up
this.seq = t === this.lastT ? this.seq + 1 : 0;
this.lastT = t;
// multiply, not <<: JavaScript bit operators are 32-bit
return t * TIME_SHIFT + this.machine * MACHINE_SHIFT + this.seq;
}
parts(id) {
const t = Math.floor(id / TIME_SHIFT);
const rest = id % TIME_SHIFT;
return [t, Math.floor(rest / MACHINE_SHIFT), rest % MACHINE_SHIFT];
}
}TIME_SHIFT = 4096 # 2^12: room for 4 machine bits + 8 seq bits
MACHINE_SHIFT = 256 # 2^8: room for 8 seq bits
MAX_SEQ = 255
class LabelIds:
def __init__(self, machine):
self.machine = machine
self.last_t = -1 # nothing issued yet
self.seq = 0
def next(self, t):
if t < self.last_t: # clock moved backwards
return -1
if t == self.last_t and self.seq == MAX_SEQ: # millisecond used up
return -1
self.seq = self.seq + 1 if t == self.last_t else 0
self.last_t = t
return t * TIME_SHIFT + self.machine * MACHINE_SHIFT + self.seq
def parts(self, id):
t, rest = divmod(id, TIME_SHIFT)
return [t, rest // MACHINE_SHIFT, rest % MACHINE_SHIFT]Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.