Select/filter for filtering in Ruby - Time & Space Complexity
When we use select or filter in Ruby, we want to pick certain items from a list based on a rule.
We ask: How does the time to do this grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filtered = numbers.select do |num|
num.even?
end
This code picks all even numbers from the list called numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each number to see if it is even.
- How many times: Once for every item in the list.
As the list gets bigger, the program checks more numbers one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the size of the list.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the list gets bigger.
[X] Wrong: "Filtering only a few items means the program runs quickly no matter the list size."
[OK] Correct: The program still checks every item, even if only a few pass the filter.
Understanding how filtering scales helps you explain how your code handles bigger data smoothly.
"What if we used a method that stops checking after finding the first match? How would the time complexity change?"
Practice
select method do in Ruby when used on an array?Solution
Step 1: Understand the purpose of
Theselectselectmethod filters elements from an array based on a condition given in a block.Step 2: Check the behavior of
It returns a new array containing only elements for which the block returns true, without changing the original array.selectFinal Answer:
It returns a new array with elements that meet the condition in the block. -> Option AQuick Check:
selectfilters and returns new array [OK]
- Thinking select modifies the original array
- Confusing select with count or sort
- Believing select returns a single element
nums using select?Solution
Step 1: Review Ruby block syntax for select
Theselectmethod requires a block with a parameter and a condition, usually written as{ |param| condition }.Step 2: Identify correct syntax
nums.select { |n| n % 2 == 0 } uses correct block syntax with|n|and conditionn % 2 == 0. Other options have syntax errors or wrong block usage.Final Answer:
nums.select { |n| n % 2 == 0 } -> Option DQuick Check:
Correct block syntax = nums.select { |n| n % 2 == 0 } [OK]
- Missing vertical bars around block variable
- Using wrong block delimiters or syntax
- Passing argument inside select parentheses
numbers = [1, 2, 3, 4, 5]
even_numbers = numbers.select { |num| num.even? }
puts even_numbers.inspectSolution
Step 1: Understand the
The block usesselectblock conditionnum.even?which returns true for even numbers only.Step 2: Filter the array based on condition
From [1, 2, 3, 4, 5], only 2 and 4 are even, so the new array is [2, 4].Final Answer:
[2, 4] -> Option CQuick Check:
select with even? filters evens = [2, 4] [OK]
- Confusing even? with odd?
- Expecting original array unchanged output
- Forgetting select returns new filtered array
words = ["cat", "dog", "elephant", "fox"] long_words = words.select |word| word.length > 3 puts long_words.inspect
Solution
Step 1: Identify block syntax error
The code usesselect |word| word.length > 3without curly braces or do..end, which is invalid syntax.Step 2: Correct block syntax
It should beselect { |word| word.length > 3 }orselect do |word| word.length > 3 end.Final Answer:
Missing curly braces or do..end around the block -> Option BQuick Check:
Block syntax requires braces or do..end [OK]
- Omitting block delimiters
- Confusing length and size methods (both work but length is correct)
- Thinking select and filter are different methods
scores = { alice: 50, bob: 75, carol: 40, dave: 90 }, how do you select only the key-value pairs where the score is 50 or more?Solution
Step 1: Understand hash select/filter usage
When usingselecton a hash, the block receives two parameters: key and value.Step 2: Apply condition on value (score)
We want pairs where score >= 50, so the block should checkscore >= 50.Step 3: Identify correct syntax
scores.select { |name, score| score >= 50 } correctly uses|name, score| score >= 50. The other options have wrong parameters or conditions.Final Answer:
scores.select { |name, score| score >= 50 } -> Option AQuick Check:
Hash select with two block params and value condition [OK]
- Using only one block variable for hash select
- Checking condition on key instead of value
- Confusing select and filter with wrong parameters
Ready to prove it?
Pass the test and this pattern moves unknown
