Build a Redis-style sorted set on a skip list: rank with spans, ZRANGE and ZRANGEBYSCORE with exclusive bounds
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
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 3
- code listings
- 6
- edge cases
- 5
- follow-up questions
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1Keep a Map from member to score for O(1) score lookups, and a skip list ordered by (score, member) for everything about order. Updating a score is remove plus insert.
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.