0
0
Swiftprogramming~20 mins

Structured concurrency model in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of async let with structured concurrency
What is the output of this Swift code using structured concurrency with async let?
Swift
func fetchNumber() async -> Int {
    return 10
}

func fetchDouble() async -> Int {
    return 20
}

func test() async {
    async let a = fetchNumber()
    async let b = fetchDouble()
    let sum = await a + await b
    print(sum)
}

Task {
    await test()
}
A30
B10
C20
DCompilation error
Attempts:
2 left
💡 Hint
Remember that async let runs tasks concurrently and you must await their results.
🧠 Conceptual
intermediate
1:30remaining
Understanding Task Groups in Swift concurrency
Which statement best describes the behavior of TaskGroup in Swift's structured concurrency?
ATaskGroup runs child tasks sequentially and returns after the first completes.
BTaskGroup runs child tasks concurrently but does not wait for any to finish.
CTaskGroup runs child tasks concurrently and waits for all to complete before continuing.
DTaskGroup runs child tasks sequentially and returns immediately.
Attempts:
2 left
💡 Hint
Think about how structured concurrency manages child tasks and their lifetimes.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in this structured concurrency code
What runtime error will this Swift code produce when executed?
Swift
func test() async {
    async let a = Task.sleep(nanoseconds: 1_000_000_000) // 1 second
    print("Before await")
    let _ = await a
    print("After await")
}

Task {
    await test()
}
ARuntime error: Task cancellation error
BNo error, prints 'Before await' then 'After await' after 1 second
CCompilation error due to incorrect Task.sleep usage
DDeadlock error because async let cannot be awaited
Attempts:
2 left
💡 Hint
Check how Task.sleep is used and awaited in async let.
Predict Output
advanced
2:30remaining
Output of nested TaskGroup with cancellation
What will be printed by this Swift code using nested TaskGroups and cancellation?
Swift
func test() async {
    await withTaskGroup(of: Void.self) { group in
        group.addTask {
            print("Task 1 start")
            try? await Task.sleep(nanoseconds: 500_000_000)
            print("Task 1 end")
        }
        group.addTask {
            print("Task 2 start")
            Task.current.cancel()
            try? await Task.sleep(nanoseconds: 1_000_000_000)
            print("Task 2 end")
        }
    }
}

Task {
    await test()
}
ANo output due to immediate cancellation
B
Task 1 start
Task 2 start
Task 2 end
Task 1 end
C
Task 2 start
Task 1 start
Task 2 end
D
Task 1 start
Task 2 start
Task 1 end
Attempts:
2 left
💡 Hint
Consider how cancellation affects running tasks and which prints happen before cancellation.
🧠 Conceptual
expert
1:30remaining
Effect of structured concurrency on resource cleanup
Which best describes how Swift's structured concurrency model helps with resource cleanup in asynchronous code?
AIt automatically cancels child tasks when the parent task finishes or throws an error, ensuring cleanup.
BIt requires manual cancellation of all child tasks to free resources.
CIt delays resource cleanup until the entire program exits.
DIt does not affect resource cleanup; developers must manage it explicitly.
Attempts:
2 left
💡 Hint
Think about how structured concurrency scopes child tasks to their parent.