SCARD for set size in Redis - Time & Space Complexity
We want to understand how the time to get the size of a set in Redis changes as the set grows.
How does the command SCARD perform when the set has more or fewer elements?
Analyze the time complexity of the following Redis command.
# Get the number of elements in a set named 'myset'
SCARD myset
This command returns the count of items stored in the set 'myset'.
SCARD does not loop through the set elements to count them.
- Primary operation: Direct retrieval of the stored size value.
- How many times: Only once per command call.
The time to get the set size stays almost the same no matter how many elements are in the set.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count does not increase with the size of the set.
Time Complexity: O(1)
This means the time to get the set size stays constant no matter how big the set is.
[X] Wrong: "SCARD must check every element to count them, so it gets slower with bigger sets."
[OK] Correct: Redis stores the size of the set internally, so SCARD just reads that number directly without scanning elements.
Knowing that SCARD runs in constant time shows you understand how Redis optimizes common operations, which is a useful skill for working with fast data stores.
"What if we used a command that lists all set members instead of SCARD? How would the time complexity change?"
Practice
SCARD do when used on a set?Solution
Step 1: Understand the purpose of SCARD
The SCARD command is designed to count elements in a set, not modify or list them.Step 2: Identify the correct behavior
SCARD returns the count of unique elements stored in the set.Final Answer:
Returns the number of unique elements in the set -> Option DQuick Check:
SCARD = set size count [OK]
- Confusing SCARD with commands that list or delete elements
- Thinking SCARD adds elements
- Assuming SCARD returns elements instead of count
myset in Redis?Solution
Step 1: Recall Redis command syntax
Redis commands use the format COMMAND key without parentheses.Step 2: Match correct command for set size
SCARD is the command to get set size, soSCARD mysetis correct.Final Answer:
SCARD myset -> Option AQuick Check:
Correct Redis syntax = SCARD myset [OK]
- Using parentheses like a function call
- Using non-existent commands like GETSIZE or SIZE
- Confusing SCARD with GET
SADD fruits apple banana orange SADD fruits banana grape SCARD fruits
What will be the output of
SCARD fruits?Solution
Step 1: Identify unique elements added to the set
First SADD adds apple, banana, orange (3 unique). Second SADD adds banana (already exists), grape (new).Step 2: Count unique elements after both commands
Unique elements are apple, banana, orange, grape = 4 total.Final Answer:
4 -> Option CQuick Check:
Unique fruits count = 4 [OK]
- Counting duplicates multiple times
- Assuming SADD overwrites set
- Confusing SCARD output with list of elements
SCARD myset but get an error: WRONGTYPE Operation against a key holding the wrong kind of value. What is the most likely cause?Solution
Step 1: Understand the error message
The error indicates the key exists but is not a set type.Step 2: Identify cause of WRONGTYPE error
SCARD only works on sets; ifmysetis string or list, this error occurs.Final Answer:
mysetis not a set but another data type like string or list -> Option AQuick Check:
WRONGTYPE = wrong data type for SCARD [OK]
- Assuming key does not exist causes this error
- Thinking SCARD command spelling causes this error
- Ignoring data type mismatch
setA with 3 elements and setB with 4 elements, where 2 elements overlap. You want to find the total number of unique elements across both sets without retrieving all elements. Which Redis command or combination should you use?Solution
Step 1: Understand the problem of counting unique elements across two sets
Simply adding SCARD results counts duplicates twice.Step 2: Use SUNIONSTORE to combine sets uniquely
SUNIONSTORE creates a new set with all unique elements from both sets.Step 3: Use SCARD on the combined set
SCARD on the new set gives total unique elements count.Final Answer:
SUNIONSTORE tempSet setA setB followed by SCARD tempSet -> Option BQuick Check:
Use SUNIONSTORE + SCARD for unique combined count [OK]
- Adding SCARD counts directly causing duplicates
- Using SINTER or SDIFF which don't give total unique count
- Trying to count without combining sets
Ready to prove it?
Pass the test and this pattern moves unknown
