Challenge - 5 Problems
Sequence Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sequence processing with map and filter
What is the output of this Kotlin code using sequences?
Kotlin
val result = sequenceOf(1, 2, 3, 4, 5) .map { it * 2 } .filter { it > 5 } .toList() println(result)
Attempts:
2 left
💡 Hint
Remember that map doubles each number, then filter keeps only those greater than 5.
✗ Incorrect
The sequence doubles each number: [2,4,6,8,10]. Then filter keeps numbers > 5: [6,8,10].
🧠 Conceptual
intermediate1:30remaining
When to prefer sequences over collections
Which situation is the best reason to use sequences instead of collections in Kotlin?
Attempts:
2 left
💡 Hint
Think about lazy evaluation and performance with big data.
✗ Incorrect
Sequences process elements lazily, which is efficient for large data and many operations. Collections process eagerly and keep all data in memory.
🔧 Debug
advanced2:00remaining
Identify the error in sequence usage
What error will this Kotlin code produce?
Kotlin
val seq = sequenceOf(1, 2, 3) val list: List<Int> = seq.filter { it > 1 } println(list.first())
Attempts:
2 left
💡 Hint
Check the type returned by filter on a sequence.
✗ Incorrect
filter on a sequence returns another Sequence, not a List. Assigning to a List variable causes a type mismatch error.
❓ Predict Output
advanced1:30remaining
Output of sequence with take and map
What is the output of this Kotlin code?
Kotlin
val result = sequenceOf(10, 20, 30, 40, 50) .take(3) .map { it / 10 } .toList() println(result)
Attempts:
2 left
💡 Hint
take(3) keeps first 3 elements, then map divides each by 10.
✗ Incorrect
take(3) keeps 10, 20, 30. Dividing each by 10 gives 1, 2, 3.
🧠 Conceptual
expert2:30remaining
Why sequences can improve performance in Kotlin
Why do Kotlin sequences often improve performance when chaining multiple operations on large data sets?
Attempts:
2 left
💡 Hint
Think about how sequences handle intermediate steps.
✗ Incorrect
Sequences use lazy evaluation, so each element passes through all operations before moving to the next, avoiding intermediate collections and saving memory and time.