Challenge - 5 Problems
Swift Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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: " ") }
Attempts:
2 left
💡 Hint
Look at how the iterator decreases the current value and stops when it goes below zero.
✗ Incorrect
The iterator starts at 3 and yields values down to 0. When current becomes -1, it returns nil to stop iteration.
🧠 Conceptual
intermediate1:30remaining
Understanding Sequence protocol requirements
Which of the following is a required method to implement when conforming to the Sequence protocol in Swift?
Attempts:
2 left
💡 Hint
Sequence requires a method that returns an iterator.
✗ Incorrect
The Sequence protocol requires the makeIterator() method that returns an iterator conforming to IteratorProtocol.
🔧 Debug
advanced2: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) }
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.
✗ Incorrect
There is no compile-time or runtime error. The closure captures the mutable local variable 'index' and accesses 'values' from a copy of 'self' (immutable since 'let'). It correctly prints 1
2
3.
📝 Syntax
advanced2:00remaining
Correct syntax for custom iterator
Which option shows the correct syntax for a custom iterator conforming to IteratorProtocol in Swift?
Attempts:
2 left
💡 Hint
Remember next() must be mutating and return an optional Element.
✗ Incorrect
Option B correctly implements next() as mutating and returns Int? with proper logic. Others either miss optional return or have logic errors.
🚀 Application
expert2: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)
Attempts:
2 left
💡 Hint
Count the even numbers from 0 up to 5 inclusive.
✗ Incorrect
The sequence yields 0, 2, 4 and stops before 6 because 6 > 5. So 3 elements are printed.