Bird
Raised Fist0
Redisquery~10 mins

SCARD for set size in Redis - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the number of elements in the set named 'myset'.

Redis
SCARD [1]
Drag options to blanks, or click blank then click option'
Acountset
Bsetcount
Celements
Dmyset
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong set name that does not exist.
Forgetting to provide the set name after SCARD.
2fill in blank
medium

Complete the code to find the size of the set stored in the variable 'setKey'.

Redis
SCARD [1]
Drag options to blanks, or click blank then click option'
AsetKey
Bsetkey
CSetKey
Dset_key
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the case of the key name.
Using underscores or other characters not in the key.
3fill in blank
hard

Fix the error in the command to get the set size of 'users'.

Redis
SCARD [1]
Drag options to blanks, or click blank then click option'
Ausers
Buser
CuserSet
Duser_set
Attempts:
3 left
💡 Hint
Common Mistakes
Using a singular form instead of plural.
Adding extra characters or underscores.
4fill in blank
hard

Fill both blanks to get the size of the set named 'activeUsers' and store it in variable 'count'.

Redis
count = [1]([2]) activeUsers
Drag options to blanks, or click blank then click option'
Aredis.call
BSCARD
Cget
DSET
Attempts:
3 left
💡 Hint
Common Mistakes
Using get which is for strings, not sets.
Using SET which is for setting values, not getting size.
5fill in blank
hard

Fill all three blanks to write a Lua script that returns the size of the set given by the first key argument.

Redis
return [1]([2], [3][1])
Drag options to blanks, or click blank then click option'
Aredis.call
BKEYS
CSCARD
DARGV
Attempts:
3 left
💡 Hint
Common Mistakes
Using ARGV instead of KEYS for keys.
Using get instead of SCARD.

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