0
0
Kotlinprogramming~20 mins

Map transformation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Transformation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin map transformation?

Consider the following Kotlin code that transforms a map. What will be printed?

Kotlin
val original = mapOf("a" to 1, "b" to 2, "c" to 3)
val transformed = original.mapValues { it.value * 2 }
println(transformed)
A{a=2, b=4, c=6}
BCompilation error
C{a=3, b=4, c=5}
D{a=1, b=2, c=3}
Attempts:
2 left
💡 Hint

mapValues applies a transformation only to the values, keeping keys the same.

Predict Output
intermediate
2:00remaining
What does this Kotlin code print after filtering and transforming a map?

Given this Kotlin code, what is the output?

Kotlin
val numbers = mapOf(1 to "one", 2 to "two", 3 to "three", 4 to "four")
val filtered = numbers.filterKeys { it % 2 == 0 }.mapValues { it.value.uppercase() }
println(filtered)
A{2=two, 4=four}
B{2=TWO, 4=FOUR}
C{1=ONE, 3=THREE}
DRuntime exception
Attempts:
2 left
💡 Hint

filterKeys keeps only keys that satisfy the condition, then mapValues transforms the values.

🔧 Debug
advanced
2:00remaining
Why does this Kotlin map transformation code cause a compilation error?

Look at this Kotlin code snippet. It tries to create a new map with values doubled only if the value is greater than 2. Why does it fail to compile?

Kotlin
val data = mapOf(1 to 1, 2 to 3, 3 to 5)
val result = data.mapValues { if (it.value > 2) it.value * 2 }
println(result)
AThe lambda does not return a value for all cases, causing a type mismatch.
BmapValues cannot be used with if statements inside the lambda.
CThe mapOf keys must be strings, not integers.
DThe println statement is missing parentheses.
Attempts:
2 left
💡 Hint

Consider what happens when the if condition is false inside the lambda.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a map with keys doubled and values squared in Kotlin?

Choose the Kotlin code snippet that correctly creates a new map where each key is doubled and each value is squared from the original map mapOf(1 to 2, 3 to 4).

Kotlin
val original = mapOf(1 to 2, 3 to 4)
Aoriginal.map { it.key * 2 to it.value * it.value }
Boriginal.mapValues { (k, v) -> k * 2 to v * v }
Coriginal.mapKeys { it.key * 2 }.mapValues { it.value * it.value }
Doriginal.map { (k, v) -> k * 2 to v * v }.toMap()
Attempts:
2 left
💡 Hint

map returns a list of pairs, which can be converted back to a map with toMap().

🚀 Application
expert
2:00remaining
How many entries are in the resulting map after this Kotlin transformation?

Given the code below, how many entries does the result map contain?

Kotlin
val input = mapOf("x" to 10, "y" to 20, "z" to 30)
val result = input.mapKeys { it.key + it.value.toString() }.filterKeys { it.length > 2 }
A0
B1
C3
D2
Attempts:
2 left
💡 Hint

Check the length of each new key after concatenation.