Bird
Raised Fist0
Redisquery~10 mins

LREM for element removal in Redis - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to remove all occurrences of 'apple' from the list 'fruits'.

Redis
LREM fruits 0 [1]
Drag options to blanks, or click blank then click option'
A"apple"
Bapple
C'apple'
Dapple"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the element.
Using mismatched or missing quotes.
2fill in blank
medium

Complete the code to remove only the first occurrence of 'banana' from the list 'fruits'.

Redis
LREM fruits [1] "banana"
Drag options to blanks, or click blank then click option'
A-1
B0
C2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 removes all occurrences, not just the first.
Using negative numbers removes from tail to head.
3fill in blank
hard

Fix the error in the code to remove the last occurrence of 'orange' from the list 'fruits'.

Redis
LREM fruits [1] "orange"
Drag options to blanks, or click blank then click option'
A-1
B0
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 removes the first occurrence, not the last.
Using 0 removes all occurrences.
4fill in blank
hard

Fill both blanks to remove two occurrences of 'grape' starting from the tail in the list 'fruits'.

Redis
LREM fruits [1] [2]
Drag options to blanks, or click blank then click option'
A-2
B2
C"grape"
D"apple"
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive count removes from head, not tail.
Using wrong element name.
5fill in blank
hard

Fill all three blanks to remove three occurrences of 'melon' starting from the head in the list 'fruits'.

Redis
LREM [1] [2] [3]
Drag options to blanks, or click blank then click option'
Afruits
B3
C"melon"
D"banana"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong list name.
Using negative count to remove from tail.
Using wrong element name.

Practice

(1/5)
1. What does the Redis command LREM mylist 2 "apple" do?
easy
A. Removes up to 2 occurrences of "apple" from the head of the list.
B. Removes all occurrences of "apple" from the list.
C. Removes 2 elements from the tail of the list regardless of value.
D. Removes the first 2 elements of the list.

Solution

  1. Step 1: Understand the LREM command parameters

    LREM takes a key, a count, and a value. The count 2 means remove up to 2 matching elements.
  2. Step 2: Interpret the count sign and value

    Positive count removes from head to tail. So it removes up to 2 "apple" elements starting from the front.
  3. Final Answer:

    Removes up to 2 occurrences of "apple" from the head of the list. -> Option A
  4. Quick Check:

    LREM with positive count removes from head [OK]
Hint: Positive count removes from head; negative from tail [OK]
Common Mistakes:
  • Thinking count removes elements regardless of value
  • Confusing positive and negative count directions
  • Assuming count removes all matches always
2. Which of the following is the correct syntax to remove exactly 3 occurrences of "banana" from the tail of a Redis list named fruits?
easy
A. LREM fruits 3 "banana"
B. LREM fruits -3 "banana"
C. LREM fruits 0 "banana"
D. LREM fruits "banana" 3

Solution

  1. Step 1: Recall LREM syntax

    The syntax is LREM key count value. Negative count removes from tail.
  2. Step 2: Identify correct count for tail removal

    To remove 3 occurrences from tail, count must be -3.
  3. Final Answer:

    LREM fruits -3 "banana" -> Option B
  4. Quick Check:

    Negative count removes from tail [OK]
Hint: Use negative count to remove from tail [OK]
Common Mistakes:
  • Using positive count to remove from tail
  • Swapping value and count positions
  • Using zero count to remove specific elements
3. Given a Redis list colors with elements ["red", "blue", "red", "green", "red"], what will be the list after executing LREM colors 2 "red"?
medium
A. ["blue", "red", "green", "red"]
B. ["red", "blue", "green"]
C. ["blue", "green", "red"]
D. ["red", "blue", "red", "green"]

Solution

  1. Step 1: Identify elements and count

    List is ["red", "blue", "red", "green", "red"]. LREM with count 2 removes up to 2 "red" from head.
  2. Step 2: Remove first 2 "red" from head

    First "red" at index 0 removed, second "red" at index 2 removed. Remaining elements: ["blue", "green", "red"]
  3. Final Answer:

    ["blue", "green", "red"] -> Option C
  4. Quick Check:

    Removed first 2 "red" from head [OK]
Hint: Count 2 removes first 2 matches from head [OK]
Common Mistakes:
  • Removing all "red" elements instead of 2
  • Removing from tail instead of head
  • Confusing list order after removal
4. You run LREM mylist -1 "orange" but it does not remove any element. What could be the reason?
medium
A. The list mylist does not contain "orange".
B. Negative count cannot be used with LREM.
C. The command syntax is incorrect; value must come before count.
D. LREM only removes elements from the head, not the tail.

Solution

  1. Step 1: Check command syntax and usage

    LREM syntax is correct and negative count removes from tail.
  2. Step 2: Consider why no removal happened

    If no elements removed, likely the value "orange" is not in the list.
  3. Final Answer:

    The list mylist does not contain "orange". -> Option A
  4. Quick Check:

    No matching element means no removal [OK]
Hint: No matching value means LREM removes nothing [OK]
Common Mistakes:
  • Thinking negative count is invalid
  • Assuming syntax order is different
  • Believing LREM only removes from head
5. You have a Redis list tasks with elements ["email", "call", "email", "meeting", "email", "call"]. You want to remove all occurrences of "email" except the last one. Which sequence of commands achieves this?
hard
A. LREM tasks -2 "email"
B. LREM tasks 0 "email"
C. LREM tasks -5 "email"
D. LREM tasks 2 "email"

Solution

  1. Step 1: Count total "email" elements

    There are 3 "email" elements at positions 0, 2, 4.
  2. Step 2: Removal strategy

    Positive count removes matches from head-to-tail. Count 2 removes the first two "email"s, leaving the last one.
  3. Step 3: Simulate execution

    Remove index 0, list shifts to ["call", "email", "meeting", "email", "call"], then remove next match at new index 1 (orig 2). Result: ["call", "meeting", "email", "call"].
  4. Final Answer:

    LREM tasks 2 "email" -> Option D
  5. Quick Check:

    Positive count removes first matches from head [OK]
Hint: Positive count removes early matches from head [OK]
Common Mistakes:
  • Using negative count removes later matches from tail
  • Using zero count removes all occurrences
  • Using large negative count removes all if exceeds occurrences
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