Challenge - 5 Problems
Swift Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a protocol with associated type conformance
What is the output of this Swift code using a protocol with an associated type?
Swift
protocol Container { associatedtype Item mutating func append(_ item: Item) func count() -> Int subscript(i: Int) -> Item { get } } struct IntStack: Container { var items = [Int]() mutating func append(_ item: Int) { items.append(item) } func count() -> Int { items.count } subscript(i: Int) -> Int { items[i] } } var stack = IntStack() stack.append(10) stack.append(20) print(stack.count()) print(stack[1])
Attempts:
2 left
💡 Hint
Think about how many items are appended and what is at index 1.
✗ Incorrect
The IntStack struct conforms to Container with Item as Int. Two items (10 and 20) are appended, so count() returns 2. The item at index 1 is the second appended item, 20.
🧠 Conceptual
intermediate1:30remaining
Understanding associatedtype constraints
Given the protocol below, which statement about the associatedtype is true?
Swift
protocol Identifiable { associatedtype ID: Comparable var id: ID { get } }
Attempts:
2 left
💡 Hint
Look at the ': Comparable' part in the associatedtype declaration.
✗ Incorrect
The associatedtype ID has a constraint ': Comparable', meaning any type used for ID must conform to Comparable protocol.
🔧 Debug
advanced2:00remaining
Identify the error in protocol with associated type usage
What error will this Swift code produce?
Swift
protocol Container { associatedtype Item mutating func append(_ item: Item) } struct StringStack: Container { var items = [String]() mutating func append(_ item: Int) { items.append(String(item)) } }
Attempts:
2 left
💡 Hint
Check the parameter type of append compared to the associatedtype Item.
✗ Incorrect
The protocol requires append(_ item: Item) where Item is inferred as String from items array. The struct's append uses Int parameter, causing a mismatch and conformance error.
📝 Syntax
advanced2:00remaining
Correct syntax for protocol with associated type and default implementation
Which option correctly defines a protocol with an associated type and provides a default implementation for a method?
Attempts:
2 left
💡 Hint
Check method signature and operator used in default implementation.
✗ Incorrect
Option C correctly matches the method signature and uses the + operator for sum. Options B has wrong parameter labels, C and D use wrong operators for sum.
🚀 Application
expert2:30remaining
Implement a generic function using protocol with associated types
Which option correctly implements a generic function that takes any Container and returns the first item?
Swift
protocol Container { associatedtype Item func count() -> Int subscript(i: Int) -> Item { get } } func firstItem<C: Container>(_ container: C) -> C.Item? { // Fill in the blank }
Attempts:
2 left
💡 Hint
Check how to safely access the first element using count and subscript.
✗ Incorrect
Option A safely checks if count() > 0 before accessing container[0]. Option A risks out-of-bounds error. Option A uses count property which does not exist (count() is a method). Option A uses 'first' which is not defined in protocol.