Bird
Raised Fist0
Redisquery~20 mins

List vs sorted set for sequences in Redis - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of adding elements to a Redis List
What will be the output of the following Redis commands?

Commands:
LPUSH mylist 10
LPUSH mylist 20
LRANGE mylist 0 -1
Redis
LPUSH mylist 10
LPUSH mylist 20
LRANGE mylist 0 -1
A["20", "10"]
B["10", "20"]
C["10"]
D[]
Attempts:
2 left
💡 Hint
Remember LPUSH adds elements to the head of the list.
❓ query_result
intermediate
2:00remaining
Output of adding elements to a Redis Sorted Set
What will be the output of the following Redis commands?

Commands:
ZADD myzset 5 "apple"
ZADD myzset 1 "banana"
ZADD myzset 3 "cherry"
ZRANGE myzset 0 -1 WITHSCORES
Redis
ZADD myzset 5 "apple"
ZADD myzset 1 "banana"
ZADD myzset 3 "cherry"
ZRANGE myzset 0 -1 WITHSCORES
A["cherry", "3", "banana", "1", "apple", "5"]
B["apple", "5", "banana", "1", "cherry", "3"]
C["apple", "banana", "cherry"]
D["banana", "1", "cherry", "3", "apple", "5"]
Attempts:
2 left
💡 Hint
Sorted sets order elements by score ascending.
🧠 Conceptual
advanced
2:00remaining
Choosing between Redis List and Sorted Set for sequence with duplicates
You want to store a sequence of events that can have duplicate timestamps and need to preserve insertion order. Which Redis data structure is best suited?
ARedis Sorted Set, because it automatically orders by score and allows duplicates.
BRedis List, because it preserves insertion order and allows duplicates.
CRedis Hash, because it stores key-value pairs efficiently.
DRedis Set, because it stores unique elements without order.
Attempts:
2 left
💡 Hint
Consider if duplicates and order matter.
📝 Syntax
advanced
2:00remaining
Identify the invalid Redis command for adding to a Sorted Set
Which of the following commands will cause a syntax error when adding elements to a Redis Sorted Set?
AZADD myzset "orange" 10
BZADD myzset 10 "orange"
CZADD myzset 5 "grape"
DZADD myzset 3 "melon"
Attempts:
2 left
💡 Hint
Check the order of arguments for ZADD command.
❓ optimization
expert
2:00remaining
Optimizing retrieval of top N recent events with timestamps in Redis
You store events with timestamps as scores in a Redis Sorted Set. You want to efficiently retrieve the 10 most recent events. Which command is best?
AZRANGE myzset -10 -1 WITHSCORES
BZRANGEBYSCORE myzset +inf -inf LIMIT 0 10
CZREVRANGE myzset 0 9 WITHSCORES
DZREM myzset 0 9
Attempts:
2 left
💡 Hint
Think about ordering and direction of retrieval.

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

  1. Step 1: Understand List behavior

    Lists in Redis keep elements in the order they were added and allow duplicates.
  2. Step 2: Compare with Sorted Set

    Sorted sets store unique elements sorted by score, so duplicates are not allowed.
  3. Final Answer:

    List -> Option B
  4. 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

  1. Step 1: Identify command for sorted set insertion

    ZADD adds elements with a score to a sorted set.
  2. Step 2: Check other commands

    LPUSH adds to list, SADD adds to set, HSET adds to hash field.
  3. Final Answer:

    ZADD myzset 10 element -> Option D
  4. 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

  1. Step 1: Understand LPUSH order

    LPUSH adds elements to the head (start) of the list, so last pushed is first.
  2. Step 2: Trace the list after pushes

    LPUSH mylist a: ["a"], then LPUSH mylist b: ["b", "a"], then LPUSH mylist c: ["c", "b", "a"]. LRANGE mylist 0 -1 returns ["c", "b", "a"].
  3. Final Answer:

    ["c", "b", "a"] -> Option C
  4. Quick Check:

    LPUSH adds to head, so newest first [OK]
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

  1. Step 1: Understand sorted set uniqueness

    Sorted sets store unique elements; adding the same element updates its score by default.
  2. 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.
  3. Final Answer:

    Sorted sets do not allow duplicate elements; score updates require special flag -> Option A
  4. 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

  1. Step 1: Analyze requirement for sorting by timestamp

    Sorted sets sort elements by numeric score, perfect for timestamps.
  2. Step 2: Handle duplicates with same timestamp

    Sorted sets require unique elements, so append unique suffix to actions to allow duplicates with same score.
  3. 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.
  4. Final Answer:

    Use a Sorted Set with timestamps as scores; append a unique suffix to actions to allow duplicates -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard