0
0
Kotlinprogramming~20 mins

Zip for combining collections in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Zip Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of zipping two lists of different lengths?
Consider the following Kotlin code that uses zip to combine two lists of different lengths. What will be printed?
Kotlin
val list1 = listOf("a", "b", "c", "d")
val list2 = listOf(1, 2)
val zipped = list1.zip(list2)
println(zipped)
A[(a, 1), (b, 2), (c, null), (d, null)]
BCompilation error due to different list sizes
C[(a, 1), (b, 2), (c, 3), (d, 4)]
D[(a, 1), (b, 2)]
Attempts:
2 left
💡 Hint
Remember that zip pairs elements until the shortest list ends.
Predict Output
intermediate
2:00remaining
What is the output when zipping with a transform function?
What will this Kotlin code print when using zip with a transform lambda?
Kotlin
val nums = listOf(1, 2, 3)
val words = listOf("one", "two", "three")
val result = nums.zip(words) { n, w -> "$w:$n" }
println(result)
A[one:1, two:2, three:3]
B[1:one, 2:two, 3:three]
C[one-1, two-2, three-3]
DCompilation error due to wrong lambda syntax
Attempts:
2 left
💡 Hint
The lambda receives elements from the first and second list in order.
🔧 Debug
advanced
2:00remaining
Why does this zip code cause a compilation error?
This Kotlin code tries to zip two lists but causes a compilation error. What is the error?
Kotlin
val a = listOf(1, 2, 3)
val b = listOf("x", "y")
val zipped = a.zip(b) { x -> x.toString() }
println(zipped)
ALambda must have two parameters, one for each list element
BLists must be the same size to use zip
Czip requires explicit type arguments for generic parameters
DCannot zip lists of different types
Attempts:
2 left
💡 Hint
Check the lambda parameters for zip with transform.
Predict Output
advanced
2:00remaining
What is the output of zipping a list with itself?
What will this Kotlin code print?
Kotlin
val list = listOf(10, 20, 30)
val zipped = list.zip(list)
println(zipped)
A[(10, 10), (20, 30), (30, 20)]
B[(10, 10), (20, 20), (30, 30)]
C[(10, 20), (20, 30), (30, 10)]
DCompilation error due to zipping list with itself
Attempts:
2 left
💡 Hint
Zipping a list with itself pairs elements at the same index.
🧠 Conceptual
expert
2:00remaining
How many pairs are in the zipped list?
Given these Kotlin lists, how many pairs will the zipped list contain?
val first = listOf(1, 2, 3, 4, 5)
val second = listOf("a", "b", "c")
A8
B5
C3
DCompilation error due to different list sizes
Attempts:
2 left
💡 Hint
The zipped list length equals the shorter list length.