0
0
Redisquery~20 mins

LTRIM for list capping in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LTRIM List Capping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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
A[a, d, e]
B[a, b, c]
C[c, d, e]
D[b, c, d]
Attempts:
2 left
💡 Hint
LTRIM keeps elements from the start index to the end index inclusive.
🧠 Conceptual
intermediate
1:30remaining
Why use LTRIM for list capping in Redis?
What is the main reason to use the LTRIM command when managing Redis lists?
ATo limit the list size by removing older elements and keep only the most recent ones.
BTo sort the list elements alphabetically.
CTo convert the list into a set.
DTo duplicate the list elements for backup.
Attempts:
2 left
💡 Hint
Think about how to keep a list from growing too large.
📝 Syntax
advanced
2: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?
ALTRIM events -100 -1
BLTRIM events 0 99
CLTRIM events 1 100
DLTRIM events -99 0
Attempts:
2 left
💡 Hint
Negative indexes count from the end of the list.
🔧 Debug
advanced
2: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?
ALTRIM requires negative indexes to work properly.
BLTRIM does not modify the list if the end index is greater than the list length.
CLTRIM uses zero-based indexing, so starting at 1 skips the first element and trims everything after index 100, capping the list at 100 elements.
DLTRIM only works on sets, not lists.
Attempts:
2 left
💡 Hint
Remember how Redis indexes list elements.
optimization
expert
3: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?
AUse <code>LTRIM chat_messages 0 999</code> before every <code>LPUSH</code> command.
BRun <code>LPUSH chat_messages new_message</code> followed immediately by <code>LTRIM chat_messages 0 999</code> in a single Redis transaction or pipeline.
CUse <code>LRANGE chat_messages 0 999</code> to get the latest messages and delete the rest manually.
DRun <code>LPUSH chat_messages new_message</code> and then run <code>LTRIM chat_messages 0 999</code> as separate commands with delay.
Attempts:
2 left
💡 Hint
Think about atomicity and command order.