Bird
Raised Fist0
Redisquery~20 mins

ZRANGEBYSCORE for score-based queries in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ZRANGEBYSCORE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of ZRANGEBYSCORE with inclusive range
Given a sorted set 'players' with scores: {"Alice": 50, "Bob": 70, "Charlie": 70, "Diana": 90}, what is the output of the command ZRANGEBYSCORE players 50 70?
A["Alice", "Bob", "Charlie"]
B["Alice", "Diana"]
C["Bob", "Charlie"]
D["Alice", "Bob"]
Attempts:
2 left
💡 Hint
ZRANGEBYSCORE includes elements with scores equal to the min and max by default.
❓ query_result
intermediate
2:00remaining
ZRANGEBYSCORE with exclusive min and max
What is the result of ZRANGEBYSCORE players (50 (90 on the same sorted set as before?
A["Bob", "Charlie"]
B["Alice", "Bob", "Charlie"]
C["Diana"]
D[]
Attempts:
2 left
💡 Hint
Parentheses before a score mean exclusive range boundary.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in ZRANGEBYSCORE usage
Which option contains a syntax error when trying to get elements with scores between 10 and 20?
AZRANGEBYSCORE myset 10 (20
BZRANGEBYSCORE myset (10 20
CZRANGEBYSCORE myset 10 20 (LIMIT 0 5)
DZRANGEBYSCORE myset 10 20
Attempts:
2 left
💡 Hint
Check the syntax for LIMIT usage in ZRANGEBYSCORE.
❓ optimization
advanced
2:00remaining
Optimizing ZRANGEBYSCORE for large sorted sets
You want to retrieve only the first 3 elements with scores between 100 and 200 from a large sorted set. Which command is the most efficient?
AZRANGEBYSCORE myset (100 (200 LIMIT 0 3
BZRANGEBYSCORE myset 100 200 LIMIT 0 3
CZRANGEBYSCORE myset 100 200
DZRANGEBYSCORE myset 100 200 LIMIT 3 0
Attempts:
2 left
💡 Hint
LIMIT offset count syntax helps reduce data transferred.
🧠 Conceptual
expert
2:00remaining
Understanding ZRANGEBYSCORE with negative and positive infinity
What is the output of ZRANGEBYSCORE myset -inf +inf on a sorted set with elements {"a": 1, "b": 2, "c": 3}?
A["a"]
B[]
C["c", "b", "a"]
D["a", "b", "c"]
Attempts:
2 left
💡 Hint
Negative and positive infinity include all scores.

Practice

(1/5)
1. What does the Redis command ZRANGEBYSCORE do?
easy
A. It returns the rank of a member in a sorted set.
B. It deletes members from a sorted set by score.
C. It adds a member with a score to a sorted set.
D. It returns members in a sorted set within a given score range.

Solution

  1. Step 1: Understand the command purpose

    ZRANGEBYSCORE is used to get members from a sorted set based on their scores.
  2. Step 2: Compare options with command behavior

    Only It returns members in a sorted set within a given score range. correctly describes returning members within a score range.
  3. Final Answer:

    It returns members in a sorted set within a given score range. -> Option D
  4. Quick Check:

    ZRANGEBYSCORE = returns members by score range [OK]
Hint: ZRANGEBYSCORE fetches by score range, not by rank or deletion [OK]
Common Mistakes:
  • Confusing ZRANGEBYSCORE with ZREM (deletion)
  • Thinking it returns rank instead of members
  • Mixing it up with ZADD (adding members)
2. Which of the following is the correct syntax to get members with scores between 10 and 20 from sorted set myzset including their scores?
easy
A. ZRANGEBYSCORE myzset 10 20 INCLSCORES
B. ZRANGEBYSCORE myzset 10 20 WITHSCORE
C. ZRANGEBYSCORE myzset 10 20 WITHSCORES
D. ZRANGEBYSCORE myzset 10 20 SCORES

Solution

  1. Step 1: Recall correct option for including scores

    The correct keyword to include scores in output is WITHSCORES.
  2. Step 2: Check syntax correctness

    ZRANGEBYSCORE myzset 10 20 WITHSCORES uses the exact correct syntax: ZRANGEBYSCORE myzset 10 20 WITHSCORES.
  3. Final Answer:

    ZRANGEBYSCORE myzset 10 20 WITHSCORES -> Option C
  4. Quick Check:

    WITHSCORES keyword includes scores [OK]
Hint: Use WITHSCORES exactly to get scores with members [OK]
Common Mistakes:
  • Misspelling WITHSCORES
  • Using WITHSCORE or SCORES instead
  • Omitting WITHSCORES when scores are needed
3. Given the sorted set myzset with members and scores:
"a"=5, "b"=10, "c"=15, "d"=20, "e"=25
What is the result of ZRANGEBYSCORE myzset 10 20 LIMIT 1 2?
medium
A. ["c", "d"]
B. ["b", "c", "d"]
C. ["b", "c"]
D. ["a", "b"]

Solution

  1. Step 1: Identify members in score range 10 to 20

    Members with scores 10, 15, 20 are "b", "c", "d".
  2. Step 2: Apply LIMIT 1 2

    LIMIT 1 2 means skip 1 member, then return next 2 members. Skipping "b", next two are "c", "d".
  3. Final Answer:

    ["c", "d"] -> Option A
  4. Quick Check:

    LIMIT skips then returns count [OK]
Hint: LIMIT offset count skips offset, returns count members [OK]
Common Mistakes:
  • Ignoring LIMIT offset and returning from start
  • Confusing score range with member count
  • Including members outside score range
4. You run the command ZRANGEBYSCORE myzset (10 20 but get an error. What is the likely cause?
medium
A. Syntax error due to missing closing parenthesis after 20.
B. Incorrect use of parentheses for exclusive range; missing closing parenthesis.
C. Command is missing the key name.
D. Missing closing parenthesis for exclusive range syntax.

Solution

  1. Step 1: Analyze the command syntax

    The command uses (10 to indicate exclusive minimum score 10, which is correct syntax.
  2. Step 2: Identify the error

    The command ends with 20 without closing parenthesis or space, causing syntax error.
  3. Final Answer:

    Syntax error due to missing closing parenthesis after 20. -> Option A
  4. Quick Check:

    Exclusive range needs parentheses on score values [OK]
Hint: Check parentheses carefully for exclusive score ranges [OK]
Common Mistakes:
  • Forgetting parentheses for exclusive ranges
  • Missing spaces between arguments
  • Using parentheses incorrectly around scores
5. You want to get the top 3 members with scores between 0 and 100 from sorted set myzset, including their scores, but only if their score is greater than 50. Which command achieves this?
hard
A. ZRANGEBYSCORE myzset 50 100 WITHSCORES LIMIT 0 3
B. ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3
C. ZRANGEBYSCORE myzset [50 100 WITHSCORES LIMIT 0 3
D. ZRANGEBYSCORE myzset (50 100 LIMIT 3 WITHSCORES

Solution

  1. Step 1: Define score range with exclusive lower bound

    To get scores greater than 50, use (50 for exclusive minimum.
  2. Step 2: Include scores and limit results

    Use WITHSCORES to include scores and LIMIT 0 3 to get top 3 members.
  3. Step 3: Verify command syntax

    ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3 correctly uses ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3.
  4. Final Answer:

    ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3 -> Option B
  5. Quick Check:

    Exclusive range + WITHSCORES + LIMIT = correct command [OK]
Hint: Use (score for exclusive, WITHSCORES, then LIMIT offset count [OK]
Common Mistakes:
  • Using inclusive 50 instead of exclusive
  • Placing LIMIT before WITHSCORES
  • Using wrong bracket types for exclusive range
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