0
0
Swiftprogramming~20 mins

Sequence protocol for custom iteration in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of custom sequence iteration
What is the output of this Swift code that uses a custom sequence conforming to the Sequence protocol?
Swift
struct Countdown: Sequence {
    let start: Int
    func makeIterator() -> some IteratorProtocol {
        var current = start
        return AnyIterator {
            if current < 0 { return nil }
            defer { current -= 1 }
            return current
        }
    }
}

let countdown = Countdown(start: 3)
for number in countdown {
    print(number, terminator: " ")
}
A3 2 1
B0 1 2 3
C3 2 1 0
D3 2 1 0 -1
Attempts:
2 left
💡 Hint
Look at how the iterator decreases the current value and stops when it goes below zero.
🧠 Conceptual
intermediate
1:30remaining
Understanding Sequence protocol requirements
Which of the following is a required method to implement when conforming to the Sequence protocol in Swift?
Afunc sequence() -> [Element]
Bfunc next() -> Element?
Cfunc start() -> Element
Dfunc makeIterator() -> some IteratorProtocol
Attempts:
2 left
💡 Hint
Sequence requires a method that returns an iterator.
🔧 Debug
advanced
2:00remaining
Identify the error in custom iterator implementation
What error will this Swift code produce when trying to iterate over CustomSequence?
Swift
struct CustomSequence: Sequence {
    let values: [Int]
    func makeIterator() -> some IteratorProtocol {
        var index = 0
        return AnyIterator {
            if index >= values.count {
                return nil
            }
            let val = values[index]
            index += 1
            return val
        }
    }
}

let seq = CustomSequence(values: [1, 2, 3])
for v in seq {
    print(v)
}
ANo error, prints 1 2 3
BRuntime error: Index out of range
CCompile-time error: Cannot use 'values' inside closure because it is not captured correctly
DCompile-time error: Missing return type in makeIterator()
Attempts:
2 left
💡 Hint
The code compiles and runs without errors. Local mutable variables and struct properties can both be captured correctly by escaping closures.
📝 Syntax
advanced
2:00remaining
Correct syntax for custom iterator
Which option shows the correct syntax for a custom iterator conforming to IteratorProtocol in Swift?
A
struct MyIterator: IteratorProtocol {
    var current = 0
    func next() -&gt; Int {
        current += 1
        return current
    }
}
B
struct MyIterator: IteratorProtocol {
    var current = 0
    mutating func next() -&gt; Int? {
        defer { current += 1 }
        return current &lt; 3 ? current : nil
    }
}
C
struct MyIterator: IteratorProtocol {
    var current = 0
    mutating func next() -&gt; Int {
        if current &lt; 3 {
            current += 1
            return current
        }
        return nil
    }
}
D
struct MyIterator: IteratorProtocol {
    var current = 0
    mutating func next() -&gt; Int? {
        if current &gt; 3 {
            return nil
        }
        current += 1
        return current
    }
}
Attempts:
2 left
💡 Hint
Remember next() must be mutating and return an optional Element.
🚀 Application
expert
2:30remaining
Number of elements iterated in custom sequence
Given this Swift custom sequence, how many elements will be printed when iterating over it?
Swift
struct EvenNumbers: Sequence {
    let max: Int
    func makeIterator() -> some IteratorProtocol {
        var current = 0
        return AnyIterator {
            if current > max {
                return nil
            }
            let result = current
            current += 2
            return result
        }
    }
}

let evens = EvenNumbers(max: 5)
var count = 0
for _ in evens {
    count += 1
}
print(count)
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Count the even numbers from 0 up to 5 inclusive.