Challenge - 5 Problems
Top-N Redis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Retrieve top 3 highest scores from a sorted set
Given a Redis sorted set
game_scores with player scores, which command returns the top 3 players with the highest scores?Redis
ZADD game_scores 50 player1 70 player2 60 player3 90 player4 80 player5
Attempts:
2 left
💡 Hint
Remember that sorted sets are ordered by score ascending by default. Use the command that retrieves elements in descending order.
✗ Incorrect
ZRANGE returns elements with lowest scores first. ZREVRANGE returns elements with highest scores first. To get top 3 highest scores, use ZREVRANGE with indexes 0 to 2.
📝 Syntax
intermediate2:00remaining
Identify the correct syntax to get top 5 elements by score
Which of the following Redis commands correctly retrieves the top 5 elements with the highest scores from a sorted set named
leaderboard?Attempts:
2 left
💡 Hint
Indexes in Redis commands start at 0 and are inclusive. Use the command that returns elements in descending order.
✗ Incorrect
ZREVRANGE leaderboard 0 4 returns elements ranked 1 to 5 by highest score. Other options either use wrong indexes or wrong commands.
❓ optimization
advanced2:30remaining
Efficiently retrieve top 10 elements with scores above 50
You want to get the top 10 elements from a sorted set
scores where scores are greater than 50. Which command is the most efficient and correct?Attempts:
2 left
💡 Hint
To get top elements with scores above 50, use reverse range by score from highest to lowest.
✗ Incorrect
ZREVRANGEBYSCORE with max +inf and min 50 returns elements with scores >= 50 in descending order. LIMIT 0 10 restricts to top 10. Other options either use wrong order or wrong min/max.
🔧 Debug
advanced2:00remaining
Why does this top-N query return empty?
You run the command
ZREVRANGEBYSCORE players 100 50 LIMIT 0 5 on a sorted set players but get an empty result. What is the reason?Attempts:
2 left
💡 Hint
Check the order of min and max scores in the command.
✗ Incorrect
ZREVRANGEBYSCORE expects max score first, then min score. Here 100 is max and 50 is min, which is correct. But if the scores are reversed, it returns empty. The command is correct but if the scores are reversed, no results appear.
🧠 Conceptual
expert3:00remaining
Understanding Top-N queries with Redis sorted sets and lexicographical order
You have a sorted set
users where scores are all equal (e.g., 0), but you want to get the top 5 users sorted by their names alphabetically. Which command achieves this?Attempts:
2 left
💡 Hint
Use lexicographical range commands when scores are equal.
✗ Incorrect
ZRANGEBYLEX returns elements in lex order when scores are equal. '-' and '+' represent min and max lex range. LIMIT 0 5 returns first 5 elements alphabetically. Other options use score ranges or reverse order which do not sort lexicographically ascending.