Accessing elements (indexing, first, last) in Ruby - Time & Space Complexity
When we get elements from a list by position, like the first or last item, it's important to know how long it takes as the list grows.
We want to find out how the time to access elements changes when the list gets bigger.
Analyze the time complexity of the following code snippet.
arr = [10, 20, 30, 40, 50]
first_element = arr.first
last_element = arr.last
third_element = arr[2]
This code gets the first, last, and third elements from an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct access to elements by position (indexing)
- How many times: Each access happens once, no loops or repeated steps
Getting an element by its position takes the same short time no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even if the list grows larger.
Time Complexity: O(1)
This means accessing any element by position takes a constant, quick amount of time regardless of list size.
[X] Wrong: "Accessing the last element takes longer because it's at the end of the list."
[OK] Correct: In Ruby arrays, the last element is stored just like any other, so getting it is just as fast as the first or any indexed element.
Knowing that accessing elements by index is fast helps you explain how your code handles data efficiently, a useful skill in many coding conversations.
"What if we used a linked list instead of an array? How would accessing the last element's time complexity change?"
Practice
arr?Solution
Step 1: Understand the purpose of
The.first.firstmethod returns the first element of an array in Ruby.Step 2: Compare with other options
arr[1]returns the second element (indexing starts at 0),arr.lastandarr[-1]return the last element.Final Answer:
arr.first -> Option DQuick Check:
First element = arr.first [OK]
- Using arr[1] instead of arr[0] for first element
- Confusing .last with .first
- Using negative index for first element
arr in Ruby?Solution
Step 1: Understand negative indexing in Ruby arrays
Negative indexes count from the end, soarr[-1]returns the last element.Step 2: Check other options
arr[0]is the first element,arr.firstalso returns first, andarr[1]is second element.Final Answer:
arr[-1] -> Option CQuick Check:
Last element = arr[-1] [OK]
- Using arr[1] thinking it's last element
- Using arr.first instead of arr.last
- Using arr[-0] which is invalid
arr = [10, 20, 30, 40, 50] puts arr[2] puts arr.first puts arr[-2]
Solution
Step 1: Evaluate
Index 2 is the third element, which is 30.arr[2]Step 2: Evaluate
arr.firstandarr[-2]arr.firstis 10 (first element),arr[-2]is second last element, 40.Final Answer:
30 10 40 -> Option BQuick Check:
Index 2 = 30, first = 10, second last = 40 [OK]
- Confusing index 2 with index 3
- Mixing up first and last elements
- Misunderstanding negative indexes
arr = [5, 10, 15] puts arr.first() puts arr[-4]
Solution
Step 1: Check
arr.first()usagefirst()can be called with or without parentheses; no error here.Step 2: Analyze
Array has 3 elements, so index -4 is out of range and returnsarr[-4]nil, which prints as empty line, no error.Final Answer:
Negative index -4 is out of range and returns nil, no error -> Option AQuick Check:
Out of range negative index returns nil [OK]
- Assuming out of range index causes error
- Thinking first() needs arguments
- Confusing nil with error
arr = ["a", "b", "c", "d", "e"], which Ruby expression returns a new array with only the first and last elements?Solution
Step 1: Understand the goal
We want a new array containing only the first and last elements.Step 2: Evaluate each option
[arr.first, arr.last]creates an array witharr.firstandarr.last.arr[0, 1] + arr[-1]tries to add an array and a string (wrong).arr[0] + arr[-1]adds two strings, resulting in concatenation, not array.arr.first + arr.lastadds two strings, also concatenation.Final Answer:
[arr.first, arr.last] -> Option AQuick Check:
Array with first and last elements = [arr.first, arr.last] [OK]
- Adding strings instead of creating an array
- Using + with array and string causing error
- Forgetting to wrap elements in array brackets
Ready to prove it?
Pass the test and this pattern moves unknown
