0
0
Redisquery~20 mins

Top-N queries in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Top-N Redis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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
AZREVRANGE game_scores 0 2 WITHSCORES
BZRANGEBYSCORE game_scores 0 3 WITHSCORES
CZRANGE game_scores 0 2 WITHSCORES
DZREVRANGEBYSCORE game_scores 3 0 WITHSCORES
Attempts:
2 left
💡 Hint
Remember that sorted sets are ordered by score ascending by default. Use the command that retrieves elements in descending order.
📝 Syntax
intermediate
2: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?
AZRANGE leaderboard 0 5
BZRANGEBYSCORE leaderboard 5 -inf
CZREVRANGE leaderboard 1 5
DZREVRANGE leaderboard 0 4
Attempts:
2 left
💡 Hint
Indexes in Redis commands start at 0 and are inclusive. Use the command that returns elements in descending order.
optimization
advanced
2: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?
AZRANGEBYSCORE scores 50 +inf LIMIT 0 10
BZREVRANGEBYSCORE scores +inf 50 LIMIT 0 10
CZREVRANGEBYSCORE scores 50 +inf LIMIT 0 10
DZRANGEBYSCORE scores +inf 50 LIMIT 0 10
Attempts:
2 left
💡 Hint
To get top elements with scores above 50, use reverse range by score from highest to lowest.
🔧 Debug
advanced
2: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?
AThe sorted set <code>players</code> is empty.
BThe LIMIT clause is invalid and causes no results.
CThe min and max scores are reversed; max should be greater than min.
DZREVRANGEBYSCORE does not support LIMIT.
Attempts:
2 left
💡 Hint
Check the order of min and max scores in the command.
🧠 Conceptual
expert
3: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?
AZRANGEBYLEX users - + LIMIT 0 5
BZRANGEBYSCORE users 0 0 LIMIT 0 5
CZREVRANGEBYLEX users + - LIMIT 0 5
DZREVRANGEBYSCORE users 0 0 LIMIT 0 5
Attempts:
2 left
💡 Hint
Use lexicographical range commands when scores are equal.