First of all, we can have below schema for the players and its score:
contest_id(partitionKey), user_id1(sortKey), problem1_finish_time, problem2_finish_time, score(secondaryIndex)
For the score and user_id, we can build a BST. Let’s say we want to find the players who ranks between 50-60. We can find the 50th player in O(logN) time. Check this leetcode problem. Then, we can do extra 10 range scan based on the searched element.
Implementation can be done with redis ZADD, ZRANGE
Below is the command we
0.0.0.0:6379> zadd players 400 d (integer) 1 0.0.0.0:6379> zadd players 500 e (integer) 1
Here is a full list of the result with scores:
0.0.0.0:6379> zrange players 0 -1 withscores 1) “b” 2) “100” 3) “a” 4) “200” 5) “c” 6) “300” 7) “d” 8) “400” 9) “e” 10) “500”
By running ZRANGE board_name from to, it returns the result:
0.0.0.0:6379> zrange players 2 4 1) "c" 2) "d" 3) "e"
Check this post https://redislabs.com/solutions/use-cases/leaderboards/