Bird
Raised Fist0
Redisquery~20 mins

Why sorted sets combine uniqueness with ordering in Redis - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Why do Redis sorted sets enforce uniqueness of members?
Redis sorted sets require each member to be unique. Why is this important for their functionality?
ATo store duplicate members with different scores
BTo allow multiple scores for the same member
CTo ensure each member has a single, well-defined score for ordering
DTo improve network transfer speed by compressing duplicates
Attempts:
2 left
💡 Hint
Think about how ordering works when members can have scores.
❓ query_result
intermediate
2:00remaining
What is the output of ZRANGE with scores on a sorted set?
Given a sorted set with members and scores, what does the command `ZRANGE myset 0 -1 WITHSCORES` return?
Redis
ZADD myset 10 "apple" 20 "banana" 15 "cherry"
ZRANGE myset 0 -1 WITHSCORES
A["apple", "10", "cherry", "15", "banana", "20"]
B["banana", "20", "cherry", "15", "apple", "10"]
C["cherry", "15", "apple", "10", "banana", "20"]
D["apple", "10", "banana", "20", "cherry", "15"]
Attempts:
2 left
💡 Hint
ZRANGE returns members ordered by score from lowest to highest.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this sorted set command
Which option contains a syntax error when adding members to a sorted set?
Redis
ZADD myset 10 "apple" 20 "banana" 15 "cherry"
AZADD myset "10" "apple" "20" "banana" "15" "cherry"
BZADD myset 10 "apple" 20 "banana" 15
CZADD myset 10 apple 20 banana 15 cherry
DZADD myset 10 "apple" 20 "banana" 15 "cherry"
Attempts:
2 left
💡 Hint
Check if all scores have corresponding members.
❓ optimization
advanced
2:00remaining
How to efficiently update scores in a large sorted set?
You want to update scores of multiple members in a large Redis sorted set. Which approach is most efficient?
AUse ZINCRBY for each member to add the new score
BUse multiple ZADD commands, one per member
CDelete the sorted set and recreate it with new scores
DUse a single ZADD command with all member-score pairs
Attempts:
2 left
💡 Hint
Consider network round trips and command overhead.
🔧 Debug
expert
2:00remaining
Why does this ZRANK command return nil unexpectedly?
You added members to a sorted set but `ZRANK myset "orange"` returns nil. What is the most likely cause?
AThe member "orange" was never added to the sorted set
BThe sorted set key "myset" does not exist
CThe member "orange" has a score of zero
DZRANK only works with string keys, not sorted sets
Attempts:
2 left
💡 Hint
ZRANK returns nil if the member is not found in the set.

Practice

(1/5)
1. What is the main reason Redis sorted sets combine uniqueness with ordering?
easy
A. To store unique members with associated scores for sorted retrieval
B. To allow duplicate members with different scores
C. To store unordered lists of members
D. To only store scores without members

Solution

  1. Step 1: Understand uniqueness in sorted sets

    Sorted sets store each member only once, ensuring uniqueness.
  2. Step 2: Understand ordering in sorted sets

    Each member has a score that determines its order in the set.
  3. Final Answer:

    To store unique members with associated scores for sorted retrieval -> Option A
  4. Quick Check:

    Uniqueness + ordering = unique members with scores [OK]
Hint: Remember: unique members + scores = sorted sets [OK]
Common Mistakes:
  • Thinking duplicates are allowed
  • Confusing sorted sets with lists
  • Ignoring the role of scores
2. Which of the following is the correct Redis command to add a member with a score to a sorted set?
easy
A. ZADD myset member1 10
B. SADD myset 10 member1
C. ZADD myset 10 member1
D. ADD myset 10 member1

Solution

  1. Step 1: Recall Redis sorted set add syntax

    The correct syntax is ZADD key score member.
  2. Step 2: Match syntax with options

    ZADD myset 10 member1 matches ZADD myset 10 member1 correctly.
  3. Final Answer:

    ZADD myset 10 member1 -> Option C
  4. Quick Check:

    ZADD key score member = correct syntax [OK]
Hint: ZADD uses score before member name [OK]
Common Mistakes:
  • Using SADD instead of ZADD
  • Swapping score and member order
  • Using ADD command which doesn't exist
3. Given the commands:
ZADD myset 5 apple
ZADD myset 10 banana
ZADD myset 7 cherry
What will ZRANGE myset 0 -1 WITHSCORES return?
medium
A. ["apple", "5", "banana", "10", "cherry", "7"]
B. ["apple", "5", "cherry", "7", "banana", "10"]
C. ["banana", "10", "cherry", "7", "apple", "5"]
D. ["cherry", "7", "apple", "5", "banana", "10"]

Solution

  1. Step 1: Understand ZADD inserts with scores

    Members apple=5, banana=10, cherry=7 are added.
  2. Step 2: ZRANGE returns members sorted by score ascending

    Order is apple(5), cherry(7), banana(10).
  3. Final Answer:

    ["apple", "5", "cherry", "7", "banana", "10"] -> Option B
  4. Quick Check:

    ZRANGE sorts by score ascending [OK]
Hint: ZRANGE returns members sorted by score ascending [OK]
Common Mistakes:
  • Assuming ZRANGE sorts by insertion order
  • Confusing WITHSCORES output format
  • Mixing up ascending and descending order
4. You run these commands:
ZADD myset 1 apple
ZADD myset 2 apple
ZRANGE myset 0 -1
But the output shows only one 'apple'. What is the issue?
medium
A. The second ZADD command has wrong syntax
B. Redis sorted sets allow duplicates, so this is a bug
C. ZRANGE command is incorrect and filters duplicates
D. Sorted sets do not allow duplicate members; score is updated instead

Solution

  1. Step 1: Understand uniqueness in sorted sets

    Sorted sets keep unique members only; adding same member updates score.
  2. Step 2: Analyze commands effect

    Second ZADD updates 'apple' score from 1 to 2, no duplicate added.
  3. Final Answer:

    Sorted sets do not allow duplicate members; score is updated instead -> Option D
  4. Quick Check:

    Same member updates score, no duplicates [OK]
Hint: Same member updates score, no duplicates added [OK]
Common Mistakes:
  • Expecting duplicates in sorted sets
  • Thinking ZRANGE filters duplicates
  • Assuming syntax error caused issue
5. You want to keep a leaderboard where players have unique names and scores that can change. Which Redis data structure best fits this need and why?
hard
A. Sorted sets, because they store unique members with scores and keep them ordered
B. Lists, because they allow duplicates and maintain insertion order
C. Hashes, because they store key-value pairs without ordering
D. Sets, because they store unique members but no scores

Solution

  1. Step 1: Identify requirements for leaderboard

    Need unique player names, scores, and sorted order by score.
  2. Step 2: Match Redis data structures to requirements

    Sorted sets store unique members with scores and keep order automatically.
  3. Step 3: Exclude other options

    Lists allow duplicates and no score ordering; hashes lack ordering; sets lack scores.
  4. Final Answer:

    Sorted sets, because they store unique members with scores and keep them ordered -> Option A
  5. Quick Check:

    Leaderboard needs unique + ordered scores = sorted sets [OK]
Hint: Leaderboard needs unique + ordered scores: use sorted sets [OK]
Common Mistakes:
  • Choosing lists or sets ignoring ordering or scores
  • Confusing hashes with sorted sets
  • Not considering uniqueness requirement
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