Bird
Raised Fist0
Redisquery~20 mins

XRANGE and XREVRANGE in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of XRANGE with specific ID range
Given a Redis stream named mystream with entries:

1) ID: 1609459200000-0, fields: {"name": "Alice", "age": "30"}
2) ID: 1609459201000-0, fields: {"name": "Bob", "age": "25"}
3) ID: 1609459202000-0, fields: {"name": "Charlie", "age": "35"}

What is the output of the command:
XRANGE mystream 1609459200000-0 1609459201000-0
Redis
XRANGE mystream 1609459200000-0 1609459201000-0
A[["1609459200000-0", {"name": "Alice", "age": "30"}], ["1609459201000-0", {"name": "Bob", "age": "25"}]]
B[["1609459201000-0", {"name": "Bob", "age": "25"}], ["1609459202000-0", {"name": "Charlie", "age": "35"}]]
C[["1609459200000-0", {"name": "Alice", "age": "30"}], ["1609459202000-0", {"name": "Charlie", "age": "35"}]]
D[]
Attempts:
2 left
💡 Hint
XRANGE returns entries in the given ID range inclusive, in ascending order.
❓ query_result
intermediate
2:00remaining
Output of XREVRANGE with specific ID range
Using the same Redis stream mystream as before, what is the output of:
XREVRANGE mystream 1609459202000-0 1609459200000-0
Redis
XREVRANGE mystream 1609459202000-0 1609459200000-0
A[["1609459202000-0", {"name": "Charlie", "age": "35"}], ["1609459201000-0", {"name": "Bob", "age": "25"}], ["1609459200000-0", {"name": "Alice", "age": "30"}]]
B[["1609459200000-0", {"name": "Alice", "age": "30"}], ["1609459201000-0", {"name": "Bob", "age": "25"}]]
C[["1609459201000-0", {"name": "Bob", "age": "25"}], ["1609459202000-0", {"name": "Charlie", "age": "35"}]]
D[]
Attempts:
2 left
💡 Hint
XREVRANGE returns entries in the given ID range inclusive, in descending order.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in XRANGE usage
Which of the following XRANGE commands will cause a syntax error in Redis?
AXRANGE mystream 1609459200000-0 1609459201000-0
BXRANGE mystream 1609459200000-0
CXRANGE mystream 1609459200000-0 1609459201000-0 COUNT 2
DXRANGE mystream - +
Attempts:
2 left
💡 Hint
XRANGE requires both start and end IDs to be specified.
❓ optimization
advanced
2:00remaining
Optimizing XRANGE with COUNT
You want to fetch only the latest 3 entries from a Redis stream mystream. Which command is the most efficient?
AXRANGE mystream - + COUNT 3
BXREVRANGE mystream - + COUNT 3
CXRANGE mystream + - COUNT 3
DXREVRANGE mystream + - COUNT 3
Attempts:
2 left
💡 Hint
To get latest entries, use XREVRANGE with descending order and COUNT.
🧠 Conceptual
expert
3:00remaining
Understanding XRANGE and XREVRANGE ID boundaries
Consider a Redis stream mystream with entries having IDs:
1000-0, 1000-1, 1001-0, 1002-0.

What will be the output of:
XRANGE mystream 1000-1 1002-0

and
XREVRANGE mystream 1002-0 1000-1

respectively?
AXRANGE returns entries with IDs 1000-1, 1001-0; XREVRANGE returns entries 1002-0, 1001-0
BXRANGE returns entries with IDs 1000-0, 1000-1, 1001-0; XREVRANGE returns entries 1002-0, 1001-0, 1000-0
CXRANGE returns entries with IDs 1000-1, 1001-0, 1002-0; XREVRANGE returns entries 1002-0, 1001-0, 1000-1
DBoth commands return empty lists because the ranges are invalid
Attempts:
2 left
💡 Hint
XRANGE and XREVRANGE include the boundary IDs specified.

Practice

(1/5)
1. What does the Redis command XRANGE do when used on a stream?
easy
A. Adds new entries to the stream.
B. Reads stream entries from newest to oldest within a specified range.
C. Deletes entries from the stream within a specified range.
D. Reads stream entries from oldest to newest within a specified range.

Solution

  1. Step 1: Understand XRANGE purpose

    XRANGE is used to read entries from a Redis stream in order from the oldest to the newest.
  2. Step 2: Compare with other commands

    XREVRANGE reads in reverse order, and other commands like XADD add entries, so XRANGE is for reading oldest to newest.
  3. Final Answer:

    Reads stream entries from oldest to newest within a specified range. -> Option D
  4. Quick Check:

    XRANGE = oldest to newest read [OK]
Hint: XRANGE always reads forward (oldest to newest) [OK]
Common Mistakes:
  • Confusing XRANGE with XREVRANGE order
  • Thinking XRANGE adds or deletes entries
  • Assuming XRANGE reads newest first
2. Which of the following is the correct syntax to read the last 5 entries from a Redis stream using XREVRANGE?
easy
A. XRANGE mystream + - COUNT 5
B. XREVRANGE mystream + - COUNT 5
C. XREVRANGE mystream - + COUNT 5
D. XREVRANGE mystream 0 - COUNT 5

Solution

  1. Step 1: Understand XREVRANGE syntax

    XREVRANGE reads from the newest entry (+) to the oldest (-). To get last 5 entries, use range '+' to '-' with COUNT 5.
  2. Step 2: Check options

    XREVRANGE mystream + - COUNT 5 uses correct order '+' to '-' and COUNT 5. XREVRANGE mystream - + COUNT 5 reverses range, XRANGE mystream + - COUNT 5 uses XRANGE, and XREVRANGE mystream 0 - COUNT 5 uses invalid start ID.
  3. Final Answer:

    XREVRANGE mystream + - COUNT 5 -> Option B
  4. Quick Check:

    XREVRANGE newest to oldest with COUNT [OK]
Hint: XREVRANGE uses '+' then '-' for newest to oldest [OK]
Common Mistakes:
  • Swapping '+' and '-' in range
  • Using XRANGE instead of XREVRANGE
  • Incorrect start or end IDs
3. Given a Redis stream mystream with entries:
1609459200000-0: {"name":"Alice"}
1609459260000-0: {"name":"Bob"}
1609459320000-0: {"name":"Carol"}
What will be the result of XRANGE mystream 1609459200000-0 1609459260000-0?
medium
A. [["1609459320000-0", {"name":"Carol"}]]
B. [["1609459260000-0", {"name":"Bob"}], ["1609459200000-0", {"name":"Alice"}]]
C. [["1609459200000-0", {"name":"Alice"}], ["1609459260000-0", {"name":"Bob"}]]
D. Empty list

Solution

  1. Step 1: Understand XRANGE with ID range

    XRANGE returns entries from start ID to end ID in order from oldest to newest.
  2. Step 2: Identify matching entries

    Entries with IDs 1609459200000-0 and 1609459260000-0 fall within the range, so both are returned in order.
  3. Final Answer:

    [["1609459200000-0", {"name":"Alice"}], ["1609459260000-0", {"name":"Bob"}]] -> Option C
  4. Quick Check:

    XRANGE returns oldest to newest in range [OK]
Hint: XRANGE returns entries in ascending ID order [OK]
Common Mistakes:
  • Expecting reverse order output
  • Including entries outside the range
  • Confusing XRANGE with XREVRANGE
4. You run the command XREVRANGE mystream - + COUNT 3 on a stream with more than 3 entries but get an empty list. What is the likely cause?
medium
A. The range start and end IDs are reversed; XREVRANGE expects '+' then '-'.
B. COUNT cannot be used with XREVRANGE.
C. The stream key 'mystream' does not exist.
D. XREVRANGE requires numeric IDs only.

Solution

  1. Step 1: Check XREVRANGE range order

    XREVRANGE reads from newest to oldest, so the start ID must be '+' and end ID '-'. Using '-' then '+' reverses the order and results in an empty list.
  2. Step 2: Verify other options

    COUNT is valid with XREVRANGE, a missing key returns empty list (but stream exists with >3 entries), and special IDs like -/+ are valid.
  3. Final Answer:

    The range start and end IDs are reversed; XREVRANGE expects '+' then '-'. -> Option A
  4. Quick Check:

    XREVRANGE range must be '+' to '-' [OK]
Hint: XREVRANGE range must start with '+' and end with '-' [OK]
Common Mistakes:
  • Swapping start and end IDs in XREVRANGE
  • Assuming COUNT is invalid
  • Confusing the empty result with missing key
5. You want to retrieve the 3 newest entries from a stream mystream but only those with IDs greater than 1609459200000-0. Which command achieves this correctly?
hard
A. XREVRANGE mystream + 1609459200000-0 COUNT 3
B. XRANGE mystream 1609459200000-0 + COUNT 3
C. XREVRANGE mystream 1609459200000-0 + COUNT 3
D. XRANGE mystream + 1609459200000-0 COUNT 3

Solution

  1. Step 1: Understand the requirement

    We want the 3 newest entries with IDs greater than 1609459200000-0, so we read backwards from newest (+) down to that ID.
  2. Step 2: Choose correct command and range

    XREVRANGE reads newest to oldest, so range '+' to '1609459200000-0' with COUNT 3 gets newest entries above that ID.
  3. Step 3: Eliminate wrong options

    XRANGE reads oldest to newest, so XRANGE mystream 1609459200000-0 + COUNT 3 and D read forward. XREVRANGE mystream 1609459200000-0 + COUNT 3 reverses range order for XREVRANGE, causing error.
  4. Final Answer:

    XREVRANGE mystream + 1609459200000-0 COUNT 3 -> Option A
  5. Quick Check:

    Newest entries above ID = XREVRANGE + to ID [OK]
Hint: Use XREVRANGE + to ID for newest entries above ID [OK]
Common Mistakes:
  • Using XRANGE instead of XREVRANGE for newest entries
  • Reversing range IDs in XREVRANGE
  • Omitting COUNT to limit results
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