Bird
Raised Fist0
Redisquery~20 mins

Time-based event queues in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Time-based Event Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Retrieve events scheduled before a specific timestamp
Given a Redis sorted set named events where the score is the event timestamp, which command returns all events scheduled before timestamp 1650000000?
AZRANGEBYSCORE events -inf 1650000000
BZRANGEBYSCORE events 1650000000 +inf
CZRANGE events 0 1650000000
DZREVRANGEBYSCORE events 1650000000 -inf
Attempts:
2 left
💡 Hint
Remember that scores in sorted sets represent timestamps and you want all events with scores less than or equal to 1650000000.
📝 Syntax
intermediate
1:30remaining
Identify the correct syntax to add a timed event
Which Redis command correctly adds an event named event42 scheduled at timestamp 1680000000 to a sorted set events?
AZADD events 1680000000 event42
BZADD events event42 1680000000
CZADD events (1680000000) event42
DZADD events event42
Attempts:
2 left
💡 Hint
ZADD syntax requires the score first, then the member.
❓ optimization
advanced
2:00remaining
Efficiently removing expired events
You want to remove all events from the events sorted set that have timestamps less than the current time 1690000000. Which command is the most efficient to do this?
AZREM events 1690000000
BZREMRANGEBYSCORE events -inf (1690000000
CZREM events -inf 1690000000
DZREMRANGEBYRANK events 0 1690000000
Attempts:
2 left
💡 Hint
Removing by score range is best for timestamp-based removals.
🔧 Debug
advanced
2:00remaining
Why does this time-based event retrieval return no results?
A developer runs this command to get events before timestamp 1700000000: ZRANGEBYSCORE events (1700000000 -inf. Why does it return no results?
AThe command syntax is correct; the sorted set is empty
BParentheses around 1700000000 exclude that score, causing no matches
CThe score range is reversed; min should be less than max
DZRANGEBYSCORE does not support negative infinity
Attempts:
2 left
💡 Hint
Check the order of min and max arguments in ZRANGEBYSCORE.
🧠 Conceptual
expert
2:30remaining
Choosing the best data structure for time-based event queues
Which Redis data structure is best suited for implementing a time-based event queue where events must be processed in order of their scheduled timestamps?
AList because it maintains insertion order and supports push/pop operations
BSet because it stores unique members without order
CHash because it stores key-value pairs for fast lookup
DSorted Set (ZSET) because it stores members with scores and supports range queries by score
Attempts:
2 left
💡 Hint
Consider which structure supports ordering by timestamp and efficient range queries.

Practice

(1/5)
1. What is the main purpose of using a Redis sorted set for a time-based event queue?
easy
A. To store events in a hash for quick lookup
B. To store events with timestamps as scores for ordered retrieval
C. To store only unique event names without timestamps
D. To store events without any order

Solution

  1. Step 1: Understand Redis sorted sets

    Redis sorted sets store members with a score that determines their order.
  2. Step 2: Apply to time-based event queues

    Using timestamps as scores allows events to be retrieved in chronological order.
  3. Final Answer:

    To store events with timestamps as scores for ordered retrieval -> Option B
  4. Quick Check:

    Sorted sets + timestamps = ordered event queue [OK]
Hint: Remember: sorted sets order by score (timestamp) [OK]
Common Mistakes:
  • Thinking sorted sets don't keep order
  • Confusing hashes with sorted sets
  • Ignoring the timestamp as score
2. Which Redis command correctly adds an event with a timestamp score to a sorted set named events?
easy
A. SADD events 1625097600 "event1"
B. HSET events 1625097600 "event1"
C. ZADD events 1625097600 "event1"
D. LPUSH events 1625097600 "event1"

Solution

  1. Step 1: Identify the command for sorted sets

    ZADD adds members with scores to sorted sets; others are for different data types.
  2. Step 2: Check syntax correctness

    ZADD events 1625097600 "event1" adds "event1" with score 1625097600 to sorted set events.
  3. Final Answer:

    ZADD events 1625097600 "event1" -> Option C
  4. Quick Check:

    ZADD = add to sorted set [OK]
Hint: Use ZADD for sorted sets, not SADD or HSET [OK]
Common Mistakes:
  • Using SADD which is for sets without scores
  • Using HSET which is for hashes
  • Using LPUSH which is for lists
3. Given the sorted set events with members and scores:
{"event1": 1000, "event2": 1500, "event3": 2000}
What will the command ZREVRANGEBYSCORE events 1500 -inf return?
medium
A. ["event1"]
B. ["event3", "event2", "event1"]
C. ["event1", "event2"]
D. ["event2", "event1"]

Solution

  1. Step 1: Understand ZREVRANGEBYSCORE command

    This command returns members with scores between max and min in descending order.
  2. Step 2: Apply to given scores

    Scores ≤ 1500 are event1 (1000) and event2 (1500). Descending order: event2, event1.
  3. Final Answer:

    ["event2", "event1"] -> Option D
  4. Quick Check:

    Descending scores ≤ 1500 = event2, event1 [OK]
Hint: ZREVRANGEBYSCORE returns descending order by score [OK]
Common Mistakes:
  • Confusing ascending with descending order
  • Including events with scores above max
  • Misreading score boundaries
4. You run this Redis command to fetch due events:
ZREVRANGEBYSCORE events 1625097600 -inf
But you notice the same events are returned repeatedly. What is the likely mistake?
medium
A. Not removing processed events from the sorted set
B. Using ZREVRANGEBYSCORE instead of ZRANGEBYSCORE
C. Using the wrong key name for the sorted set
D. Using a timestamp that is too high

Solution

  1. Step 1: Understand event queue behavior

    After processing events, they must be removed to avoid duplicates.
  2. Step 2: Identify cause of repeated events

    If events are not removed, the same events appear on every fetch.
  3. Final Answer:

    Not removing processed events from the sorted set -> Option A
  4. Quick Check:

    Remove events after processing to avoid repeats [OK]
Hint: Always delete processed events from sorted set [OK]
Common Mistakes:
  • Thinking order command causes repeats
  • Ignoring event removal step
  • Assuming wrong key name causes repeats
5. You want to implement a Redis time-based event queue that fetches all events due up to now and removes them atomically to avoid duplicates. Which sequence of commands is best?
hard
A. Use ZRANGEBYSCORE to get events ≤ now, then ZREM to remove them
B. Use ZREVRANGEBYSCORE to get events ≥ now, then ZREM to remove them
C. Use ZRANGEBYSCORE with a future timestamp, then LPUSH to add events
D. Use SPOP to pop events from a set

Solution

  1. Step 1: Fetch due events with ZRANGEBYSCORE

    ZRANGEBYSCORE with max = current timestamp fetches all events due up to now.
  2. Step 2: Remove fetched events with ZREM

    Removing them prevents duplicates; doing both steps atomically avoids race conditions.
  3. Final Answer:

    Use ZRANGEBYSCORE to get events ≤ now, then ZREM to remove them -> Option A
  4. Quick Check:

    Fetch ≤ now + remove = atomic event processing [OK]
Hint: Fetch with ZRANGEBYSCORE ≤ now, then remove with ZREM [OK]
Common Mistakes:
  • Using ZREVRANGEBYSCORE with wrong score range
  • Using sets or lists instead of sorted sets
  • Not removing events after fetching
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