Challenge - 5 Problems
Sequence Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Kotlin code using map and filter?
Consider this Kotlin code that uses sequence operators
map and filter. What will it print?Kotlin
val numbers = sequenceOf(1, 2, 3, 4, 5) val result = numbers.filter { it % 2 == 1 }.map { it * 3 }.toList() println(result)
Attempts:
2 left
💡 Hint
First filter odd numbers, then multiply each by 3.
✗ Incorrect
The code filters odd numbers (1,3,5) and then multiplies each by 3, resulting in [3,9,15].
❓ Predict Output
intermediateWhat does this Kotlin code print after filtering and mapping?
Look at this Kotlin code using sequence operators. What is the output?
Kotlin
val words = sequenceOf("apple", "banana", "cherry", "date") val result = words.filter { it.length > 5 }.map { it.uppercase() }.toList() println(result)
Attempts:
2 left
💡 Hint
Filter words longer than 5 letters, then convert to uppercase.
✗ Incorrect
Only "banana" and "cherry" have length > 5, so after uppercase, the list is ["BANANA", "CHERRY"].
🔧 Debug
advancedWhy does this Kotlin code throw an exception?
This Kotlin code throws an exception. What is the cause?
Kotlin
val nums = sequenceOf(1, 2, 3, 4) val result = nums.map { if (it == 3) null else it * 2 }.filter { it!! > 2 }.toList() println(result)
Attempts:
2 left
💡 Hint
Check how null values are handled in filter with 'it!!'.
✗ Incorrect
The map returns null for element 3. The filter uses 'it!!' which throws NullPointerException when 'it' is null.
📝 Syntax
advancedWhich option is the correct Kotlin syntax to filter even numbers and double them using sequences?
Choose the correct Kotlin code that filters even numbers from a sequence and doubles them.
Attempts:
2 left
💡 Hint
Remember to use lambda syntax with curly braces and 'it'.
✗ Incorrect
Option A uses correct lambda syntax with braces and 'it'. Options A and D miss braces, causing syntax errors. Option A changes order, doubling before filtering, which is logically different.
🚀 Application
expertHow many items are in the resulting list after this Kotlin sequence operation?
Given this Kotlin code, how many items does the resulting list contain?
Kotlin
val data = sequenceOf(10, 15, 20, 25, 30) val result = data.filter { it > 15 }.map { it / 5 }.filter { it % 2 == 0 }.toList() println(result.size)
Attempts:
2 left
💡 Hint
Step through filtering and mapping carefully.
✗ Incorrect
Filter >15 gives [20,25,30]. Map divides by 5: [4,5,6]. Filter even numbers: [4,6]. So size is 2.
