Reject for inverse filtering in Ruby - Time & Space Complexity
We want to understand how the time needed to run the reject for inverse filtering method changes as the input size grows.
Specifically, how does the number of operations increase when we process more data?
Analyze the time complexity of the following code snippet.
def reject_inverse_filtering(data, threshold)
result = []
data.each do |value|
inverse = 1.0 / value
if inverse > threshold
result << value
end
end
result
end
This code goes through each number in the data, calculates its inverse, and keeps the original number if the inverse is bigger than a threshold.
- Primary operation: Looping through each element in the data array.
- How many times: Once for every element in the input data.
As the input data gets bigger, the code does more work by checking each new element once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and calculations |
| 100 | About 100 checks and calculations |
| 1000 | About 1000 checks and calculations |
Pattern observation: The work grows directly in proportion to the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items to check.
[X] Wrong: "Calculating the inverse inside the loop makes the code slower than linear time."
[OK] Correct: Each inverse calculation is a simple step done once per item, so it still grows linearly with input size.
Understanding how loops affect time helps you explain your code clearly and shows you can think about efficiency in real tasks.
"What if we added a nested loop inside the existing loop? How would the time complexity change?"
Practice
reject do when called on an array?Solution
Step 1: Understand the purpose of
Therejectrejectmethod filters elements by removing those that satisfy the condition.Step 2: Compare with other methods
Unlikeselect, which keeps matching elements,rejectexcludes them and returns a new array.Final Answer:
It returns a new array excluding elements that match the given condition. -> Option DQuick Check:
reject excludes matching elements = A [OK]
- Thinking reject modifies the original array
- Confusing reject with select
- Assuming reject returns a single element
reject on an array nums to remove even numbers?Solution
Step 1: Identify correct block syntax for reject
Therejectmethod requires a block with{ |var| condition }ordo |var| ... end.Step 2: Check each option's syntax
nums.reject { |n| n % 2 == 0 } uses correct block syntax with braces and block variable. nums.reject(n % 2 == 0) misses block. nums.reject => n { n % 2 == 0 } uses invalid syntax. nums.reject do n; n % 2 == 0 end misses block variable indosyntax.Final Answer:
nums.reject { |n| n % 2 == 0 } -> Option AQuick Check:
Correct block syntax with |var| = D [OK]
- Omitting block variable in block
- Passing condition as method argument
- Using incorrect block syntax
arr = [1, 2, 3, 4, 5]
result = arr.reject { |x| x > 3 }
puts result.inspectSolution
Step 1: Understand the reject condition
The block removes elements wherex > 3is true, so numbers 4 and 5 are removed.Step 2: Determine remaining elements
Elements 1, 2, and 3 remain because they do not satisfy the condition.Final Answer:
[1, 2, 3] -> Option AQuick Check:
reject removes >3, so [1,2,3] remain = A [OK]
- Confusing reject with select
- Expecting removed elements as output
- Ignoring block condition logic
data. What is the error?
data = [1, nil, 2, nil, 3] clean = data.reject do |x| x = nil end puts clean.inspect
Solution
Step 1: Identify the block logic error
The block usesx = nilwhich assigns nil to x instead of checking if x equals nil.Step 2: Correct the comparison operator
It should usex == nilto test if x is nil for rejection.Final Answer:
The block uses assignment (=) instead of comparison (==). -> Option CQuick Check:
Use == for comparison inside reject blocks [OK]
- Using = instead of == in block condition
- Thinking reject modifies original array
- Confusing reject with select
scores = { alice: 90, bob: 55, carol: 75, dave: 40 }, how can you use reject to create a new hash excluding all entries with scores below 60?Solution
Step 1: Understand hash reject block parameters
When rejecting from a hash, the block receives two parameters: key and value.Step 2: Apply condition on values
We want to reject entries where the score (value) is less than 60, so the block should checkscore < 60.Step 3: Verify options
scores.reject { |name, score| score < 60 } correctly uses both key and value parameters and checks the score. The other options either use only one block parameter or check the key instead of the value.Final Answer:
scores.reject { |name, score| score < 60 } -> Option BQuick Check:
Reject hash entries by value condition = B [OK]
- Using only one block parameter for hash reject
- Checking key instead of value
- Confusing reject with select for filtering
Ready to prove it?
Pass the test and this pattern moves unknown
