Challenge - 5 Problems
Collection Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of collection operation replacing a loop
What is the output of this Kotlin code that uses a collection operation instead of a loop?
Kotlin
val numbers = listOf(1, 2, 3, 4, 5) val doubled = numbers.map { it * 2 } println(doubled)
Attempts:
2 left
💡 Hint
The map function applies the given operation to each element.
✗ Incorrect
The map operation creates a new list by multiplying each number by 2, replacing the need for a loop.
🧠 Conceptual
intermediate1:30remaining
Why use collection operations over loops?
Which of the following is the main advantage of using collection operations like map and filter instead of traditional loops?
Attempts:
2 left
💡 Hint
Think about code clarity and simplicity.
✗ Incorrect
Collection operations express intent clearly and reduce boilerplate code compared to loops.
📝 Syntax
advanced2:00remaining
Identify the correct Kotlin collection operation syntax
Which option correctly filters a list to keep only even numbers using a collection operation?
Kotlin
val nums = listOf(1, 2, 3, 4, 5, 6)
Attempts:
2 left
💡 Hint
Filtering keeps elements that match a condition.
✗ Incorrect
filter returns a list of elements matching the condition; map transforms elements; forEach performs actions without returning a list; reduce combines elements.
❓ optimization
advanced2:30remaining
Optimizing collection operations to avoid intermediate lists
Which Kotlin collection operation chain avoids creating intermediate lists and processes elements lazily?
Attempts:
2 left
💡 Hint
Sequences process elements lazily, unlike lists.
✗ Incorrect
Using asSequence() creates a lazy sequence that avoids intermediate collections until toList() is called.
🔧 Debug
expert3:00remaining
Debugging unexpected output from collection operations
Given this Kotlin code, what is the output and why?
val words = listOf("apple", "banana", "cherry")
val result = words.map { it.length > 5 }
println(result)
Attempts:
2 left
💡 Hint
map transforms each element based on the lambda expression.
✗ Incorrect
The lambda checks if each word's length is greater than 5, returning a list of Booleans.