Bird
Raised Fist0
Rubyprogramming~20 mins

Lazy enumerators in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Lazy Enumerator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of lazy enumerator with chained operations
What is the output of this Ruby code using lazy enumerators?
Ruby
result = (1..Float::INFINITY).lazy.select { |x| x % 3 == 0 }.map { |x| x * 2 }.first(5)
puts result.inspect
A[1, 2, 3, 4, 5]
B[3, 6, 9, 12, 15]
C[2, 4, 6, 8, 10]
D[6, 12, 18, 24, 30]
Attempts:
2 left
💡 Hint
Remember that lazy enumerators only compute values as needed and the select filters multiples of 3.
🧠 Conceptual
intermediate
1:30remaining
Behavior of lazy enumerators with infinite sequences
Which statement best describes how Ruby's lazy enumerators handle infinite sequences?
AThey cannot be used with infinite sequences.
BThey compute elements only when needed, avoiding infinite loops.
CThey compute all elements at once, causing memory overflow.
DThey always return an array immediately.
Attempts:
2 left
💡 Hint
Think about how lazy evaluation delays computation.
🔧 Debug
advanced
1:30remaining
Identify the error in lazy enumerator chaining
What error does this Ruby code raise?
Ruby
result = (1..10).lazy.map { |x| x * 2 }.select { |x| x > 10 }
puts result
ARuntimeError: infinite loop detected
BTypeError: no implicit conversion of nil into Integer
CNo error, prints a lazy enumerator object
DArgumentError: wrong number of arguments (given 0, expected 1)
Attempts:
2 left
💡 Hint
Printing a lazy enumerator does not execute it.
❓ Predict Output
advanced
2:00remaining
Output of lazy enumerator with take and map
What is the output of this Ruby code?
Ruby
result = (1..Float::INFINITY).lazy.map { |x| x * x }.take(4).force
puts result.inspect
A[1, 4, 9, 16]
B[1, 2, 3, 4]
C[1, 8, 27, 64]
D[2, 4, 6, 8]
Attempts:
2 left
💡 Hint
The map squares each number, take limits to first 4, force evaluates the lazy enumerator.
📝 Syntax
expert
1:30remaining
Which option causes a syntax error with lazy enumerators?
Which of these Ruby code snippets will cause a syntax error?
A(1..10).lazy.select { |x| x.even? }.map ->(x) { x * 3 }
B(1..10).lazy.select { |x| x.even? }.map { |x| x * 3 }
C(1..10).lazy.select { |x| x.even? }.map { |x| x * 3 }.force
D(1..10).lazy.select { |x| x.even? }.map { |x| x * 3 }.first(3)
Attempts:
2 left
💡 Hint
Check the syntax of the block passed to map.

Practice

(1/5)
1. What is the main benefit of using .lazy enumerators in Ruby?
easy
A. They sort elements in ascending order by default.
B. They immediately compute all elements for faster access.
C. They convert arrays into hashes automatically.
D. They delay computation until results are needed, saving memory and time.

Solution

  1. Step 1: Understand what lazy enumerators do

    Lazy enumerators delay the processing of elements until you actually ask for them, which helps save memory and time especially with large or infinite collections.
  2. Step 2: Compare options with this behavior

    Only They delay computation until results are needed, saving memory and time. describes this delaying behavior. Other options describe immediate computation, conversion, or sorting, which are not the main features of lazy enumerators.
  3. Final Answer:

    They delay computation until results are needed, saving memory and time. -> Option D
  4. Quick Check:

    Lazy enumerators delay work = A [OK]
Hint: Lazy means delay work until needed, not immediate [OK]
Common Mistakes:
  • Thinking lazy enumerators compute all elements immediately
  • Confusing lazy enumerators with sorting or conversion methods
  • Assuming lazy enumerators change data structure type
2. Which of the following is the correct way to create a lazy enumerator from an array [1, 2, 3] in Ruby?
easy
A. [1, 2, 3].to_lazy
B. lazy([1, 2, 3])
C. [1, 2, 3].lazy
D. Array.lazy([1, 2, 3])

Solution

  1. Step 1: Recall the syntax for creating lazy enumerators

    In Ruby, calling .lazy on an enumerable creates a lazy enumerator.
  2. Step 2: Check each option's syntax

    Only [1, 2, 3].lazy is valid Ruby syntax. The others are not valid methods or functions.
  3. Final Answer:

    [1, 2, 3].lazy -> Option C
  4. Quick Check:

    Use .lazy on enumerable = A [OK]
Hint: Use .lazy method on enumerable to create lazy enumerator [OK]
Common Mistakes:
  • Trying to call non-existent methods like to_lazy
  • Using lazy as a standalone function
  • Confusing class methods with instance methods
3. What is the output of the following Ruby code?
result = (1..Float::INFINITY).lazy.select { |x| x % 3 == 0 }.map { |x| x * 2 }.first(5)
puts result.inspect
medium
A. [3, 6, 9, 12, 15]
B. [6, 12, 18, 24, 30]
C. [2, 4, 6, 8, 10]
D. RuntimeError: infinite loop

Solution

  1. Step 1: Understand the lazy chain

    The code creates an infinite range from 1 upwards, then lazily selects numbers divisible by 3, then doubles them, and finally takes the first 5 results.
  2. Step 2: Calculate the first 5 values

    Numbers divisible by 3 starting from 1 are 3, 6, 9, 12, 15. Doubling each gives 6, 12, 18, 24, 30.
  3. Final Answer:

    [6, 12, 18, 24, 30] -> Option B
  4. Quick Check:

    Lazy select multiples of 3, map *2, first 5 = B [OK]
Hint: Trace lazy steps and apply first(n) to get output [OK]
Common Mistakes:
  • Forgetting to double the numbers after selection
  • Confusing selected numbers with original range
  • Expecting an error from infinite range without lazy
4. Identify the error in this Ruby code using lazy enumerators:
numbers = (1..10).lazy.map { |n| n * 2 }
puts numbers.take(5)
medium
A. The take method should be called with .to_a to get results.
B. The code will run fine and print the first 5 doubled numbers.
C. The code will raise a syntax error because lazy is not defined.
D. Calling take returns an Enumerator, not an Array, causing puts to raise an error.

Solution

  1. Step 1: Understand what take returns on a lazy enumerator

    Calling take(n) on a lazy enumerator returns another lazy enumerator, not an array.
  2. Step 2: How to get actual values

    To get an array of results, you must call to_a after take(5), like numbers.take(5).to_a.
  3. Final Answer:

    The take method should be called with .to_a to get results. -> Option A
  4. Quick Check:

    Use take(n).to_a to get array from lazy enumerator [OK]
Hint: Add .to_a after take(n) to get array output [OK]
Common Mistakes:
  • Expecting take(n) alone to return an array
  • Confusing lazy enumerator with normal enumerator
  • Assuming lazy is undefined or causes syntax error
5. Given a large array arr = (1..1000000).to_a, which Ruby code snippet efficiently finds the first 3 even numbers greater than 500000 using lazy enumerators?
hard
A. arr.lazy.select { |x| x > 500000 && x.even? }.first(3)
B. arr.select { |x| x > 500000 && x.even? }.first(3)
C. arr.lazy.map { |x| x * 2 }.first(3)
D. arr.take(3).lazy.select(&:even?)

Solution

  1. Step 1: Understand the goal and data size

    We want the first 3 even numbers greater than 500,000 from a large array. Using lazy enumerators avoids processing the entire array.
  2. Step 2: Analyze each option

    arr.lazy.select { |x| x > 500000 && x.even? }.first(3) uses lazy, filters with select for the condition, then takes first 3 - efficient and correct.
    arr.select { |x| x > 500000 && x.even? }.first(3) processes entire array eagerly, inefficient.
    arr.lazy.map { |x| x * 2 }.first(3) maps all elements, not filtering by condition.
    arr.take(3).lazy.select(&:even?) takes first 3 elements then filters, which misses numbers > 500,000.
  3. Final Answer:

    arr.lazy.select { |x| x > 500000 && x.even? }.first(3) -> Option A
  4. Quick Check:

    Lazy select with condition then first(3) = C [OK]
Hint: Use lazy select with condition, then first(n) for efficiency [OK]
Common Mistakes:
  • Using eager select causing slow processing
  • Filtering after taking first 3 elements incorrectly
  • Using map instead of select for filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard