Challenge - 5 Problems
SRANDMEMBER Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What does SRANDMEMBER return when count is positive?
Given a Redis set
myset containing {"a", "b", "c", "d"}, what is the expected output of SRANDMEMBER myset 2?Redis
SADD myset a b c d
SRANDMEMBER myset 2Attempts:
2 left
💡 Hint
When count is positive, SRANDMEMBER returns unique elements without repetition.
✗ Incorrect
SRANDMEMBER with a positive count returns an array of unique random elements from the set, up to the count specified, without duplicates.
❓ query_result
intermediate2:00remaining
What happens when SRANDMEMBER count is negative?
Given a Redis set
colors with elements {"red", "green", "blue"}, what is the output of SRANDMEMBER colors -4?Redis
SADD colors red green blue
SRANDMEMBER colors -4Attempts:
2 left
💡 Hint
Negative count allows duplicates in the returned elements.
✗ Incorrect
When count is negative, SRANDMEMBER returns the absolute value of count random elements, allowing duplicates.
📝 Syntax
advanced2:00remaining
Which SRANDMEMBER command syntax is invalid?
Identify the invalid SRANDMEMBER command syntax from the options below.
Attempts:
2 left
💡 Hint
Count of zero is not a valid argument for SRANDMEMBER.
✗ Incorrect
SRANDMEMBER with count=0 is invalid and causes an error; count must be omitted or a non-zero integer.
❓ optimization
advanced2:00remaining
How to efficiently get 5 random unique elements from a large set?
You have a Redis set with 1 million elements. Which command is the most efficient to get 5 unique random elements?
Attempts:
2 left
💡 Hint
SRANDMEMBER with positive count returns unique elements efficiently.
✗ Incorrect
SRANDMEMBER with a positive count returns unique random elements efficiently without modifying the set.
🧠 Conceptual
expert3:00remaining
Why does SRANDMEMBER with negative count allow duplicates?
Explain the reason behind SRANDMEMBER returning duplicates when the count argument is negative.
Attempts:
2 left
💡 Hint
Think about sampling with or without replacement.
✗ Incorrect
Negative count means Redis samples with replacement, so elements can appear multiple times in the result.