Bird
Raised Fist0
Redisquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Redis command LTRIM mylist 0 9 do to the list mylist?
easy
A. Clears the entire list.
B. Removes the first 10 elements and keeps the rest.
C. Keeps only the last 10 elements of the list and removes the rest.
D. Keeps only the first 10 elements of the list and removes the rest.

Solution

  1. Step 1: Understand LTRIM parameters

    The command LTRIM mylist 0 9 keeps elements from index 0 to 9 inclusive.
  2. Step 2: Effect on list size

    This means only the first 10 elements remain; all others are removed.
  3. Final Answer:

    Keeps only the first 10 elements of the list and removes the rest. -> Option D
  4. Quick Check:

    LTRIM 0 9 keeps first 10 elements [OK]
Hint: LTRIM 0 9 means keep first 10 items only [OK]
Common Mistakes:
  • Confusing start and stop indexes
  • Thinking it removes first 10 instead of keeping them
  • Assuming it keeps last 10 elements
2. Which of the following is the correct syntax to keep only the last 5 elements of a Redis list named tasks using LTRIM?
easy
A. LTRIM tasks -5 -1
B. LTRIM tasks 0 4
C. LTRIM tasks 5 -1
D. LTRIM tasks -1 -5

Solution

  1. Step 1: Understand negative indexes in LTRIM

    Negative indexes count from the end: -1 is last element, -5 is fifth from last.
  2. Step 2: Select correct range for last 5 elements

    Using LTRIM tasks -5 -1 keeps elements from fifth last to last, i.e., last 5 elements.
  3. Final Answer:

    LTRIM tasks -5 -1 -> Option A
  4. Quick Check:

    Negative indexes keep last elements [OK]
Hint: Use negative indexes to keep last elements [OK]
Common Mistakes:
  • Using positive indexes to try to keep last elements
  • Reversing negative indexes order
  • Using invalid syntax with start > stop
3. Given a Redis list colors with elements ["red", "green", "blue", "yellow", "purple"], what will be the list content after executing LTRIM colors 1 3?
medium
A. ["green", "blue", "yellow"]
B. ["blue", "yellow", "purple"]
C. ["red", "green", "blue"]
D. ["yellow", "purple"]

Solution

  1. Step 1: Identify indexes to keep

    LTRIM colors 1 3 keeps elements from index 1 to 3 inclusive.
  2. Step 2: Extract elements at those indexes

    Indexes 1 to 3 are "green", "blue", "yellow".
  3. Final Answer:

    ["green", "blue", "yellow"] -> Option A
  4. Quick Check:

    LTRIM keeps specified index range [OK]
Hint: Indexes start at 0; keep elements between start and stop [OK]
Common Mistakes:
  • Including element at index 0 by mistake
  • Confusing index ranges
  • Assuming LTRIM removes elements inside range
4. You run LTRIM mylist 5 2 on a Redis list. What will happen?
medium
A. The command will return an error due to invalid range.
B. The list will be emptied because start index is greater than stop index.
C. The list will be trimmed to elements from index 5 to 2, keeping those elements.
D. The list will remain unchanged.

Solution

  1. Step 1: Understand LTRIM behavior with start > stop

    If start index is greater than stop index, Redis trims the list to empty.
  2. Step 2: Apply to given command

    Since 5 > 2, LTRIM mylist 5 2 empties the list.
  3. Final Answer:

    The list will be emptied because start index is greater than stop index. -> Option B
  4. Quick Check:

    LTRIM with start > stop empties list [OK]
Hint: Start index must be ≤ stop; else list empties [OK]
Common Mistakes:
  • Expecting an error instead of empty list
  • Thinking list remains unchanged
  • Assuming it keeps elements in reverse order
5. You want to keep only the most recent 20 messages in a Redis list chatlog. Which sequence of commands correctly caps the list size after adding a new message "Hello"?
hard
A. RPUSH chatlog "Hello"; LTRIM chatlog -20 0
B. RPUSH chatlog "Hello"; LTRIM chatlog 0 19
C. LPUSH chatlog "Hello"; LTRIM chatlog 0 19
D. LPUSH chatlog "Hello"; LTRIM chatlog -20 -1

Solution

  1. Step 1: Understand list direction and adding new messages

    Using LPUSH adds new messages to the start (index 0), so recent messages are at the front.
  2. Step 2: Use LTRIM to keep first 20 elements

    To keep the most recent 20 messages, trim to indexes 0 to 19, or equivalently LTRIM chatlog 0 19. Negative indexes also work but must match direction.
  3. Step 3: Match push and trim commands

    Since LPUSH adds to front, LTRIM chatlog 0 19 keeps first 20 recent messages. LPUSH chatlog "Hello"; LTRIM chatlog -20 -1 uses LPUSH and LTRIM chatlog -20 -1, which keeps last 20 elements, but since new messages are at front, last 20 are oldest. So LPUSH chatlog "Hello"; LTRIM chatlog -20 -1 is incorrect.
  4. Step 4: Correct option

    LPUSH chatlog "Hello"; LTRIM chatlog 0 19 uses LPUSH and LTRIM chatlog 0 19, which correctly keeps the 20 most recent messages at front.
  5. Final Answer:

    LPUSH chatlog "Hello"; LTRIM chatlog 0 19 -> Option C
  6. Quick Check:

    LPUSH + LTRIM 0 19 keeps recent 20 messages [OK]
Hint: Match push direction with LTRIM range to keep recent items [OK]
Common Mistakes:
  • Using RPUSH but trimming front indexes
  • Using negative indexes incorrectly with LPUSH
  • Trimming wrong range causing oldest messages to stay
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