0
0
Swiftprogramming~20 mins

Distributed actors overview in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Distributed Actor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple distributed actor method call
What is the output of this Swift code using a distributed actor?
Swift
distributed actor Counter {
    var value = 0
    distributed func increment() async {
        value += 1
    }
    func current() -> Int {
        return value
    }
}

func test() async {
    let counter = Counter()
    await counter.increment()
    print(counter.current())
}

task {
    await test()
}
A1
BRuntime error
CCompilation error
D0
Attempts:
2 left
💡 Hint
Remember that distributed functions are asynchronous and may not update local state immediately.
🧠 Conceptual
intermediate
1:30remaining
Understanding the role of distributed actor identity
What is the main purpose of the identity property in a distributed actor?
ATo store the actor's internal state
BTo uniquely identify the actor instance across the network
CTo define the actor's concurrency model
DTo manage local memory allocation
Attempts:
2 left
💡 Hint
Think about how distributed actors communicate remotely.
🔧 Debug
advanced
2:00remaining
Identify the error in distributed actor initialization
What error will this code produce and why?
Swift
distributed actor Logger {
    let id: UUID
    init() {
        id = UUID()
    }
}

func test() {
    let logger = Logger()
}
ANo error, code runs fine
BRuntime error: missing actor system
CCompilation error: distributed actors require a distributed actor system parameter in init
DCompilation error: distributed actors cannot have stored properties
Attempts:
2 left
💡 Hint
Distributed actors need a system to manage their identity and communication.
📝 Syntax
advanced
1:30remaining
Correct syntax for distributed function declaration
Which option correctly declares a distributed function in a distributed actor?
Swift
distributed actor Service {
    ???
}
Adistributed func fetchData() async throws -> String
Bfunc distributed fetchData() async throws -> String
Cdistributed func fetchData() -> String
Dfunc fetchData() distributed async throws -> String
Attempts:
2 left
💡 Hint
The 'distributed' keyword comes before 'func'.
🚀 Application
expert
2:30remaining
Predict the number of items in a distributed actor's dictionary after remote calls
Given this distributed actor code, how many key-value pairs will the dictionary contain after the calls?
Swift
distributed actor Store {
    var items: [String: Int] = [:]
    distributed func addItem(key: String, value: Int) async {
        items[key] = value
    }
    func countItems() -> Int {
        return items.count
    }
}

func test() async {
    let store = Store()
    await store.addItem(key: "a", value: 1)
    await store.addItem(key: "b", value: 2)
    print(store.countItems())
}

task {
    await test()
}
A0
B1
CCompilation error
D2
Attempts:
2 left
💡 Hint
Distributed functions run remotely and do not update local state immediately.