Build a Redis-style sorted set on a skip list: rank with spans, ZRANGE and ZRANGEBYSCORE with exclusive bounds

hardAbout 65 minutesUpdated CodingProjavascriptdata-structuresskip-listleaderboardredisranking
Not solved

Share this question

Problem

Game leaderboards, trending lists and rate limiters often sit on a Redis sorted set: members with numeric scores, kept in order, with fast "what is my rank?" and "give me ranks 0 to 9" queries. Redis implements it (beyond a small size, where it uses a compact list) with a hash table plus a skip list whose links store spans, which makes rank queries O(log n). Build the same structure.

const board = new SortedSet();
board.add("asha", 300);
board.add("ravi", 150);
board.add("meera", 300);
board.rank("meera"); // 2 (after ravi 150 and asha 300, which sorts before meera)
board.range(0, -1, { rev: true }); // ["meera", "asha", "ravi"]
board.rangeByScore("(150", "+inf"); // ["asha", "meera"]

Ref. UR-1448 · © UIRound · uiround.com/questions/build-a-redis-style-sorted-set-on-a-skip-list-rank-with-spans-zrange-and-zrangebyscore-with-exclusive-bounds

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment