Challenge - 5 Problems
Lazy Evaluation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of eager vs lazy list processing
Consider the following Kotlin code that processes a list eagerly and lazily. What is the output when running this code?
Kotlin
fun main() { val numbers = listOf(1, 2, 3, 4) val eager = numbers.map { println("Eager: $it"); it * 2 } val lazy = numbers.asSequence().map { println("Lazy: $it"); it * 2 } println("Eager result: $eager") println("Lazy result: ${lazy.toList()}") }
Attempts:
2 left
💡 Hint
Remember that eager operations run immediately, while lazy operations run when the result is requested.
✗ Incorrect
The eager map runs immediately, printing for each element before the result is printed. The lazy sequence map only runs when toList() is called, so its print statements appear just before the lazy result.
❓ Predict Output
intermediate2:00remaining
Effect of filtering on lazy vs eager collections
What is the output of this Kotlin code that filters a list eagerly and lazily?
Kotlin
fun main() { val numbers = listOf(1, 2, 3, 4) val eager = numbers.filter { println("Eager filter: $it"); it % 2 == 0 } val lazy = numbers.asSequence().filter { println("Lazy filter: $it"); it % 2 == 0 } println("Eager filtered: $eager") println("Lazy filtered: ${lazy.toList()}") }
Attempts:
2 left
💡 Hint
Filtering eagerly runs the predicate immediately, lazy filtering runs it when the result is requested.
✗ Incorrect
The eager filter prints for all elements before printing the filtered list. The lazy filter prints only when toList() is called, so prints appear after eager output but before lazy filtered list.
🔧 Debug
advanced2:00remaining
Why does this lazy sequence not print anything?
This Kotlin code uses a lazy sequence with map and filter, but nothing is printed. Why?
Kotlin
fun main() { val numbers = listOf(1, 2, 3, 4) val lazy = numbers.asSequence() .map { println("Mapping: $it"); it * 2 } .filter { println("Filtering: $it"); it > 4 } // No terminal operation here }
Attempts:
2 left
💡 Hint
Think about when lazy sequences actually run their operations.
✗ Incorrect
Lazy sequences only run their operations when a terminal operation like toList() or forEach() is called. Without it, map and filter lambdas are not executed.
❓ Predict Output
advanced2:00remaining
Output of mixed eager and lazy operations
What is the output of this Kotlin code mixing eager and lazy operations?
Kotlin
fun main() { val numbers = listOf(1, 2, 3) val eager = numbers.map { println("Eager map: $it"); it + 1 } val lazy = numbers.asSequence().map { println("Lazy map: $it"); it + 1 } println("Eager sum: ${eager.sum()}") println("Lazy sum: ${lazy.sum()}") }
Attempts:
2 left
💡 Hint
Eager map runs immediately, lazy map runs when sum() is called.
✗ Incorrect
The eager map prints immediately during map call. The lazy map prints only when sum() triggers evaluation.
🧠 Conceptual
expert2:00remaining
Why prefer lazy evaluation in large data processing?
Which of the following is the best reason to prefer lazy evaluation over eager evaluation when processing large data sets in Kotlin?
Attempts:
2 left
💡 Hint
Think about how lazy evaluation handles data compared to eager evaluation.
✗ Incorrect
Lazy evaluation processes elements on demand, which saves memory by not creating large intermediate collections. It does not automatically use multiple threads or parallelism.