Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does destructuring in Kotlin collection iteration allow you to do?
for ((key, value) in map) { ... }
easy
A. Only access the value in the loop
B. Only access the key in the loop
C. Access both key and value directly in the loop variables
D. Modify the map while iterating

Solution

  1. Step 1: Understand destructuring syntax

    In Kotlin, for ((key, value) in map) extracts both key and value from each map entry.
  2. Step 2: Recognize loop variable usage

    This lets you use key and value directly inside the loop without extra calls.
  3. Final Answer:

    Access both key and value directly in the loop variables -> Option C
  4. Quick Check:

    Destructuring = direct access to parts [OK]
Hint: Destructuring splits pairs into variables directly [OK]
Common Mistakes:
  • Thinking only key or value is accessible
  • Trying to modify map inside loop
  • Not using parentheses for destructuring
2. Which of the following is the correct syntax for destructuring a map in Kotlin?
val map = mapOf("a" to 1, "b" to 2)
easy
A. for (key, value in map) { println(key + value) }
B. for ((key, value) in map) { println(key + value) }
C. for [key, value] in map { println(key + value) }
D. for {key, value} in map { println(key + value) }

Solution

  1. Step 1: Recall Kotlin destructuring syntax

    Kotlin requires parentheses around variables when destructuring: for ((key, value) in map).
  2. Step 2: Check each option

    Only for ((key, value) in map) { println(key + value) } uses parentheses correctly; others use commas without parentheses or wrong brackets.
  3. Final Answer:

    for ((key, value) in map) { println(key + value) } -> Option B
  4. Quick Check:

    Parentheses needed for destructuring [OK]
Hint: Use parentheses around variables in destructuring [OK]
Common Mistakes:
  • Omitting parentheses in for loop
  • Using square or curly brackets instead
  • Confusing destructuring with tuple unpacking syntax
3. What is the output of this Kotlin code?
val map = mapOf("x" to 10, "y" to 20)
for ((k, v) in map) {
  println("$k$v")
}
medium
A. Syntax error
B. 10x\n20y
C. k v\nk v
D. x10\ny20

Solution

  1. Step 1: Understand map entries and destructuring

    The map has entries "x" to 10 and "y" to 20. The loop destructures each entry into k and v.
  2. Step 2: Print key and value concatenated

    Inside the loop, println("$k$v") prints key and value together without space, so outputs are "x10" and "y20" on separate lines.
  3. Final Answer:

    x10\ny20 -> Option D
  4. Quick Check:

    Print key + value per entry [OK]
Hint: Destructure map entries to print key and value [OK]
Common Mistakes:
  • Swapping key and value in output
  • Expecting spaces between key and value
  • Confusing variable names
4. Identify the error in this Kotlin code snippet:
val map = mapOf("a" to 1, "b" to 2)
for (key, value in map) {
  println(key + value)
}
medium
A. Missing parentheses around key and value in for loop
B. mapOf cannot be iterated
C. Cannot add key and value directly
D. No error, code runs fine

Solution

  1. Step 1: Check for destructuring syntax

    The for loop uses for (key, value in map) which lacks parentheses around key, value.
  2. Step 2: Understand correct syntax

    Kotlin requires parentheses for destructuring pairs: for ((key, value) in map). Without them, it's a syntax error.
  3. Final Answer:

    Missing parentheses around key and value in for loop -> Option A
  4. Quick Check:

    Parentheses required for destructuring [OK]
Hint: Always use parentheses for destructuring in for loops [OK]
Common Mistakes:
  • Omitting parentheses in destructuring
  • Thinking mapOf is not iterable
  • Confusing addition with string concatenation
5. Given a list of pairs representing database rows:
val rows = listOf(Pair("id", 1), Pair("name", "Alice"), Pair("age", 30))

Which Kotlin code correctly prints each key and value using destructuring?
hard
A. for ((key, value) in rows) { println("$key: $value") }
B. for (key, value in rows) { println("$key: $value") }
C. for (row in rows) { println(row.key + ": " + row.value) }
D. for ((k, v) in rows) { println(k + v) }

Solution

  1. Step 1: Recognize list of pairs and destructuring

    The list contains pairs, so destructuring with (key, value) works to access each part.
  2. Step 2: Check each option for correctness

    for ((key, value) in rows) { println("$key: $value") } uses correct destructuring syntax and prints key and value with colon and space. for (key, value in rows) { println("$key: $value") } misses parentheses, for (row in rows) { println(row.key + ": " + row.value) } tries to access properties that don't exist, for ((k, v) in rows) { println(k + v) } concatenates without separator.
  3. Final Answer:

    for ((key, value) in rows) { println("$key: $value") } -> Option A
  4. Quick Check:

    Use parentheses and print with separator [OK]
Hint: Use parentheses and print key: value format [OK]
Common Mistakes:
  • Omitting parentheses in for loop
  • Accessing pair elements as properties
  • Concatenating without separator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard