Challenge - 5 Problems
Swift Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of async task execution order
What is the output of this Swift code using async/await and Task groups?
Swift
import Foundation func fetchNumber(_ n: Int) async -> Int { try? await Task.sleep(n * 100_000_000) // sleep n*0.1 seconds return n } func test() async { await withTaskGroup(of: Int.self) { group in for i in 1...3 { group.addTask { await fetchNumber(i) } } for await result in group { print(result) } } } Task { await test() } RunLoop.main.run()
Attempts:
2 left
💡 Hint
Think about how Task groups run tasks concurrently and how results arrive.
✗ Incorrect
Task groups run child tasks concurrently, so the order of completion depends on the sleep duration. Since sleep times differ, results can come in any order.
🧠 Conceptual
intermediate1:30remaining
Why use structured concurrency?
Which of the following best explains why structured concurrency is important in modern Swift concurrency?
Attempts:
2 left
💡 Hint
Think about how tasks are organized and cleaned up.
✗ Incorrect
Structured concurrency ensures tasks are grouped and their lifetimes managed, preventing resource leaks and making code easier to reason about.
🔧 Debug
advanced2:00remaining
Identify the concurrency bug
What error or problem will this Swift code cause when run?
Swift
var sharedCounter = 0 func increment() async { for _ in 1...1000 { sharedCounter += 1 } } Task { await increment() } Task { await increment() } Task.sleep(1_000_000_000) // wait 1 second print(sharedCounter)
Attempts:
2 left
💡 Hint
Think about what happens when two tasks modify the same variable without synchronization.
✗ Incorrect
The sharedCounter is accessed concurrently without protection, causing a race condition and lost increments, so the final value is less than 2000.
📝 Syntax
advanced1:30remaining
Which code compiles without error?
Which of these Swift concurrency snippets compiles without error?
Attempts:
2 left
💡 Hint
Remember async functions can only await other async functions.
✗ Incorrect
Option A is correct because foo is async and awaits bar which is also async. Other options have mismatched async/await usage causing compile errors.
🚀 Application
expert2:30remaining
How many tasks run concurrently?
Given this Swift code, how many tasks run concurrently at the peak?
Swift
import Foundation func work(id: Int) async { print("Start \(id)") try? await Task.sleep(500_000_000) // 0.5 seconds print("End \(id)") } func main() async { await withTaskGroup(of: Void.self) { group in for i in 1...5 { group.addTask { await work(id: i) } if i == 3 { try? await Task.sleep(600_000_000) // 0.6 seconds } } } } Task { await main() } RunLoop.main.run()
Attempts:
2 left
💡 Hint
Task groups start tasks immediately; sleeps inside the loop do not block running tasks.
✗ Incorrect
All 5 tasks are added quickly and run concurrently. The sleep inside the loop pauses only the loop, not the already started tasks.