0
0
Kotlinprogramming~20 mins

Destructuring in collection iteration in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Destructuring Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of destructuring a list of pairs
What is the output of this Kotlin code that uses destructuring in a for loop to iterate over a list of pairs?
Kotlin
val pairs = listOf(Pair("a", 1), Pair("b", 2), Pair("c", 3))
for ((letter, number) in pairs) {
    println("$letter$number")
}
Aa1\nb2\nc3
B(a,1)\n(b,2)\n(c,3)
Ca\nb\nc
D1\n2\n3
Attempts:
2 left
💡 Hint
Look at how the pair is split into two variables inside the loop.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in destructuring declaration
Which option contains a syntax error when destructuring a map entry in a for loop?
Kotlin
val map = mapOf("x" to 10, "y" to 20)
for ((key, value) in map) {
    println("$key -> $value")
}
Afor ((key, value) in map) { println("$key -> $value") }
Bfor ((k, v) in map) { println("$k -> $v") }
Cfor (key, value in map) { println("$key -> $value") }
Dfor ((key, value) in map.entries) { println("$key -> $value") }
Attempts:
2 left
💡 Hint
Check the parentheses around the destructured variables.
optimization
advanced
2:00remaining
Optimizing iteration with destructuring on large map
Given a large map, which option is the most efficient way to iterate with destructuring to print keys and values?
Kotlin
val largeMap = (1..1000000).associateWith { it * 2 }
AlargeMap.keys.forEach { println("$it -> ${largeMap[it]}") }
BlargeMap.forEach { (k, v) -> println("$k -> $v") }
ClargeMap.entries.forEach { println("${it.key} -> ${it.value}") }
Dfor ((k, v) in largeMap) { println("$k -> $v") }
Attempts:
2 left
💡 Hint
Consider the overhead of accessing values inside the loop.
🔧 Debug
advanced
2:00remaining
Debug the error in destructuring a list of triples
What error occurs when running this code and why?
Kotlin
val triples = listOf(Triple(1, 2, 3), Triple(4, 5, 6))
for ((a, b) in triples) {
    println("$a and $b")
}
AType mismatch error: expected 2 variables but Triple has 3
BNo error, prints '1 and 2' and '4 and 5'
CRuntime exception: IndexOutOfBounds
DSyntax error: missing third variable in destructuring
Attempts:
2 left
💡 Hint
Check how many variables Triple expects in destructuring.
🧠 Conceptual
expert
2:00remaining
Understanding destructuring declarations with data classes
Given this data class and list, what is the output of the code below?
Kotlin
data class Person(val name: String, val age: Int)
val people = listOf(Person("Alice", 30), Person("Bob", 25))
for ((name, age) in people) {
    println("$name is $age years old")
}
Aname age\nname age
BPerson(name=Alice, age=30)\nPerson(name=Bob, age=25)
CCompilation error: cannot destructure Person
DAlice is 30 years old\nBob is 25 years old
Attempts:
2 left
💡 Hint
Data classes automatically support destructuring declarations.