0
0
Kotlinprogramming~10 mins

Sequence vs collection performance in Kotlin - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a sequence from a list.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val seq = numbers.[1]()
Drag options to blanks, or click blank then click option'
Afilter
BasSequence
Cmap
DtoList
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList()' which returns a list, not a sequence.
Using 'map()' or 'filter()' directly without converting to sequence first.
2fill in blank
medium

Complete the code to get the first element greater than 3 from a sequence.

Kotlin
val seq = sequenceOf(1, 2, 3, 4, 5)
val first = seq.filter { it > 3 }.[1]()
Drag options to blanks, or click blank then click option'
Alast
Bmap
Cfind
Dfirst
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'last()' which returns the last element.
Using 'find()' which returns nullable and requires a predicate.
3fill in blank
hard

Fix the error in the code to sum numbers lazily using a sequence.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val sum = numbers.[1]().map { it * 2 }.sum()
Drag options to blanks, or click blank then click option'
AtoList
Bfilter
CasSequence
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList()' which does not enable lazy evaluation.
Using 'map()' directly without converting to sequence.
4fill in blank
hard

Fill both blanks to create a sequence and filter even numbers.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.[1]().[2] { it % 2 == 0 }.toList()
Drag options to blanks, or click blank then click option'
AasSequence
BtoList
Cfilter
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList()' before filtering which is eager evaluation.
Using 'map' instead of 'filter' to select elements.
5fill in blank
hard

Fill all three blanks to create a sequence, map values, and find the max.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val maxVal = numbers.[1]().[2] { it * 3 }.[3]()
Drag options to blanks, or click blank then click option'
AasSequence
Bmap
Cmax
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filter' instead of 'map' for transformation.
Using 'maxBy' which requires a selector function.
Not converting to sequence first.