LRANGE for reading elements in Redis - Time & Space Complexity
When using Redis to read parts of a list, it's important to know how the time it takes grows as the list gets bigger.
We want to understand how the LRANGE command's work changes with the size of the list and the requested range.
Analyze the time complexity of the following Redis command.
LRANGE mylist 0 99
This command reads the first 100 elements from the list named 'mylist'.
Look at what repeats when LRANGE runs.
- Primary operation: Reading each element in the requested range from the list.
- How many times: Once for each element in the range (here, 100 times).
The time to run LRANGE grows with how many elements you ask for, not the total list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The work grows directly with the number of elements requested.
Time Complexity: O(k)
This means the time depends on how many elements you want to read, not the whole list size.
[X] Wrong: "LRANGE always takes time based on the total list size."
[OK] Correct: Actually, LRANGE only reads the elements you ask for, so the time depends on the range size, not the full list.
Understanding how LRANGE scales helps you explain how Redis handles list reads efficiently, a useful skill for real projects and interviews.
What if we changed LRANGE to read the entire list instead of a part? How would the time complexity change?
Practice
LRANGE mylist 0 -1 do?Solution
Step 1: Understand LRANGE parameters
The commandLRANGE key start stopreturns elements from indexstarttostopinclusive.Step 2: Interpret 0 and -1 indexes
Index 0 is the first element, and -1 means the last element, so it returns the whole list.Final Answer:
Returns all elements in the list mylist. -> Option CQuick Check:
LRANGE mylist 0 -1 = all elements [OK]
- Thinking -1 excludes the last element
- Confusing LRANGE with LLEN
- Assuming LRANGE deletes elements
tasks using LRANGE?Solution
Step 1: Understand zero-based indexing
Redis list indexes start at 0, so the first element is index 0.Step 2: Calculate indexes for first 3 elements
First 3 elements are at indexes 0, 1, and 2, so LRANGE should be 0 to 2.Final Answer:
LRANGE tasks 0 2 -> Option DQuick Check:
First 3 elements = indexes 0 to 2 [OK]
- Starting at index 1 instead of 0
- Using stop index as 3 which returns 4 elements
- Using negative indexes incorrectly
colors with elements ["red", "green", "blue", "yellow", "purple"], what is the result of LRANGE colors 1 3?Solution
Step 1: Identify elements by index
Indexes: 0="red", 1="green", 2="blue", 3="yellow", 4="purple".Step 2: Select elements from index 1 to 3
Elements are "green", "blue", and "yellow".Final Answer:
["green", "blue", "yellow"] -> Option BQuick Check:
LRANGE colors 1 3 = green, blue, yellow [OK]
- Including index 0 when start is 1
- Using stop index as exclusive instead of inclusive
- Confusing element order
LRANGE mylist 2 0 on a Redis list. What is the problem with this command?Solution
Step 1: Check index order in LRANGE
LRANGE expects start index less than or equal to stop index to return elements.Step 2: Analyze given indexes
Start is 2, stop is 0, so start > stop, which returns an empty list.Final Answer:
The start index is greater than the stop index, so it returns an empty list. -> Option AQuick Check:
Start > stop means empty result [OK]
- Assuming negative indexes are invalid syntax
- Expecting reversed range to return elements
- Confusing LRANGE with deletion commands
logs with 11 elements. You want to get the last 5 elements using LRANGE. Which command is correct?Solution
Step 1: Understand negative indexes in LRANGE
Negative indexes count from the end: -1 is last element, -5 is fifth from last.Step 2: Select last 5 elements using negative indexes
LRANGE logs -5 -1 returns elements from fifth last to last, i.e., last 5 elements.Step 3: Check other options
LRANGE logs 5 9 uses positive indexes 5 to 9 which gets elements 5-9, but for 11 elements the last 5 are indexes 6-10, so incorrect. Negative indexes are safer if length changes.Final Answer:
LRANGE logs -5 -1 -> Option AQuick Check:
Negative indexes -5 to -1 = last 5 elements [OK]
- Using wrong negative indexes like -4 to 0
- Confusing zero-based positive indexes with negative
- Assuming LRANGE excludes stop index
Ready to prove it?
Pass the test and this pattern moves unknown
