Bird
Raised Fist0
Redisquery~20 mins

Leaderboard implementation in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Leaderboard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Retrieve top 3 players from leaderboard

Given a Redis sorted set game_leaderboard where player scores are stored, which command returns the top 3 players with the highest scores?

Redis
ZADD game_leaderboard 1500 player1 1800 player2 1200 player3 2000 player4 1700 player5
AZREVRANGE game_leaderboard 0 2 WITHSCORES
BZRANGEBYSCORE game_leaderboard 0 3 WITHSCORES
CZRANGE game_leaderboard 0 2 WITHSCORES
DZREVRANGEBYSCORE game_leaderboard 0 3 WITHSCORES
Attempts:
2 left
💡 Hint

Remember that higher scores mean higher rank, and ZREVRANGE returns elements from highest to lowest score.

🧠 Conceptual
intermediate
2:00remaining
Understanding score updates in Redis leaderboard

Which Redis command correctly increases the score of player3 by 300 points in the game_leaderboard sorted set?

AZADD game_leaderboard player3 300
BZADD game_leaderboard 300 player3
CZINCRBY player3 300 game_leaderboard
DZINCRBY game_leaderboard 300 player3
Attempts:
2 left
💡 Hint

Look for the command that increments a member's score by a given amount.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in leaderboard score retrieval

Which option contains a syntax error when trying to get the rank of player2 in game_leaderboard?

AZRANK game_leaderboard player2
BZREVRANK game_leaderboard player2
CZRANK player2 game_leaderboard
D2reyalp draobredael_emag KNARVERZ
Attempts:
2 left
💡 Hint

Check the order of arguments for the rank commands.

❓ optimization
advanced
2:00remaining
Efficiently retrieve players ranked 5 to 10 with scores

Which Redis command efficiently retrieves players ranked from 5 to 10 (inclusive) with their scores from game_leaderboard, sorted from highest to lowest score?

AZREVRANGE game_leaderboard 4 9 WITHSCORES
BZREVRANGEBYSCORE game_leaderboard 10 5 WITHSCORES
CZRANGEBYSCORE game_leaderboard 5 10 WITHSCORES
DZRANGE game_leaderboard 4 9 WITHSCORES
Attempts:
2 left
💡 Hint

Remember that ranks start at 0 and ZREVRANGE returns highest scores first.

🔧 Debug
expert
3:00remaining
Why does this leaderboard update fail?

Consider this Redis command intended to update player6's score to 2500 in game_leaderboard:

ZINCRBY game_leaderboard 2500 player6

Why might this command not set player6's score to exactly 2500?

ABecause <code>ZINCRBY</code> requires the member name before the increment value
BBecause <code>ZINCRBY</code> adds 2500 to the existing score instead of setting it directly
CBecause <code>ZINCRBY</code> only works if the member already exists
DBecause <code>ZINCRBY</code> cannot be used on sorted sets
Attempts:
2 left
💡 Hint

Think about what ZINCRBY does to the score value.

Practice

(1/5)
1. What Redis data structure is best suited for implementing a leaderboard where members have scores?
easy
A. Hash
B. Sorted Set
C. List
D. String

Solution

  1. Step 1: Understand leaderboard requirements

    A leaderboard needs to store members with scores and retrieve them sorted by score.
  2. Step 2: Identify Redis data structure

    Redis Sorted Sets store members with scores and keep them sorted automatically.
  3. Final Answer:

    Sorted Set -> Option B
  4. Quick Check:

    Leaderboard = Sorted Set [OK]
Hint: Leaderboards need sorted data, use Sorted Sets [OK]
Common Mistakes:
  • Choosing List which is ordered by insertion, not score
  • Using Hash which stores key-value but not sorted by score
  • Using String which holds plain text, not multiple members
2. Which Redis command correctly adds or updates a member's score in a leaderboard stored as a sorted set?
easy
A. ZADD leaderboard 100 user1
B. ZADD leaderboard user1 100
C. ZINCRBY leaderboard 100 user1
D. ZREM leaderboard user1

Solution

  1. Step 1: Recall ZADD syntax

    ZADD command syntax is: ZADD key score member.
  2. Step 2: Match syntax to options

    ZADD leaderboard 100 user1 uses ZADD leaderboard 100 user1 which matches the correct syntax.
  3. Final Answer:

    ZADD leaderboard 100 user1 -> Option A
  4. Quick Check:

    ZADD syntax = ZADD key score member [OK]
Hint: ZADD syntax: key, score, then member [OK]
Common Mistakes:
  • Swapping score and member order
  • Using ZINCRBY which increments score, not sets it
  • Using ZREM which removes member instead of adding
3. Given the following Redis commands executed in order:
ZADD leaderboard 50 user1
ZADD leaderboard 100 user2
ZADD leaderboard 75 user3
ZREVRANGE leaderboard 0 1 WITHSCORES
What is the output of the last command?
medium
A. ["user2", "100", "user3", "75"]
B. ["user1", "50", "user3", "75"]
C. ["user3", "75", "user2", "100"]
D. ["user2", "100", "user1", "50"]

Solution

  1. Step 1: Add members with scores

    user1=50, user2=100, user3=75 added to sorted set.
  2. Step 2: Use ZREVRANGE 0 1 WITHSCORES

    This fetches top 2 members by highest score descending: user2(100), user3(75).
  3. Final Answer:

    ["user2", "100", "user3", "75"] -> Option A
  4. Quick Check:

    Top 2 by score desc = user2, user3 [OK]
Hint: ZREVRANGE 0 1 gets top 2 highest scores [OK]
Common Mistakes:
  • Confusing ZREVRANGE with ZRANGE (ascending order)
  • Ignoring WITHSCORES option output format
  • Mixing member and score order in output
4. You run the following Redis commands:
ZADD leaderboard 30 user1
ZADD leaderboard 60 user2
ZADD leaderboard 45 user3
ZREVRANGE leaderboard 0 2
But the output is empty. What is the most likely cause?
medium
A. ZREVRANGE returns ascending order by default
B. ZREVRANGE syntax is incorrect, it needs WITHSCORES
C. ZADD commands used wrong order of arguments
D. The leaderboard key was deleted before ZREVRANGE

Solution

  1. Step 1: Check command correctness

    ZADD commands have correct syntax and add members with scores.
  2. Step 2: Analyze empty output from ZREVRANGE

    Empty output means the key 'leaderboard' likely does not exist or was deleted before ZREVRANGE.
  3. Final Answer:

    The leaderboard key was deleted before ZREVRANGE -> Option D
  4. Quick Check:

    Empty output = missing key [OK]
Hint: Empty ZREVRANGE means key missing or deleted [OK]
Common Mistakes:
  • Thinking WITHSCORES is required for output (it's optional)
  • Assuming ZREVRANGE returns ascending order
  • Believing ZADD argument order was wrong (it was correct)
5. You want to implement a leaderboard that shows the top 3 users but excludes any user with a score below 50. Which Redis commands or approach should you use?
hard
A. Use ZRANGEBYSCORE leaderboard 50 +inf LIMIT 0 3
B. Use ZREVRANGE leaderboard 0 2 and filter scores < 50 in application
C. Use ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3
D. Use ZRANGE leaderboard 0 2 WITHSCORES and exclude scores < 50

Solution

  1. Step 1: Understand filtering by score

    We want top 3 users with scores >= 50, sorted descending.
  2. Step 2: Choose correct Redis command

    ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3 returns members with scores between +inf and 50 descending, limited to 3.
  3. Final Answer:

    Use ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3 -> Option C
  4. Quick Check:

    Filter by score with ZREVRANGEBYSCORE [OK]
Hint: Use ZREVRANGEBYSCORE with score limits and LIMIT [OK]
Common Mistakes:
  • Using ZREVRANGE without score filtering
  • Using ZRANGEBYSCORE which sorts ascending, not descending
  • Filtering scores in app instead of Redis command
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard