Challenge - 5 Problems
Sequence Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin sequence code?
Consider the following Kotlin code that creates and processes a sequence. What will be printed?
Kotlin
val seq = sequenceOf(1, 2, 3, 4, 5).filter { it % 2 == 0 }.map { it * 3 } println(seq.toList())
Attempts:
2 left
💡 Hint
Remember that filter keeps only even numbers, then map multiplies them by 3.
✗ Incorrect
The sequence filters even numbers (2,4) and then multiplies each by 3, resulting in [6,12].
🧠 Conceptual
intermediate1:30remaining
Which Kotlin sequence creation method produces an infinite sequence?
Among the following Kotlin sequence creation methods, which one can produce an infinite sequence?
Attempts:
2 left
💡 Hint
Think about which method can keep producing values without a defined end.
✗ Incorrect
generateSequence with a lambda that always returns a value can produce an infinite sequence.
🔧 Debug
advanced2:00remaining
What error does this Kotlin sequence code raise?
Examine this Kotlin code snippet. What error will it cause when run?
Kotlin
val seq = generateSequence(1) { if (it < 5) it + 1 else null } println(seq.toList())
Attempts:
2 left
💡 Hint
Check the lambda's return type and what happens when condition is false.
✗ Incorrect
The lambda must return null to end the sequence, and here it returns null when condition is false, so no error occurs.
📝 Syntax
advanced1:30remaining
Which option correctly creates a Kotlin sequence from a range and filters odd numbers?
Select the Kotlin code snippet that correctly creates a sequence from 1 to 10 and filters only odd numbers.
Attempts:
2 left
💡 Hint
Remember how to convert a range to a sequence and apply filter.
✗ Incorrect
Option C correctly converts the range to a sequence and filters odd numbers. Others have syntax or logic errors.
🚀 Application
expert2:30remaining
How many items are in the resulting sequence?
Given this Kotlin code, how many items will the resulting sequence contain?
Kotlin
val seq = generateSequence(1) { if (it < 10) it + 2 else null } val result = seq.toList()
Attempts:
2 left
💡 Hint
Count the numbers starting at 1, adding 2 each time, stopping before exceeding 10.
✗ Incorrect
Sequence generates: 1, 3, 5, 7, 9 and stops because next would be 11 which is > 10. So 5 items total.