Challenge - 5 Problems
ZCARD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of ZCARD on a sorted set?
Given a Redis sorted set named
What will be the output of the command
myzset with the following members and scores:zadd myzset 1 one 2 two 3 threeWhat will be the output of the command
ZCARD myzset?Redis
ZCARD myzset
Attempts:
2 left
💡 Hint
ZCARD returns the number of elements in a sorted set.
✗ Incorrect
The sorted set
myzset has three members: 'one', 'two', and 'three'. ZCARD returns the count of members, which is 3.📝 Syntax
intermediate2:00remaining
Which command syntax correctly returns the size of a sorted set?
Which of the following Redis commands correctly returns the number of elements in the sorted set
myzset?Attempts:
2 left
💡 Hint
The command to get the count of elements in a sorted set is a five-letter command starting with Z and ending with CARD.
✗ Incorrect
ZCARD is the correct Redis command to get the number of elements in a sorted set. ZCOUNT requires min and max score arguments, and ZSIZE and ZLEN are invalid commands.
❓ query_result
advanced2:00remaining
What happens when ZCARD is used on a non-existent key?
If you run
ZCARD nosuchkey on a Redis server where nosuchkey does not exist, what will be the output?Attempts:
2 left
💡 Hint
Redis commands often return zero or empty results for non-existent keys instead of errors.
✗ Incorrect
ZCARD returns 0 if the key does not exist, indicating the sorted set has zero elements.
🔧 Debug
advanced2:00remaining
Why does ZCARD return an error on this key?
You have a Redis key
mystring holding a string value. When you run ZCARD mystring, you get an error. Why?Attempts:
2 left
💡 Hint
ZCARD is specific to sorted sets and cannot be used on other data types.
✗ Incorrect
ZCARD returns an error if the key exists but is not a sorted set. Here,
mystring is a string, so the command fails.❓ optimization
expert2:00remaining
Efficiently checking if a sorted set is empty
You want to check if a sorted set
myzset is empty or not in the most efficient way. Which Redis command or approach is best?Attempts:
2 left
💡 Hint
Think about which command directly returns the count without extra overhead.
✗ Incorrect
ZCARD directly returns the number of elements in the sorted set efficiently. ZCOUNT requires score range arguments and is less direct. ZRANGE returns elements but involves more data transfer. ZRANK requires a member name and does not check emptiness directly.