Bird
Raised Fist0
Redisquery~20 mins

Set vs sorted set for membership in Redis - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference in membership check performance

Which Redis data structure provides the fastest membership check for a single element?

ABoth have the same performance since they store elements similarly.
BSorted Set, because it maintains elements in order for quick binary search.
CSet, because it uses a hash table for O(1) membership checks.
DNeither supports membership checks efficiently; use a list instead.
Attempts:
2 left
💡 Hint

Think about how each data structure stores elements and how that affects lookup speed.

❓ query_result
intermediate
1:30remaining
Check membership in a Redis Set

Given a Redis Set named fruits containing apple, banana, and cherry, what is the output of the command SISMEMBER fruits banana?

Anil
B1
C0
DError
Attempts:
2 left
💡 Hint

Remember what SISMEMBER returns when the element exists in the set.

📝 Syntax
advanced
2:00remaining
Valid command to check membership in a Sorted Set

Which of the following Redis commands correctly checks if the member user42 exists in the Sorted Set online_users?

AZMEMBER online_users user42
BSISMEMBER online_users user42
CZRANK online_users user42
DZSCORE online_users user42
Attempts:
2 left
💡 Hint

Think about which command returns a score or indication of membership for sorted sets.

❓ optimization
advanced
2:30remaining
Choosing data structure for frequent membership checks with ordering

You need to frequently check if elements exist and also retrieve them in a sorted order by score. Which Redis data structure is best suited?

ASorted Set, because it supports fast membership checks and maintains order.
BSet, because it has faster membership checks and you can sort on retrieval.
CList, because it maintains order and supports membership checks efficiently.
DHash, because it stores key-value pairs and supports membership checks.
Attempts:
2 left
💡 Hint

Consider both membership check speed and the need to keep elements sorted.

🔧 Debug
expert
3:00remaining
Why does this membership check fail on a Sorted Set?

You run the command SISMEMBER online_users user42 on a Sorted Set online_users and it returns an error even though user42 exists. Why?

ABecause <code>SISMEMBER</code> only works on Sets, not Sorted Sets.
BBecause the Sorted Set is empty.
CBecause <code>user42</code> is not a member of the Sorted Set.
DBecause <code>SISMEMBER</code> requires a score argument for Sorted Sets.
Attempts:
2 left
💡 Hint

Check command compatibility with data types.

Practice

(1/5)
1. Which Redis data structure is best for quickly checking if an item exists without caring about order?
easy
A. Set
B. Sorted Set
C. List
D. Hash

Solution

  1. Step 1: Understand the purpose of Sets

    Sets store unique items and allow fast membership checks without any order.
  2. Step 2: Compare with Sorted Sets

    Sorted Sets also store unique items but maintain order using scores, which is unnecessary if order doesn't matter.
  3. Final Answer:

    Set -> Option A
  4. Quick Check:

    Membership without order = Set [OK]
Hint: Use Set for membership checks without order [OK]
Common Mistakes:
  • Confusing Sorted Set with Set for membership
  • Thinking List is best for membership
  • Assuming Hash stores unique items like Set
2. Which Redis command correctly adds a member to a Set named myset?
easy
A. SADD myset member1
B. ZADD myset 1 member1
C. LPUSH myset member1
D. HSET myset member1 1

Solution

  1. Step 1: Identify the command for adding to a Set

    The command SADD adds members to a Set in Redis.
  2. Step 2: Check other commands

    ZADD is for Sorted Sets, LPUSH is for Lists, and HSET is for Hashes.
  3. Final Answer:

    SADD myset member1 -> Option A
  4. Quick Check:

    SADD adds to Set [OK]
Hint: Use SADD to add members to a Set [OK]
Common Mistakes:
  • Using ZADD for Sets
  • Using LPUSH which is for Lists
  • Using HSET which is for Hashes
3. Given the commands:
SADD fruits apple banana orange
ZADD scores 10 alice 20 bob 15 carol
Which command checks if 'banana' is a member of the Set fruits?
medium
A. SISMEMBER scores banana
B. ZSCORE fruits banana
C. ZRANK fruits banana
D. SISMEMBER fruits banana

Solution

  1. Step 1: Identify the correct membership check for Sets

    SISMEMBER checks if an item is in a Set.
  2. Step 2: Match the key and member

    The Set is fruits, and we check for 'banana'. So SISMEMBER fruits banana is correct.
  3. Final Answer:

    SISMEMBER fruits banana -> Option D
  4. Quick Check:

    Check membership in Set = SISMEMBER [OK]
Hint: Use SISMEMBER for Set membership checks [OK]
Common Mistakes:
  • Using ZSCORE or ZRANK on Sets
  • Checking membership on wrong key
  • Confusing Sorted Set commands with Set commands
4. You want to check if 'alice' is in a Sorted Set scores. Which command is incorrect and will cause an error?
medium
A. ZRANK scores alice
B. ZSCORE scores alice
C. SISMEMBER scores alice
D. ZREM scores alice

Solution

  1. Step 1: Understand commands for Sorted Sets

    ZSCORE, ZRANK, and ZREM are valid Sorted Set commands.
  2. Step 2: Identify the invalid command

    SISMEMBER is only for Sets, not Sorted Sets, so it will cause an error.
  3. Final Answer:

    SISMEMBER scores alice -> Option C
  4. Quick Check:

    SISMEMBER invalid for Sorted Set [OK]
Hint: SISMEMBER works only on Sets, not Sorted Sets [OK]
Common Mistakes:
  • Using SISMEMBER on Sorted Sets
  • Confusing ZREM with SREM
  • Assuming SISMEMBER works on all collections
5. You have a leaderboard stored as a Sorted Set leaderboard with player scores. You want to check if a player john exists in the leaderboard and get his rank. Which sequence of commands is correct?
hard
A. ZSCORE leaderboard john
SISMEMBER leaderboard john
B. ZSCORE leaderboard john
ZRANK leaderboard john
C. ZRANK leaderboard john
SISMEMBER leaderboard john
D. SISMEMBER leaderboard john
ZRANK leaderboard john

Solution

  1. Step 1: Check existence and score in Sorted Set

    ZSCORE returns the score if the member exists, otherwise null.
  2. Step 2: Get the rank if member exists

    ZRANK returns the rank of the member in the Sorted Set.
  3. Step 3: Validate command order

    First check score with ZSCORE, then get rank with ZRANK. Using SISMEMBER is invalid for Sorted Sets.
  4. Final Answer:

    ZSCORE leaderboard john
    ZRANK leaderboard john
    -> Option B
  5. Quick Check:

    Use ZSCORE then ZRANK for Sorted Set membership and rank [OK]
Hint: Use ZSCORE then ZRANK for Sorted Set membership and rank [OK]
Common Mistakes:
  • Using SISMEMBER on Sorted Sets
  • Checking rank before confirming existence
  • Mixing Set commands with Sorted Set commands
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