Challenge - 5 Problems
Lazy Enumerator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Remember that lazy enumerators only compute values as needed and the select filters multiples of 3.
✗ Incorrect
The code selects numbers divisible by 3 from an infinite range, then doubles them. The first five such numbers are 3,6,9,12,15, doubled to 6,12,18,24,30.
🧠 Conceptual
intermediate1:30remaining
Behavior of lazy enumerators with infinite sequences
Which statement best describes how Ruby's lazy enumerators handle infinite sequences?
Attempts:
2 left
💡 Hint
Think about how lazy evaluation delays computation.
✗ Incorrect
Lazy enumerators compute elements on demand, so they can handle infinite sequences without running forever or using too much memory.
🔧 Debug
advanced1: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
Attempts:
2 left
💡 Hint
Printing a lazy enumerator does not execute it.
✗ Incorrect
The code creates a lazy enumerator and prints it. Printing a lazy enumerator shows its object representation without error.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
The map squares each number, take limits to first 4, force evaluates the lazy enumerator.
✗ Incorrect
The code squares numbers starting from 1, takes the first 4 squares, and forces evaluation to get [1,4,9,16].
📝 Syntax
expert1:30remaining
Which option causes a syntax error with lazy enumerators?
Which of these Ruby code snippets will cause a syntax error?
Attempts:
2 left
💡 Hint
Check the syntax of the block passed to map.
✗ Incorrect
Option A uses incorrect syntax for passing a lambda to map; it should be map(&lambda) or map { ... }.