0
0
Swiftprogramming~20 mins

Lazy collections for performance in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Lazy Collections Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this lazy map operation?
Consider the following Swift code using a lazy collection. What will be printed?
Swift
let numbers = [1, 2, 3, 4, 5]
let lazySquares = numbers.lazy.map { num in
    print("Mapping \(num)")
    return num * num
}
print("Before accessing elements")
let firstSquare = lazySquares.first
print("First square: \(firstSquare ?? 0)")
ABefore accessing elements\nMapping 1\nFirst square: 1
BMapping 1\nBefore accessing elements\nFirst square: 1
CBefore accessing elements\nMapping 1\nMapping 2\nMapping 3\nMapping 4\nMapping 5\nFirst square: 1
DMapping 1\nMapping 2\nMapping 3\nMapping 4\nMapping 5\nBefore accessing elements\nFirst square: 1
Attempts:
2 left
💡 Hint
Remember that lazy collections delay computation until elements are accessed.
Predict Output
intermediate
2:00remaining
How many elements are processed in this lazy filter?
Given this Swift code, how many times will the print statement inside the filter closure run?
Swift
let numbers = Array(1...10)
let lazyFiltered = numbers.lazy.filter { num in
    print("Filtering \(num)")
    return num % 2 == 0
}
let firstTwo = Array(lazyFiltered.prefix(2))
A4 times
B2 times
C10 times
D5 times
Attempts:
2 left
💡 Hint
The filter stops after finding enough elements for prefix(2).
🔧 Debug
advanced
2:00remaining
Why does this lazy map not print anything?
Look at this Swift code using lazy map. Why does it produce no output?
Swift
let numbers = [1, 2, 3]
let lazyMapped = numbers.lazy.map { num in
    print("Mapping \(num)")
    return num * 2
}
// No further code
ABecause the print statement is inside a closure that is immediately executed
BBecause the lazy map closure is never executed without accessing elements
CBecause the array is empty
DBecause lazy collections do not support print statements
Attempts:
2 left
💡 Hint
Think about when lazy collections run their closures.
Predict Output
advanced
2:00remaining
What is the output of this chained lazy operation?
What will this Swift code print?
Swift
let numbers = [1, 2, 3, 4, 5]
let lazyResult = numbers.lazy.filter { num in
    print("Filtering \(num)")
    return num % 2 != 0
}.map { num in
    print("Mapping \(num)")
    return num * 10
}
print("Start accessing")
let result = Array(lazyResult.prefix(2))
print("Result: \(result)")
AStart accessing\nFiltering 1\nFiltering 2\nFiltering 3\nFiltering 4\nFiltering 5\nMapping 1\nMapping 3\nMapping 5\nResult: [10, 30]
BFiltering 1\nFiltering 2\nFiltering 3\nFiltering 4\nFiltering 5\nMapping 1\nMapping 3\nMapping 5\nStart accessing\nResult: [10, 30]
CStart accessing\nFiltering 1\nMapping 1\nFiltering 2\nFiltering 3\nMapping 3\nResult: [10, 30]
DStart accessing\nFiltering 1\nMapping 1\nFiltering 2\nMapping 2\nFiltering 3\nMapping 3\nResult: [10, 20, 30]
Attempts:
2 left
💡 Hint
Remember lazy evaluation stops once prefix(2) elements are found.
🧠 Conceptual
expert
2:00remaining
Why use lazy collections for performance in Swift?
Which of the following best explains why lazy collections improve performance?
AThey automatically parallelize operations across CPU cores
BThey compute all elements immediately to avoid delays later
CThey store all intermediate results in memory for faster access
DThey delay computation until elements are needed, reducing unnecessary work
Attempts:
2 left
💡 Hint
Think about when lazy collections run their operations.