A Redis List is a simple sequence of strings sorted by insertion order. You can add elements to the start or end, and access elements by their position.
Click to reveal answer
beginner
What is a Redis Sorted Set?
A Redis Sorted Set is a collection of unique strings, each with a score. The elements are sorted by their score, allowing fast range queries by score or rank.
Click to reveal answer
beginner
How does a Redis List maintain order?
A Redis List keeps order by the sequence of insertion. New elements are added to the head or tail, preserving the order they were inserted.
Click to reveal answer
beginner
How does a Redis Sorted Set maintain order?
A Redis Sorted Set orders elements by their numeric score. The order is always sorted from lowest to highest score, regardless of insertion order.
Click to reveal answer
intermediate
When should you use a Redis Sorted Set instead of a List for sequences?
Use a Sorted Set when you need to keep elements sorted by a score, perform range queries by score or rank, or avoid duplicates. Use a List when order is based on insertion and duplicates are allowed.
Click to reveal answer
Which Redis data type keeps elements sorted by a numeric score?
ASorted Set
BHash
CList
DString
✗ Incorrect
Sorted Sets store elements with scores and keep them sorted by these scores.
Which Redis data type preserves insertion order and allows duplicates?
ASorted Set
BList
CSet
DHash
✗ Incorrect
Lists keep elements in insertion order and allow duplicates.
If you want to get elements between two scores quickly, which Redis type is best?
AList
BHash
CSorted Set
DString
✗ Incorrect
Sorted Sets support fast range queries by score.
Can Redis Sorted Sets contain duplicate elements?
AYes, duplicates allowed
BDuplicates allowed only at the start
COnly duplicates with different scores
DNo, elements are unique
✗ Incorrect
Sorted Sets require unique elements; duplicates are not allowed.
Which Redis data type would you choose to implement a queue preserving order?
AList
BSorted Set
CSet
DHash
✗ Incorrect
Lists are ideal for queues because they preserve insertion order and allow push/pop operations.
Explain the main differences between Redis Lists and Sorted Sets for storing sequences.
Think about order, duplicates, and how elements are accessed.
You got /5 concepts.
When would you prefer to use a Redis Sorted Set over a List for sequence data?
Consider use cases involving ranking or scoring.
You got /4 concepts.
Practice
(1/5)
1. Which Redis data structure allows storing duplicate elements while preserving their order?
easy
A. Hash
B. List
C. Sorted Set
D. Set
Solution
Step 1: Understand List behavior
Lists in Redis keep elements in the order they were added and allow duplicates.
Step 2: Compare with Sorted Set
Sorted sets store unique elements sorted by score, so duplicates are not allowed.
Final Answer:
List -> Option B
Quick Check:
Duplicates allowed and order preserved = List [OK]
Hint: Duplicates allowed? Use List, not Sorted Set. [OK]
Common Mistakes:
Thinking Sorted Set allows duplicates
Confusing Sets with Lists
Assuming Hashes store sequences
2. Which of the following Redis commands correctly adds an element with a score to a sorted set?
easy
A. HSET myhash field element
B. LPUSH mylist element
C. SADD myset element
D. ZADD myzset 10 element
Solution
Step 1: Identify command for sorted set insertion
ZADD adds elements with a score to a sorted set.
Step 2: Check other commands
LPUSH adds to list, SADD adds to set, HSET adds to hash field.
Final Answer:
ZADD myzset 10 element -> Option D
Quick Check:
ZADD = add with score [OK]
Hint: ZADD is for sorted sets with scores. [OK]
Common Mistakes:
Using LPUSH for sorted sets
Confusing SADD with ZADD
Using HSET for sequences
3. Given the following Redis commands executed in order: LPUSH mylist a LPUSH mylist b LPUSH mylist c What will LRANGE mylist 0 -1 return?
medium
A. ["a", "b", "c"]
B. ["b", "c", "a"]
C. ["c", "b", "a"]
D. Error
Solution
Step 1: Understand LPUSH order
LPUSH adds elements to the head (start) of the list, so last pushed is first.
Hint: LPUSH adds to front; last pushed appears first. [OK]
Common Mistakes:
Assuming LPUSH adds to tail
Mixing order of elements
Expecting error from LRANGE
4. You tried to add duplicate elements with different scores to a sorted set using: ZADD myzset 1 element ZADD myzset NX 2 element But the score did not update as expected. What is the likely issue?
medium
A. Sorted sets do not allow duplicate elements; score updates require special flag
B. ZADD command syntax is incorrect
C. You must remove the element before adding it again
D. Redis lists should be used instead
Solution
Step 1: Understand sorted set uniqueness
Sorted sets store unique elements; adding the same element updates its score by default.
Step 2: Check if score updates require flags
By default, ZADD updates score if element exists; if it did not update, likely a misunderstanding or missing flag like NX or XX.
Final Answer:
Sorted sets do not allow duplicate elements; score updates require special flag -> Option A
Quick Check:
Sorted sets unique, score updates need care [OK]
Hint: Sorted sets unique; score updates may need flags. [OK]
Common Mistakes:
Thinking duplicates allowed in sorted sets
Assuming ZADD syntax is wrong
Trying to remove before adding
5. You want to store a sequence of user actions with timestamps and retrieve them sorted by time, allowing multiple actions at the same timestamp. Which Redis data structure and approach is best?
hard
A. Use a Sorted Set with timestamps as scores; append a unique suffix to actions to allow duplicates
B. Use a List and store actions with timestamps as strings; order is insertion order
C. Use a Hash with timestamps as fields and actions as values
D. Use a Set to store actions; order does not matter
Solution
Step 1: Analyze requirement for sorting by timestamp
Sorted sets sort elements by numeric score, perfect for timestamps.
Step 2: Handle duplicates with same timestamp
Sorted sets require unique elements, so append unique suffix to actions to allow duplicates with same score.
Step 3: Compare with other options
Lists preserve order but cannot sort by timestamp easily; hashes and sets do not maintain order or allow sorting by score.
Final Answer:
Use a Sorted Set with timestamps as scores; append a unique suffix to actions to allow duplicates -> Option A
Quick Check:
Sorted set + unique elements for timestamp sorting [OK]
Hint: Sorted set + unique suffix for duplicates with same score. [OK]
Common Mistakes:
Using List but losing timestamp sorting
Ignoring uniqueness in sorted sets
Using Hash or Set which don't sort
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.