Challenge - 5 Problems
LTRIM List Capping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the result of this LTRIM command?
Given a Redis list
mylist with elements [a, b, c, d, e], what will be the content of mylist after running LTRIM mylist 1 3?Redis
LTRIM mylist 1 3
Attempts:
2 left
💡 Hint
LTRIM keeps elements from the start index to the end index inclusive.
✗ Incorrect
LTRIM trims the list to keep only elements from index 1 to 3, which are b, c, and d.
🧠 Conceptual
intermediate1:30remaining
Why use LTRIM for list capping in Redis?
What is the main reason to use the LTRIM command when managing Redis lists?
Attempts:
2 left
💡 Hint
Think about how to keep a list from growing too large.
✗ Incorrect
LTRIM is used to keep only a specific range of elements, effectively capping the list size by removing older or unwanted elements.
📝 Syntax
advanced2:00remaining
Which LTRIM command syntax correctly caps a list to the last 100 elements?
You want to keep only the last 100 elements of a Redis list named
events. Which command correctly achieves this?Attempts:
2 left
💡 Hint
Negative indexes count from the end of the list.
✗ Incorrect
Using
LTRIM events -100 -1 keeps the last 100 elements because -100 is the 100th from the end and -1 is the last element.🔧 Debug
advanced2:00remaining
Why does this LTRIM command not cap the list as expected?
You run
LTRIM logs 1 100 on a Redis list logs with 150 elements, but the list now has only 100 elements afterward. Why?Attempts:
2 left
💡 Hint
Remember how Redis indexes list elements.
✗ Incorrect
LTRIM keeps elements from index 1 to 100, which is 100 elements but excludes the first element. Elements beyond index 100 are also removed, capping the list to exactly 100 elements.
❓ optimization
expert3:00remaining
Efficiently capping a high-traffic Redis list with LTRIM
You have a Redis list
chat_messages that receives thousands of new messages per second. To keep only the latest 1000 messages, which approach is the most efficient and safe to avoid race conditions?Attempts:
2 left
💡 Hint
Think about atomicity and command order.
✗ Incorrect
Using LPUSH and LTRIM together in a single transaction or pipeline ensures the list is capped immediately after insertion without race conditions.