Bird
Raised Fist0
Redisquery~20 mins

ZRANGE and ZREVRANGE for reading in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sorted Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this ZRANGE command?
Given a sorted set myzset with elements {"one":1, "two":2, "three":3, "four":4}, what does the command ZRANGE myzset 1 2 return?
Redis
ZRANGE myzset 1 2
A["four", "three"]
B["three", "four"]
C["two", "three"]
D["one", "two"]
Attempts:
2 left
💡 Hint
ZRANGE returns elements by rank starting at 0, inclusive.
❓ query_result
intermediate
2:00remaining
What is the output of this ZREVRANGE command?
Given a sorted set myzset with elements {"one":1, "two":2, "three":3, "four":4}, what does the command ZREVRANGE myzset 0 1 return?
Redis
ZREVRANGE myzset 0 1
A["four", "three"]
B["one", "two"]
C["three", "two"]
D["two", "one"]
Attempts:
2 left
💡 Hint
ZREVRANGE returns elements by rank in descending order starting at 0.
📝 Syntax
advanced
2:00remaining
Which command correctly returns elements with scores included?
You want to get elements and their scores from myzset using ZRANGE. Which command is correct?
AZRANGE myzset 0 -1 WITHSCORE
BZRANGE myzset 0 -1 WITHSCORES
CZRANGE myzset 0 -1 WITH SCORES
DZRANGE myzset 0 -1 SCORES
Attempts:
2 left
💡 Hint
The option to include scores is a single word.
❓ optimization
advanced
2:00remaining
How to efficiently get the top 3 highest scored elements with scores?
Which command is the most efficient to get the top 3 elements with highest scores and their scores from myzset?
AZREVRANGE myzset 0 2 WITHSCORES
BZRANGE myzset -3 -1 WITHSCORES
CZRANGE myzset 0 2 WITHSCORES
DZREVRANGE myzset -3 -1 WITHSCORES
Attempts:
2 left
💡 Hint
Top highest means descending order starting at 0.
🧠 Conceptual
expert
2:00remaining
What happens if you use negative indexes in ZRANGE?
Given myzset with 5 elements, what does ZRANGE myzset -3 -1 return?
Redis
ZRANGE myzset -3 -1
AReturns an empty list
BReturns the first 3 elements in ascending order
CRaises a syntax error
DReturns the last 3 elements in ascending order
Attempts:
2 left
💡 Hint
Negative indexes count from the end backwards.

Practice

(1/5)
1. What does the Redis command ZRANGE myset 0 2 do?
easy
A. Deletes the first three elements from the sorted set 'myset'.
B. Returns the first three elements with the lowest scores from the sorted set 'myset'.
C. Returns the last three elements with the highest scores from the sorted set 'myset'.
D. Returns all elements from the sorted set 'myset' without any order.

Solution

  1. Step 1: Understand ZRANGE command behavior

    ZRANGE returns elements in ascending order by score, from the start index to the end index.
  2. Step 2: Apply the index range 0 to 2

    Indexes 0 to 2 mean the first three elements with the lowest scores are returned.
  3. Final Answer:

    Returns the first three elements with the lowest scores from the sorted set 'myset'. -> Option B
  4. Quick Check:

    ZRANGE with 0 2 = first 3 lowest scored items [OK]
Hint: ZRANGE gets items low to high by index range [OK]
Common Mistakes:
  • Confusing ZRANGE with ZREVRANGE order
  • Thinking it deletes elements
  • Assuming it returns all elements unordered
2. Which of the following is the correct syntax to get the top 5 highest scored elements from a sorted set named 'scores'?
easy
A. ZREVRANGE scores 0 4
B. ZRANGE scores -5 -1
C. ZRANGE scores 0 4 WITHSCORES
D. ZREVRANGE scores 5 0

Solution

  1. Step 1: Identify command for highest scores first

    ZREVRANGE returns elements from highest to lowest score.
  2. Step 2: Use index range 0 to 4 for top 5 elements

    Indexes 0 to 4 select the first 5 elements in descending order.
  3. Final Answer:

    ZREVRANGE scores 0 4 -> Option A
  4. Quick Check:

    Top 5 highest = ZREVRANGE 0 4 [OK]
Hint: Use ZREVRANGE 0 N-1 for top N highest scores [OK]
Common Mistakes:
  • Using negative indexes incorrectly with ZRANGE
  • Confusing ZRANGE and ZREVRANGE
  • Wrong index order like 5 0
3. Given a sorted set 'players' with elements and scores: {"Alice":10, "Bob":20, "Carol":15}, what is the output of ZRANGE players 0 1 WITHSCORES?
medium
A. ["Bob", "20", "Carol", "15"]
B. ["Alice", "10", "Bob", "20"]
C. ["Carol", "15", "Bob", "20"]
D. ["Alice", "10", "Carol", "15"]

Solution

  1. Step 1: Sort elements by score ascending

    Sorted order by score: Alice(10), Carol(15), Bob(20).
  2. Step 2: Select indexes 0 to 1 with scores

    Indexes 0 and 1 correspond to Alice and Carol with their scores.
  3. Final Answer:

    ["Alice", "10", "Carol", "15"] -> Option D
  4. Quick Check:

    ZRANGE 0 1 WITHSCORES = Alice and Carol with scores [OK]
Hint: ZRANGE returns low to high; WITHSCORES adds scores after each element [OK]
Common Mistakes:
  • Mixing order of elements
  • Forgetting WITHSCORES doubles output
  • Assuming ZREVRANGE order
4. You run ZREVRANGE leaderboard 0 2 but get unexpected results. Which of these is a likely cause?
medium
A. ZREVRANGE does not support index ranges.
B. You used ZRANGE instead of ZREVRANGE.
C. The sorted set 'leaderboard' is empty or does not exist.
D. The index range 0 2 is invalid for ZREVRANGE.

Solution

  1. Step 1: Check if sorted set exists and has elements

    If 'leaderboard' is empty or missing, ZREVRANGE returns empty result.
  2. Step 2: Validate command and index usage

    ZREVRANGE supports index ranges; 0 2 is valid; using ZRANGE instead would change order but not cause empty result.
  3. Final Answer:

    The sorted set 'leaderboard' is empty or does not exist. -> Option C
  4. Quick Check:

    Empty set causes empty ZREVRANGE output [OK]
Hint: Empty or missing set returns empty list [OK]
Common Mistakes:
  • Thinking index range is invalid
  • Confusing ZRANGE and ZREVRANGE effects
  • Believing ZREVRANGE lacks index support
5. You want to get the middle 3 elements by score from a sorted set 'scores' with 10 elements, ordered from lowest to highest. Which command correctly retrieves elements ranked 4th to 6th (0-based index) with their scores?
hard
A. ZRANGE scores 3 5 WITHSCORES
B. ZREVRANGE scores 3 5 WITHSCORES
C. ZRANGE scores 4 6 WITHSCORES
D. ZREVRANGE scores 4 6 WITHSCORES

Solution

  1. Step 1: Identify index range for middle 3 elements

    Elements ranked 4th to 6th correspond to indexes 3, 4, 5 (0-based).
  2. Step 2: Use ZRANGE for ascending order and WITHSCORES to get scores

    ZRANGE scores 3 5 WITHSCORES returns elements at indexes 3 to 5 with their scores.
  3. Final Answer:

    ZRANGE scores 3 5 WITHSCORES -> Option A
  4. Quick Check:

    Middle 3 elements = ZRANGE 3 5 WITHSCORES [OK]
Hint: ZRANGE with correct indexes gets middle elements low to high [OK]
Common Mistakes:
  • Using ZREVRANGE which reverses order
  • Off-by-one errors in index range
  • Confusing rank with index
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