0
0
Kotlinprogramming~20 mins

Sequence vs collection performance in Kotlin - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sequence Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Performance difference between Sequence and Collection in Kotlin

Consider the following Kotlin code that processes a list of numbers. What is the output of the code snippet below?

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.asSequence()
    .map { it * 2 }
    .filter { it > 5 }
    .toList()
println(result)
A[1, 2, 3, 4, 5]
B[2, 4, 6, 8, 10]
C[6, 8, 10]
D[]
Attempts:
2 left
💡 Hint

Remember that asSequence() processes elements lazily and filters after mapping.

🧠 Conceptual
intermediate
1:30remaining
Why use Sequence over Collection in Kotlin?

Which of the following best explains why you might choose a Sequence over a Collection in Kotlin?

ASequence processes elements lazily, which can improve performance for large data sets by avoiding intermediate collections.
BSequence stores all elements in memory, making it faster for small data sets.
CSequence automatically sorts elements, unlike Collection.
DSequence is mutable while Collection is immutable.
Attempts:
2 left
💡 Hint

Think about how data is processed step-by-step in sequences.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in sequence usage

Which option contains a syntax error when trying to create a sequence and filter elements greater than 10?

Kotlin
val seq = listOf(5, 10, 15, 20).asSequence()
val filtered = seq.filter { it > 10 }
println(filtered.toList())
Aval filtered = seq.filter { it -> it > 10 }
Bval filtered = seq.filter { it > 10 }
Cval filtered = seq.filter { x -> x > 10 }
Dval filtered = seq.filter(it > 10)
Attempts:
2 left
💡 Hint

Check the syntax of lambda expressions in Kotlin.

optimization
advanced
2:00remaining
Optimizing chained operations with Sequence

You want to optimize the following Kotlin code for performance on a large list. Which option best improves performance?

val result = numbers
    .map { it * 2 }
    .filter { it > 10 }
    .take(5)
    .toList()
AReplace <code>map</code> with <code>forEach</code> to avoid creating a new list.
BUse <code>numbers.asSequence()</code> before chaining operations to process lazily.
CCall <code>toList()</code> after <code>map</code> and before <code>filter</code>.
DUse <code>numbers.parallelStream()</code> to process in parallel.
Attempts:
2 left
💡 Hint

Think about how lazy evaluation can reduce work done.

🔧 Debug
expert
2:30remaining
Debugging unexpected output with Sequence and Collection

Given the code below, what is the output and why?

val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.map { it * 2 }.filter { it > 5 }
println(result)
A[6, 8, 10] because map and filter are applied eagerly on the list.
B[2, 4, 6, 8, 10] because filter is ignored without sequence.
C[] because filter removes all elements less than or equal to 5 before map.
DCompilation error because map and filter cannot be chained on List.
Attempts:
2 left
💡 Hint

Consider the order and eager evaluation of collection operations.