Bird
Raised Fist0
Kotlinprogramming~20 mins

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

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

Practice

(1/5)
1. What does the zip function do when used with two lists in Kotlin?
easy
A. Combines all elements from both lists into one longer list.
B. Pairs elements from both lists into a list of pairs, stopping at the shorter list length.
C. Creates a map from the first list's elements to the second list's elements without pairing.
D. Filters elements that are common to both lists.

Solution

  1. Step 1: Understand the zip function behavior

    The zip function pairs elements from two collections by their positions.
  2. Step 2: Consider the length of the resulting list

    The resulting list length equals the smaller of the two input lists, pairing elements until one list runs out.
  3. Final Answer:

    Pairs elements from both lists into a list of pairs, stopping at the shorter list length. -> Option B
  4. Quick Check:

    Zip pairs elements up to shortest list length [OK]
Hint: Zip pairs elements by index, stops at shortest list length [OK]
Common Mistakes:
  • Thinking zip concatenates lists instead of pairing
  • Assuming zip returns a map automatically
  • Believing zip pairs all elements ignoring list length
2. Which of the following is the correct syntax to zip two lists listA and listB in Kotlin?
easy
A. val zipped = listA + listB.zip()
B. val zipped = zip(listA, listB)
C. val zipped = listA.zip(listB)
D. val zipped = listA.zipWith(listB)

Solution

  1. Step 1: Recall Kotlin's zip syntax

    The zip function is called as an extension on the first list, passing the second list as argument.
  2. Step 2: Check each option

    val zipped = listA.zip(listB) matches the correct syntax: listA.zip(listB). Others are invalid or incorrect method calls.
  3. Final Answer:

    val zipped = listA.zip(listB) -> Option C
  4. Quick Check:

    Use listA.zip(listB) to zip two lists [OK]
Hint: Use listA.zip(listB) to combine two lists [OK]
Common Mistakes:
  • Trying to call zip as a standalone function
  • Using incorrect method names like zipWith
  • Concatenating lists instead of zipping
3. What is the output of the following Kotlin code?
val nums = listOf(1, 2, 3)
val chars = listOf('a', 'b')
val zipped = nums.zip(chars)
println(zipped)
medium
A. [(1, a), (2, b), (3, null)]
B. [(a, 1), (b, 2)]
C. [(1, a), (2, b), (3, c)]
D. [(1, a), (2, b)]

Solution

  1. Step 1: Identify the input lists and their lengths

    nums has 3 elements, chars has 2 elements.
  2. Step 2: Understand zip output length and pairing

    Zip pairs elements by index until the shorter list ends, so only first 2 pairs: (1, 'a') and (2, 'b').
  3. Final Answer:

    [(1, a), (2, b)] -> Option D
  4. Quick Check:

    Zip length = shorter list length = 2 [OK]
Hint: Zip stops at shorter list length, pairs elements by index [OK]
Common Mistakes:
  • Expecting zip to fill missing elements with null
  • Assuming zip pairs all elements ignoring length
  • Mixing order of elements in pairs
4. The following Kotlin code throws an error. What is the problem?
val list1 = listOf(1, 2, 3)
val list2 = listOf("a", "b", "c")
val zipped = list1.zip(list2) { a, b -> a + b }
println(zipped)
medium
A. Cannot add Int and String directly with + operator.
B. zip requires lists to be the same size.
C. The lambda syntax for zip is incorrect.
D. listOf cannot contain different types.

Solution

  1. Step 1: Analyze the lambda inside zip

    The lambda tries to add a (Int) and b (String) using +.
  2. Step 2: Understand Kotlin type rules

    Adding Int and String directly with + is not allowed; it causes a type error.
  3. Final Answer:

    Cannot add Int and String directly with + operator. -> Option A
  4. Quick Check:

    Int + String causes type error in Kotlin [OK]
Hint: Int + String needs explicit conversion or concatenation [OK]
Common Mistakes:
  • Assuming zip needs equal list sizes
  • Thinking lambda syntax is wrong
  • Believing listOf can't hold different types
5. Given two lists val names = listOf("Anna", "Bob", "Cara") and val scores = listOf(90, 85), how can you create a list of strings like ["Anna: 90", "Bob: 85"] using zip with a lambda?
hard
A. names.zip(scores) { name, score -> "$name: $score" }
B. names.zip(scores).map { name, score -> "$name: $score" }
C. names.zipWith(scores) { name, score -> "$name: $score" }
D. names.zip(scores).toString { "$name: $score" }

Solution

  1. Step 1: Use zip with a transform lambda

    The zip function can take a lambda to transform pairs directly into desired output.
  2. Step 2: Format each pair as a string

    Using { name, score -> "$name: $score" } creates the string format needed.
  3. Final Answer:

    names.zip(scores) { name, score -> "$name: $score" } -> Option A
  4. Quick Check:

    Use zip with lambda to transform pairs [OK]
Hint: Use zip with lambda to format pairs directly [OK]
Common Mistakes:
  • Using zipWith which doesn't exist in Kotlin
  • Trying to call toString with a lambda
  • Mapping after zip instead of using lambda inside zip
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