Bird
Raised Fist0
Redisquery~20 mins

Unique visitor tracking with sets in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Unique Visitor Tracking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
Counting unique visitors using Redis sets
Given the following Redis commands executed in order:

1. SADD visitors 101 102 103
2. SADD visitors 102 104
3. SADD visitors 105
4. SCARD visitors

What is the output of the last command?
Redis
SADD visitors 101 102 103
SADD visitors 102 104
SADD visitors 105
SCARD visitors
A3
B4
C6
D5
Attempts:
2 left
💡 Hint
Remember that sets only store unique elements.
🧠 Conceptual
intermediate
1:00remaining
Why use sets for unique visitor tracking?
Why are Redis sets a good choice for tracking unique visitors on a website?
ABecause sets store data in sorted order for fast retrieval.
BBecause sets automatically prevent duplicate entries, ensuring each visitor is counted once.
CBecause sets allow storing key-value pairs for visitor details.
DBecause sets compress data to save memory.
Attempts:
2 left
💡 Hint
Think about how duplicates are handled.
📝 Syntax
advanced
1:30remaining
Identify the correct Redis command to add multiple visitors
Which of the following Redis commands correctly adds visitor IDs 201, 202, and 203 to the set named 'daily_visitors'?
ASADD daily_visitors [201,202,203]
BADD daily_visitors 201 202 203
CSADD daily_visitors 201 202 203
DSADD daily_visitors {201,202,203}
Attempts:
2 left
💡 Hint
Check the exact Redis command name and argument format.
❓ optimization
advanced
2:00remaining
Efficiently tracking unique visitors across multiple days
You have daily sets of visitors: 'visitors_day1', 'visitors_day2', and 'visitors_day3'. You want to find the total unique visitors over these three days. Which Redis command is the most efficient to get this count without creating a new set?
ASUNION visitors_day1 visitors_day2 visitors_day3
BSUNIONSTORE total_visitors visitors_day1 visitors_day2 visitors_day3; SCARD total_visitors
CSCARD visitors_day1 + SCARD visitors_day2 + SCARD visitors_day3
DSINTER visitors_day1 visitors_day2 visitors_day3
Attempts:
2 left
💡 Hint
Look for a command that returns the union without storing it.
🔧 Debug
expert
2:00remaining
Debugging incorrect unique visitor count
A developer runs these commands:

1. SADD visitors 301 302 303
2. SADD visitors 302 304
3. DEL visitors
4. SCARD visitors

The SCARD command returns 0, but the developer expected 4. What is the reason?
AThe DEL command deleted the 'visitors' set, so SCARD returns 0 because the set no longer exists.
BSCARD only counts members added in the last SADD command.
CSCARD returns the count of unique members added in the current session only.
DSADD commands did not add members because of a syntax error.
Attempts:
2 left
💡 Hint
Think about what DEL does to a key.

Practice

(1/5)
1. What is the main advantage of using Redis sets for unique visitor tracking?
easy
A. They automatically prevent duplicate visitor IDs.
B. They store visitor data in sorted order.
C. They allow storing large text data efficiently.
D. They provide built-in encryption for visitor IDs.

Solution

  1. Step 1: Understand Redis sets behavior

    Redis sets store unique elements only, so duplicates are not saved.
  2. Step 2: Relate to visitor tracking

    This means each visitor ID is stored once, ensuring uniqueness automatically.
  3. Final Answer:

    They automatically prevent duplicate visitor IDs. -> Option A
  4. Quick Check:

    Redis sets = unique elements [OK]
Hint: Remember: sets never store duplicates [OK]
Common Mistakes:
  • Thinking sets store elements in order
  • Confusing sets with lists or hashes
  • Assuming sets encrypt data automatically
2. Which Redis command correctly adds a visitor ID 'user123' to a set named 'visitors'?
easy
A. SET visitors user123
B. ADD visitors user123
C. SADD visitors user123
D. APPEND visitors user123

Solution

  1. Step 1: Identify the correct command to add to a set

    The Redis command to add members to a set is SADD.
  2. Step 2: Check syntax correctness

    SADD visitors user123 adds 'user123' to the 'visitors' set correctly.
  3. Final Answer:

    SADD visitors user123 -> Option C
  4. Quick Check:

    SADD adds members to sets [OK]
Hint: Use SADD to add members to sets [OK]
Common Mistakes:
  • Using SET which is for strings, not sets
  • Using ADD which is invalid in Redis
  • Using APPEND which is for strings
3. Given the following Redis commands executed in order:
SADD visitors user1
SADD visitors user2
SADD visitors user1
SCARD visitors
What will be the output of SCARD visitors?
medium
A. 2
B. 0
C. 1
D. 3

Solution

  1. Step 1: Analyze SADD commands

    First adds 'user1', second adds 'user2', third tries to add 'user1' again (ignored as duplicate).
  2. Step 2: Understand SCARD output

    SCARD returns the count of unique members in the set, which are 'user1' and 'user2'.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Unique visitors count = 2 [OK]
Hint: SCARD counts unique set members [OK]
Common Mistakes:
  • Counting duplicates as separate members
  • Confusing SCARD with SMEMBERS output
  • Assuming SCARD returns all added commands count
4. You run the command SADD visitors without specifying any visitor ID. What will happen?
medium
A. A new empty set is created.
B. An error occurs due to missing member argument.
C. The command adds a null member to the set.
D. The command is ignored silently.

Solution

  1. Step 1: Check SADD command requirements

    SADD requires at least one member argument to add to the set.
  2. Step 2: Result of missing member argument

    Running SADD without members causes a syntax error in Redis.
  3. Final Answer:

    An error occurs due to missing member argument. -> Option B
  4. Quick Check:

    SADD needs members, else error [OK]
Hint: SADD must have at least one member argument [OK]
Common Mistakes:
  • Assuming empty sets can be created with SADD alone
  • Thinking null or empty string is added automatically
  • Expecting silent failure without error
5. You want to track unique visitors for multiple days using Redis sets. Which approach correctly counts total unique visitors across days stored as sets 'day1', 'day2', and 'day3'?
hard
A. Use DEL day1 day2 day3 then SCARD total.
B. Use SINTERSTORE total day1 day2 day3 then SCARD total.
C. Use SMEMBERS day1 then count manually.
D. Use SUNIONSTORE total day1 day2 day3 then SCARD total.

Solution

  1. Step 1: Understand set operations for unique visitors

    To combine unique visitors from multiple sets, use SUNIONSTORE to create a union set.
  2. Step 2: Count unique visitors in combined set

    After union, SCARD on the combined set gives total unique visitors across days.
  3. Final Answer:

    Use SUNIONSTORE total day1 day2 day3 then SCARD total. -> Option D
  4. Quick Check:

    Union sets + SCARD = total unique visitors [OK]
Hint: Use SUNIONSTORE to combine sets before counting [OK]
Common Mistakes:
  • Using SINTERSTORE which finds common visitors only
  • Counting members manually instead of using SCARD
  • Deleting sets before counting unique visitors
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