Bird
Raised Fist0
Swiftprogramming~20 mins

Zip for combining sequences in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Zip 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 Swift code using zip?

Consider the following Swift code that uses zip to combine two arrays. What will be printed?

Swift
let numbers = [1, 2, 3]
let words = ["one", "two", "three"]
for (number, word) in zip(numbers, words) {
    print("\(number): \(word)")
}
Aone: 1\ntwo: 2\nthree: 3
B1: one\n2: two
C3: three\n2: two\n1: one
D1: one\n2: two\n3: three
Attempts:
2 left
💡 Hint

Remember that zip pairs elements from both sequences in order.

❓ Predict Output
intermediate
2:00remaining
What happens when zipping sequences of different lengths?

What will be the output of this Swift code?

Swift
let a = [10, 20, 30, 40]
let b = ["a", "b"]
for (num, letter) in zip(a, b) {
    print("\(num)\(letter)")
}
A10a\n20b
B10a\n20b\n30c\n40d
C30c\n40d
D10a\n20b\n30\n40
Attempts:
2 left
💡 Hint

Think about how zip handles sequences of different lengths.

🔧 Debug
advanced
2:00remaining
Why does this Swift code using zip cause a runtime error?

Examine the code below. Why does it cause a runtime error?

Swift
let nums = [1, 2, 3]
let words = ["one", "two"]
let zipped = zip(nums, words)
let array = Array(zipped)
print(array[2])
AType error because zipped is not convertible to Array
BSyntax error because zip requires sequences of equal length
CIndex out of range error at runtime because zipped has only 2 elements
DNo error, prints (3, "three")
Attempts:
2 left
💡 Hint

Check how many elements zipped contains and what index is accessed.

🧠 Conceptual
advanced
2:00remaining
What is the type of the result of zip in Swift?

Given two arrays a and b, what is the type of zip(a, b) in Swift?

ASequence<(Int, String)>
BZip2Sequence<Array<Int>, Array<String>>
C[(Int, String)]
DArray<(Int, String)>
Attempts:
2 left
💡 Hint

Think about what zip returns before converting to an array.

🚀 Application
expert
3:00remaining
How to sum pairs of numbers using zip in Swift?

You have two arrays of integers: arr1 and arr2. You want to create a new array where each element is the sum of the corresponding elements from arr1 and arr2. Which code snippet correctly does this using zip?

Alet sums = zip(arr1, arr2).map { (a, b) in a + b }
Blet sums = zip(arr1, arr2).map { $0 + $1 }
Clet sums = zip(arr1, arr2).map { $0 * $1 }
Dlet sums = arr1.enumerated().map { $0.element + arr2[$0.offset] }
Attempts:
2 left
💡 Hint

Remember the closure syntax for map when using zip.

Practice

(1/5)
1. What does the zip function do in Swift?
easy
A. Filters elements from a sequence based on a condition
B. Adds all elements of two sequences together
C. Sorts two sequences in ascending order
D. Combines two sequences into pairs until the shortest ends

Solution

  1. Step 1: Understand the purpose of zip

    The zip function pairs elements from two sequences together, creating tuples.
  2. Step 2: Recognize stopping condition

    It stops pairing when the shortest sequence runs out of elements.
  3. Final Answer:

    Combines two sequences into pairs until the shortest ends -> Option D
  4. Quick Check:

    Zip pairs sequences = D [OK]
Hint: Remember: zip pairs elements, stops at shortest sequence [OK]
Common Mistakes:
  • Thinking zip adds or merges elements
  • Assuming zip continues to longest sequence length
  • Confusing zip with sorting or filtering
2. Which of the following is the correct syntax to zip two arrays a and b in Swift?
easy
A. let zipped = zip(a, b)
B. let zipped = zip[a, b]
C. let zipped = Array.zip(a, b)
D. let zipped = zip a, b

Solution

  1. Step 1: Recall Swift function call syntax

    Functions are called with parentheses and arguments inside, like zip(a, b).
  2. Step 2: Check each option

    let zipped = zip(a, b) uses correct syntax. Options B, C, and D use invalid syntax.
  3. Final Answer:

    let zipped = zip(a, b) -> Option A
  4. Quick Check:

    Function call uses parentheses = A [OK]
Hint: Use parentheses for function calls like zip(a, b) [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Trying to call zip as a method on array
  • Omitting parentheses around arguments
3. What is the output of this Swift code?
let numbers = [1, 2, 3]
let letters = ["a", "b", "c", "d"]
for pair in zip(numbers, letters) {
    print(pair, terminator: " ")
}
medium
A. [(1, "a"), (2, "b"), (3, "c"), (nil, "d")]
B. (1, "a") (2, "b") (3, "c")
C. (1, "a") (2, "b") (3, "c") (4, "d")
D. (1, "a") (2, "b") (3, "c") (nil, nil)

Solution

  1. Step 1: Understand zip behavior with different lengths

    Zip stops at the shortest sequence length, which is 3 here.
  2. Step 2: Trace the loop output

    Pairs printed are (1, "a"), (2, "b"), (3, "c") only.
  3. Final Answer:

    (1, "a") (2, "b") (3, "c") -> Option B
  4. Quick Check:

    Zip stops at shortest = B [OK]
Hint: Zip stops at shortest sequence length, ignore extra elements [OK]
Common Mistakes:
  • Expecting zip to include extra elements from longer sequence
  • Thinking zip fills missing values with nil
  • Confusing tuple output format
4. Identify the error in this Swift code using zip:
let nums = [1, 2, 3]
let chars = ["x", "y"]
for (num, char) in zip(nums, chars) {
    print(num + char)
}
medium
A. Missing parentheses in for loop
B. zip requires sequences of the same length
C. Cannot add Int and String directly
D. zip cannot be used with arrays

Solution

  1. Step 1: Analyze the print statement

    The code tries to add an Int and a String with num + char, which is invalid.
  2. Step 2: Check zip usage

    Zip works fine with arrays of different lengths; it stops at shortest.
  3. Final Answer:

    Cannot add Int and String directly -> Option C
  4. Quick Check:

    Type mismatch in addition = C [OK]
Hint: Check data types before adding values inside zip loop [OK]
Common Mistakes:
  • Assuming zip needs equal length sequences
  • Ignoring type mismatch errors
  • Thinking zip syntax is wrong here
5. Given two arrays names = ["Anna", "Bob", "Cara"] and scores = [85, 92, 78], which Swift code correctly creates a dictionary pairing each name with its score using zip?
hard
A. let result = Dictionary(uniqueKeysWithValues: zip(names, scores))
B. let result = zip(names, scores).toDictionary()
C. let result = Dictionary(zip(names, scores))
D. let result = names.zip(scores).toDict()

Solution

  1. Step 1: Understand how to convert zipped pairs to dictionary

    Use Dictionary(uniqueKeysWithValues:) initializer with zipped sequence.
  2. Step 2: Check each option's validity

    let result = Dictionary(uniqueKeysWithValues: zip(names, scores)) uses correct syntax. Options B, C, and D use invalid or non-existent methods.
  3. Final Answer:

    let result = Dictionary(uniqueKeysWithValues: zip(names, scores)) -> Option A
  4. Quick Check:

    Use Dictionary initializer with zip = A [OK]
Hint: Use Dictionary(uniqueKeysWithValues:) with zip to create dictionary [OK]
Common Mistakes:
  • Trying to call non-existent methods like toDictionary()
  • Passing zip directly to Dictionary() without initializer
  • Using zip as a method on array
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