Challenge - 5 Problems
Async Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this async sequence code?
Consider the following Swift code using an async sequence. What will be printed when this code runs?
Swift
import Foundation struct Countdown: AsyncSequence { typealias Element = Int let start: Int struct AsyncIterator: AsyncIteratorProtocol { var current: Int mutating func next() async -> Int? { if current < 0 { return nil } else { let value = current current -= 1 return value } } } func makeAsyncIterator() -> AsyncIterator { AsyncIterator(current: start) } } @main struct Main { static func main() async { let countdown = Countdown(start: 3) var result = "" for await number in countdown { result += "\(number) " } print(result.trimmingCharacters(in: .whitespaces)) } }
Attempts:
2 left
💡 Hint
Think about how the iterator decreases the current value and when it stops.
✗ Incorrect
The async iterator starts at 3 and decreases by 1 each time until it reaches -1, at which point it returns nil and stops. So the sequence yields 3, 2, 1, 0.
🧠 Conceptual
intermediate1:30remaining
Which statement about AsyncSequence is true?
Choose the correct statement about AsyncSequence in Swift.
Attempts:
2 left
💡 Hint
Think about how async sequences are consumed in Swift.
✗ Incorrect
AsyncSequence supports asynchronous iteration using for await-in loops. The iterator is asynchronous and returns values with async next() method.
🔧 Debug
advanced2:00remaining
What error does this async sequence code produce?
This Swift code attempts to create an async sequence but has a problem. What error will it cause?
Swift
struct Numbers: AsyncSequence { typealias Element = Int func makeAsyncIterator() -> some AsyncIteratorProtocol { return Iterator() } struct Iterator: AsyncIteratorProtocol { var current = 0 mutating func next() async -> Int? { if current < 3 { defer { current += 1 } return current } else { return nil } } } }
Attempts:
2 left
💡 Hint
Check if the method that changes state is marked mutating.
✗ Incorrect
The next() method tries to modify 'current' but is not marked mutating, causing a compile-time error.
📝 Syntax
advanced1:30remaining
Which option correctly defines an async iterator's next() method?
Select the correct syntax for the next() method in an AsyncIteratorProtocol conforming struct.
Attempts:
2 left
💡 Hint
Remember the order of async and throws keywords in Swift function declarations.
✗ Incorrect
The correct syntax places async before the return arrow and mutating before func. Option C matches the required signature.
🚀 Application
expert2:30remaining
How many items does this async sequence produce?
Given this async sequence, how many elements will be produced when iterated fully?
Swift
struct EvenNumbers: AsyncSequence { typealias Element = Int let max: Int struct Iterator: AsyncIteratorProtocol { var current = 0 let max: Int mutating func next() async -> Int? { if current > max { return nil } let result = current current += 2 return result } } func makeAsyncIterator() -> Iterator { Iterator(current: 0, max: max) } } @main struct Main { static func main() async { let evens = EvenNumbers(max: 7) var count = 0 for await _ in evens { count += 1 } print(count) } }
Attempts:
2 left
💡 Hint
Count even numbers from 0 up to and including max.
✗ Incorrect
The sequence yields 0, 2, 4, 6 before current becomes 8 which is > 7, so iteration stops after 4 elements.