SUNION, SINTER, SDIFF set operations in Redis - Time & Space Complexity
When working with Redis sets, it's important to know how the time to perform operations grows as the sets get bigger.
We want to understand how the cost changes when combining or comparing sets using SUNION, SINTER, and SDIFF.
Analyze the time complexity of the following Redis commands.
# Get union of sets
SUNION set1 set2 set3
# Get intersection of sets
SINTER set1 set2 set3
# Get difference of sets
SDIFF set1 set2 set3
These commands combine or compare multiple sets to produce a new set based on union, intersection, or difference.
Look at what repeats when Redis processes these commands.
- Primary operation: Scanning elements of each input set to compare or combine them.
- How many times: Each element in each set is checked once during the operation.
As the sets get bigger, Redis must look at more elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks per set |
| 100 | About 100 checks per set |
| 1000 | About 1000 checks per set |
Pattern observation: The work grows roughly in direct proportion to the total number of elements across all sets.
Time Complexity: O(N)
This means the time to complete the operation grows linearly with the total number of elements in all input sets.
[X] Wrong: "These set operations always take the same time no matter how big the sets are."
[OK] Correct: Actually, Redis must look at each element to combine or compare sets, so bigger sets take more time.
Understanding how set operations scale helps you explain performance in real projects and shows you can think about efficiency clearly.
What if we used SUNIONSTORE to save the union result in a new set? How would that affect the time complexity?
Practice
SUNION do when applied to multiple sets?Solution
Step 1: Understand SUNION operation
The SUNION command merges all elements from the given sets and removes duplicates.Step 2: Compare with other set operations
SINTER returns only common elements, SDIFF returns unique elements from the first set, so SUNION is the only one that merges all uniquely.Final Answer:
It returns all unique elements from all given sets combined. -> Option AQuick Check:
SUNION = all unique elements combined [OK]
- Confusing SUNION with SINTER (common elements)
- Thinking SUNION returns only first set elements
- Assuming SUNION deletes sets
set1 and set2 in Redis?Solution
Step 1: Recall Redis command for intersection
The correct Redis command to find common elements isSINTER.Step 2: Check syntax correctness
The syntax isSINTER key1 key2 .... SINTER set1 set2 matches this exactly.Final Answer:
SINTER set1 set2 -> Option AQuick Check:
SINTER syntax = SINTER keys [OK]
- Using SUNION instead of SINTER
- Using non-existent command SINTERSECTION
- Confusing SDIFF with intersection
set1 = {"apple", "banana", "cherry"}set2 = {"banana", "date", "fig"}What is the result of
SINTER set1 set2?Solution
Step 1: Identify common elements between set1 and set2
Elements in set1: apple, banana, cherry; set2: banana, date, fig. The only common element is banana.Step 2: Understand SINTER output
SINTER returns only elements present in all sets, so the result is ["banana"].Final Answer:
["banana"] -> Option BQuick Check:
SINTER common elements = ["banana"] [OK]
- Listing all elements instead of common ones
- Confusing SINTER with SUNION or SDIFF
- Including elements unique to one set
SDIFF set1 set2 but get an unexpected empty result. Given:set1 = {"cat", "dog"}set2 = {"cat", "dog", "mouse"}What is the likely reason?
Solution
Step 1: Understand SDIFF behavior
SDIFF returns elements in the first set that are not in the other sets.Step 2: Analyze given sets
set1 has "cat" and "dog"; set2 has "cat", "dog", and "mouse". Since all set1 elements are in set2, SDIFF returns an empty set.Final Answer:
All elements of set1 are also in set2, so no unique elements remain. -> Option DQuick Check:
SDIFF unique first set elements = empty [OK]
- Assuming SDIFF requires only one set
- Thinking SDIFF returns common elements
- Believing Redis lacks SDIFF support
setA = {"red", "blue", "green"}setB = {"blue", "yellow"}setC = {"green", "yellow", "black"}Which command returns elements that are in
setA but not in setB or setC?Solution
Step 1: Understand SDIFF with multiple sets
SDIFF returns elements in the first set that are not in any of the other sets.Step 2: Apply SDIFF to given sets
Elements in setA: red, blue, green. Elements in setB and setC combined: blue, yellow, green, black. Elements unique to setA are only "red".Step 3: Verify command correctness
SDIFF setA setB setC uses SDIFF with setA first, then setB and setC, which matches the requirement.Final Answer:
SDIFF setA setB setC -> Option CQuick Check:
SDIFF first set minus others = unique elements [OK]
- Using SINTER or SUNION instead of SDIFF
- Reversing set order in SDIFF
- Expecting SDIFF to return common elements
Ready to prove it?
Pass the test and this pattern moves unknown
