0
0
Swiftprogramming~20 mins

Recursive enumerations in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive Enum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple recursive enum with indirect case
What is the output of this Swift code when printed?
Swift
enum BinaryTree {
    case empty
    indirect case node(Int, left: BinaryTree, right: BinaryTree)
}

let tree = BinaryTree.node(1, left: .empty, right: .node(2, left: .empty, right: .empty))

print(tree)
Aempty
Bnode(1, left: node(2, left: empty, right: empty), right: empty)
Cnode(1, left: empty, right: node(2, left: empty, right: empty))
Dnode(2, left: empty, right: empty)
Attempts:
2 left
💡 Hint
Look at how the tree is constructed with left and right children.
🧠 Conceptual
intermediate
1:30remaining
Understanding 'indirect' keyword in recursive enums
Why is the 'indirect' keyword necessary in recursive enumerations in Swift?
AIt makes the enum cases mutable.
BIt enables the enum to be used only with classes.
CIt allows the enum to conform to protocols automatically.
DIt tells Swift to store the enum case indirectly to avoid infinite size during compilation.
Attempts:
2 left
💡 Hint
Think about how recursive data structures are stored in memory.
🔧 Debug
advanced
1:30remaining
Identify the error in this recursive enum definition
What error will this Swift code produce?
Swift
enum List {
    case empty
    case node(Int, List)
}
ARuntime error: Stack overflow when creating a list.
BCompiler error: Recursive enum 'List' is not marked 'indirect'.
CNo error, code compiles and runs fine.
DCompiler error: Missing associated value label.
Attempts:
2 left
💡 Hint
Check if the enum is marked 'indirect' for recursion.
Predict Output
advanced
2:00remaining
Output of recursive enum with computed property
What is the output of this Swift code?
Swift
indirect enum Expression {
    case number(Int)
    case addition(Expression, Expression)

    func evaluate() -> Int {
        switch self {
        case .number(let value):
            return value
        case .addition(let left, let right):
            return left.evaluate() + right.evaluate()
        }
    }
}

let expr = Expression.addition(.number(3), .addition(.number(4), .number(5)))
print(expr.evaluate())
A12
B9
CCompilation error
D7
Attempts:
2 left
💡 Hint
Evaluate the expression tree from the leaves up.
📝 Syntax
expert
2:00remaining
Correct syntax for marking only one case as indirect
Which option correctly marks only the recursive case as indirect in this enum?
Swift
enum Tree {
    case leaf(Int)
    case node(Tree, Tree)
}
A
enum Tree {
    case leaf(Int)
    indirect case node(Tree, Tree)
}
B
indirect enum Tree {
    case leaf(Int)
    case node(Tree, Tree)
}
C
enum Tree {
    indirect case leaf(Int)
    case node(Tree, Tree)
}
D
enum Tree {
    case leaf(Int)
    case node(indirect Tree, indirect Tree)
}
Attempts:
2 left
💡 Hint
Only the recursive case needs the 'indirect' keyword before it.