Bird
Raised Fist0
Redisquery~20 mins

SCARD for set size in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Set Size Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
What is the output of SCARD on a non-empty set?
Given a Redis set stored at key myset containing the elements {"apple", "banana", "cherry"}, what will the command SCARD myset return?
Redis
SADD myset apple banana cherry
SCARD myset
A3
B1
CError: key does not exist
D0
Attempts:
2 left
💡 Hint
SCARD returns the number of elements in the set stored at the given key.
❓ query_result
intermediate
1:30remaining
What does SCARD return for a non-existent key?
If you run SCARD unknownkey on a Redis database where unknownkey does not exist, what will be the output?
Redis
SCARD unknownkey
AError: key does not exist
B0
Cnull
D-1
Attempts:
2 left
💡 Hint
SCARD returns zero if the key does not exist or is empty.
📝 Syntax
advanced
1:00remaining
Which SCARD command syntax is correct?
Identify the correct syntax to get the size of a set named fruits in Redis.
ASCARD:fruits
BSCARD(fruits)
CSCARD fruits
DSCARD = fruits
Attempts:
2 left
💡 Hint
Redis commands use space-separated arguments without parentheses or symbols.
❓ optimization
advanced
1:30remaining
Efficiently checking if a Redis set is empty
You want to check if the Redis set users_online is empty without retrieving all members. Which command is the most efficient?
ASCARD users_online
BSMEMBERS users_online
CSISMEMBER users_online ""
DGET users_online
Attempts:
2 left
💡 Hint

Think about which command returns only the count without fetching all data.

🧠 Conceptual
expert
2:00remaining
Understanding SCARD behavior with different data types
What happens if you run SCARD on a Redis key that holds a string value instead of a set?
Redis
SET mykey "hello"
SCARD mykey
A0
Bnull
C1
DError: WRONGTYPE Operation against a key holding the wrong kind of value
Attempts:
2 left
💡 Hint
Redis commands are type-specific and will error if used on wrong data types.

Practice

(1/5)
1. What does the Redis command SCARD do when used on a set?
easy
A. Returns all elements in the set
B. Deletes all elements from the set
C. Adds a new element to the set
D. Returns the number of unique elements in the set

Solution

  1. Step 1: Understand the purpose of SCARD

    The SCARD command is designed to count elements in a set, not modify or list them.
  2. Step 2: Identify the correct behavior

    SCARD returns the count of unique elements stored in the set.
  3. Final Answer:

    Returns the number of unique elements in the set -> Option D
  4. Quick Check:

    SCARD = set size count [OK]
Hint: SCARD counts set items without listing them [OK]
Common Mistakes:
  • Confusing SCARD with commands that list or delete elements
  • Thinking SCARD adds elements
  • Assuming SCARD returns elements instead of count
2. Which of the following is the correct syntax to get the size of a set named myset in Redis?
easy
A. SCARD myset
B. SCARD(myset)
C. GETSIZE myset
D. SIZE myset

Solution

  1. Step 1: Recall Redis command syntax

    Redis commands use the format COMMAND key without parentheses.
  2. Step 2: Match correct command for set size

    SCARD is the command to get set size, so SCARD myset is correct.
  3. Final Answer:

    SCARD myset -> Option A
  4. Quick Check:

    Correct Redis syntax = SCARD myset [OK]
Hint: Redis commands have no parentheses, just command and key [OK]
Common Mistakes:
  • Using parentheses like a function call
  • Using non-existent commands like GETSIZE or SIZE
  • Confusing SCARD with GET
3. Given the following Redis commands executed in order:
SADD fruits apple banana orange
SADD fruits banana grape
SCARD fruits

What will be the output of SCARD fruits?
medium
A. 3
B. 5
C. 4
D. 2

Solution

  1. 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).
  2. Step 2: Count unique elements after both commands

    Unique elements are apple, banana, orange, grape = 4 total.
  3. Final Answer:

    4 -> Option C
  4. Quick Check:

    Unique fruits count = 4 [OK]
Hint: SCARD counts unique items, duplicates ignored [OK]
Common Mistakes:
  • Counting duplicates multiple times
  • Assuming SADD overwrites set
  • Confusing SCARD output with list of elements
4. You run the command SCARD myset but get an error: WRONGTYPE Operation against a key holding the wrong kind of value. What is the most likely cause?
medium
A. myset is not a set but another data type like string or list
B. The key myset does not exist
C. SCARD command is misspelled
D. Redis server is down

Solution

  1. Step 1: Understand the error message

    The error indicates the key exists but is not a set type.
  2. Step 2: Identify cause of WRONGTYPE error

    SCARD only works on sets; if myset is string or list, this error occurs.
  3. Final Answer:

    myset is not a set but another data type like string or list -> Option A
  4. Quick Check:

    WRONGTYPE = wrong data type for SCARD [OK]
Hint: WRONGTYPE means key is not a set [OK]
Common Mistakes:
  • Assuming key does not exist causes this error
  • Thinking SCARD command spelling causes this error
  • Ignoring data type mismatch
5. You have two sets in Redis: 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?
hard
A. SCARD setA + SCARD setB
B. SUNIONSTORE tempSet setA setB followed by SCARD tempSet
C. SINTER setA setB
D. SDIFF setA setB

Solution

  1. Step 1: Understand the problem of counting unique elements across two sets

    Simply adding SCARD results counts duplicates twice.
  2. Step 2: Use SUNIONSTORE to combine sets uniquely

    SUNIONSTORE creates a new set with all unique elements from both sets.
  3. Step 3: Use SCARD on the combined set

    SCARD on the new set gives total unique elements count.
  4. Final Answer:

    SUNIONSTORE tempSet setA setB followed by SCARD tempSet -> Option B
  5. Quick Check:

    Use SUNIONSTORE + SCARD for unique combined count [OK]
Hint: Combine sets with SUNIONSTORE, then SCARD the result [OK]
Common Mistakes:
  • Adding SCARD counts directly causing duplicates
  • Using SINTER or SDIFF which don't give total unique count
  • Trying to count without combining sets
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard