0
0
Redisquery~20 mins

SRANDMEMBER for random elements in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SRANDMEMBER Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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 2
AAn error because count must be negative or zero
BAn array of 2 random elements from the set, possibly with duplicates, e.g., ["a", "a"]
CA single random element as a string, e.g., "c"
DAn array of 2 unique random elements from the set, e.g., ["b", "d"]
Attempts:
2 left
💡 Hint
When count is positive, SRANDMEMBER returns unique elements without repetition.
query_result
intermediate
2: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 -4
AAn array of 4 random elements from the set, possibly with duplicates, e.g., ["red", "red", "blue", "green"]
BAn array of 3 unique elements because the set only has 3 members
CA single random element as a string
DAn error because count cannot be negative
Attempts:
2 left
💡 Hint
Negative count allows duplicates in the returned elements.
📝 Syntax
advanced
2:00remaining
Which SRANDMEMBER command syntax is invalid?
Identify the invalid SRANDMEMBER command syntax from the options below.
ASRANDMEMBER myset -2
BSRANDMEMBER myset 3
CSRANDMEMBER myset 0
DSRANDMEMBER myset
Attempts:
2 left
💡 Hint
Count of zero is not a valid argument for SRANDMEMBER.
optimization
advanced
2: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?
AUse SPOP mylargeSet 5 and then add them back
BSRANDMEMBER mylargeSet 5
CUse SMEMBERS mylargeSet and pick 5 random elements in client code
DSRANDMEMBER mylargeSet -5
Attempts:
2 left
💡 Hint
SRANDMEMBER with positive count returns unique elements efficiently.
🧠 Conceptual
expert
3:00remaining
Why does SRANDMEMBER with negative count allow duplicates?
Explain the reason behind SRANDMEMBER returning duplicates when the count argument is negative.
ABecause negative count tells Redis to sample with replacement, allowing repeated elements.
BBecause Redis ignores the sign and always returns unique elements.
CBecause negative count causes Redis to return an error and no elements.
DBecause Redis converts negative count to zero and returns an empty array.
Attempts:
2 left
💡 Hint
Think about sampling with or without replacement.