~learn.
0:00 / 1:16
Interview problem · Leaderboard

Two players on 2,210 points. Who ranks higher?

transcript

Two players. Both on 2,210 points. Your leaderboard has to put one above the other. Which one?

The usual rule: same points, whoever got there first ranks higher. But a sorted set orders members by one number. The score. Nothing else.

So pack both facts into that one number. Multiply the points by 2 to the 22. Then add the time left in the season when they reached it. The earlier you got there, the more time was left. So the bigger the number. Higher now means more points. Or the same points, earlier.

But here's the catch. A sorted-set score is a 64-bit float. Pack too much in, and it rounds. Then the tie-break is garbage.

A 64-bit float holds integers exactly up to 53 bits. 2 to the 22 seconds is about 48 days. Monthly seasons fit. Keep points well under 2 to the 31. 31 plus 22 is 53. Exact.

No second sort. No extra lookup. One number carries both. Points first, time second, packed into one exact float.

Full breakdown in the lecture.