Compact for removing nil values in Ruby - Time & Space Complexity
We want to understand how long it takes to remove nil values from a list using the compact method.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
array = [1, nil, 2, nil, 3, nil]
clean_array = array.compact
puts clean_array.inspect
This code removes all nil values from the array and prints the cleaned array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element in the array to see if it is nil.
- How many times: Once for every element in the array.
As the array gets bigger, the method looks at each element one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows directly with the size of the array.
Time Complexity: O(n)
This means the time to remove nil values grows in a straight line as the array gets bigger.
[X] Wrong: "compact removes nil values instantly, no matter the array size."
[OK] Correct: The method must check each element to know if it is nil, so bigger arrays take more time.
Understanding how simple methods like compact work helps you explain your code clearly and shows you know how performance changes with data size.
"What if we used a method that removes nil values but also duplicates? How would the time complexity change?"
Practice
What does the Ruby method compact do when called on an array?
Solution
Step 1: Understand the purpose of
Thecompactcompactmethod creates a new array by removing allnilvalues from the original array.Step 2: Compare with other options
Other options describe different array methods or behaviors, but onlycompactremovesnilvalues without modifying the original array.Final Answer:
It returns a new array without any nil values. -> Option AQuick Check:
compact removes nil values = B [OK]
- Confusing compact with uniq (removes duplicates)
- Thinking compact sorts the array
- Believing compact replaces nil with zero
Which of the following is the correct syntax to remove nil values from an array arr without changing arr itself?
arr = [1, nil, 2, nil, 3]
Solution
Step 1: Identify method that returns new array without nil
compactreturns a new array withoutnilvalues and does not modify the original array.Step 2: Understand difference with
compact!compact!modifies the original array, so it changesarr.delete(nil)removes nil but modifiesarr.remove_nilis not a Ruby method.Final Answer:
arr.compact -> Option DQuick Check:
compact returns new array, compact! modifies [OK]
- Using compact! when original array should stay unchanged
- Using delete(nil) which modifies the original array
- Assuming remove_nil is a valid Ruby method
What is the output of the following Ruby code?
arr = [nil, 5, nil, 10] new_arr = arr.compact puts new_arr.inspect puts arr.inspect
Solution
Step 1: Understand what
compactreturnscompactreturns a new array withoutnilvalues, sonew_arris [5, 10].Step 2: Check if original array changes
The original arrayarrremains unchanged as [nil, 5, nil, 10].Final Answer:
[5, 10] [nil, 5, nil, 10] -> Option AQuick Check:
compact returns new array, original unchanged = A [OK]
- Assuming original array changes after compact
- Confusing compact with compact! which modifies original
- Misreading output order
Consider this Ruby code snippet:
arr = [1, nil, 2, nil, 3] arr.compact puts arr.inspect
Why does the output still include nil values?
Solution
Step 1: Understand
compactbehaviorcompactreturns a new array withoutnilvalues but does not change the original array.Step 2: Check why
Sincearrstill hasnilarr.compactwas called without assignment,arrremains unchanged and still containsnil.Final Answer:
Because compact returns a new array but does not modify arr. -> Option CQuick Check:
compact returns new array, original unchanged = D [OK]
- Thinking compact modifies original array
- Believing puts cannot print arrays properly
- Assuming arr was reassigned automatically
You have an array data = [nil, 0, false, '', nil, 5]. You want to remove only nil values but keep 0, false, and empty strings. Which code correctly does this?
Solution
Step 1: Understand what
compactdoescompactremoves onlynilvalues and keeps all other values including0,false, and empty strings.Step 2: Analyze other options
select { |v| v }removes all falsy values (nil,false,0is truthy in Ruby but empty string is truthy), so it removesfalse.reject!(&:nil?)modifies the original array.delete_if { |v| v.nil? }modifies original array.Step 3: Choose best option for new array without nil
compactis simplest and returns new array without modifying original.Final Answer:
clean_data = data.compact -> Option BQuick Check:
compact removes nil only, keeps false and empty string = C [OK]
- Using select which removes false values
- Using delete_if which modifies original array
- Using reject! which modifies the original array
Ready to prove it?
Pass the test and this pattern moves unknown
