Destructuring in collection iteration in Kotlin - Time & Space Complexity
When we use destructuring to loop over collections, we want to know how the time needed grows as the collection gets bigger.
We ask: How does the number of steps change when the list has more items?
Analyze the time complexity of the following code snippet.
val map = mapOf("a" to 1, "b" to 2, "c" to 3)
for ((key, value) in map) {
println("Key: $key, Value: $value")
}
This code loops over a map and uses destructuring to get each key and value pair.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each entry in the map.
- How many times: Once for every key-value pair in the map.
Each item in the map is visited once, so the work grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of steps grows in a straight line with the number of items.
Time Complexity: O(n)
This means the time to finish grows directly with how many items are in the collection.
[X] Wrong: "Destructuring makes the loop slower because it adds extra steps."
[OK] Correct: Destructuring just unpacks each item during the loop, which is done once per item and does not add extra loops or nested work.
Understanding how loops with destructuring scale helps you explain your code clearly and shows you know how to reason about performance in real tasks.
"What if we nested another loop inside that also uses destructuring? How would the time complexity change?"
Practice
for ((key, value) in map) { ... }Solution
Step 1: Understand destructuring syntax
In Kotlin,for ((key, value) in map)extracts both key and value from each map entry.Step 2: Recognize loop variable usage
This lets you usekeyandvaluedirectly inside the loop without extra calls.Final Answer:
Access both key and value directly in the loop variables -> Option CQuick Check:
Destructuring = direct access to parts [OK]
- Thinking only key or value is accessible
- Trying to modify map inside loop
- Not using parentheses for destructuring
val map = mapOf("a" to 1, "b" to 2)Solution
Step 1: Recall Kotlin destructuring syntax
Kotlin requires parentheses around variables when destructuring:for ((key, value) in map).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.Final Answer:
for ((key, value) in map) { println(key + value) } -> Option BQuick Check:
Parentheses needed for destructuring [OK]
- Omitting parentheses in for loop
- Using square or curly brackets instead
- Confusing destructuring with tuple unpacking syntax
val map = mapOf("x" to 10, "y" to 20)
for ((k, v) in map) {
println("$k$v")
}Solution
Step 1: Understand map entries and destructuring
The map has entries "x" to 10 and "y" to 20. The loop destructures each entry intokandv.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.Final Answer:
x10\ny20 -> Option DQuick Check:
Print key + value per entry [OK]
- Swapping key and value in output
- Expecting spaces between key and value
- Confusing variable names
val map = mapOf("a" to 1, "b" to 2)
for (key, value in map) {
println(key + value)
}Solution
Step 1: Check for destructuring syntax
The for loop usesfor (key, value in map)which lacks parentheses aroundkey, value.Step 2: Understand correct syntax
Kotlin requires parentheses for destructuring pairs:for ((key, value) in map). Without them, it's a syntax error.Final Answer:
Missing parentheses around key and value in for loop -> Option AQuick Check:
Parentheses required for destructuring [OK]
- Omitting parentheses in destructuring
- Thinking mapOf is not iterable
- Confusing addition with string concatenation
val rows = listOf(Pair("id", 1), Pair("name", "Alice"), Pair("age", 30))Which Kotlin code correctly prints each key and value using destructuring?
Solution
Step 1: Recognize list of pairs and destructuring
The list contains pairs, so destructuring with(key, value)works to access each part.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.Final Answer:
for ((key, value) in rows) { println("$key: $value") } -> Option AQuick Check:
Use parentheses and print with separator [OK]
- Omitting parentheses in for loop
- Accessing pair elements as properties
- Concatenating without separator
Ready to prove it?
Pass the test and this pattern moves unknown
