Bird
Raised Fist0
Swiftprogramming~20 mins

BuildBlock for combining results in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift BuildBlock 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 Swift code using @resultBuilder?
Consider the following Swift code that uses a custom @resultBuilder named ResultCombiner. What will be printed when combineResults() is called?
Swift
import Foundation

@resultBuilder
struct ResultCombiner {
    static func buildBlock(_ components: String...) -> String {
        components.joined(separator: ", ")
    }
}

func combineResults() -> String {
    @ResultCombiner var result: String {
        "Hello"
        "World"
        "Swift"
    }
    return result
}

print(combineResults())
A"Hello, World, Swift"
B"HelloWorldSwift"
C"[Hello, World, Swift]"
D"Hello World Swift"
Attempts:
2 left
💡 Hint
Look at how the buildBlock function joins the components.
🧠 Conceptual
intermediate
1:30remaining
What does the buildBlock function do in a Swift result builder?
In Swift's result builder, what is the main role of the buildBlock static function?
AIt initializes the result builder instance.
BIt combines multiple components into a single result.
CIt validates the types of components passed.
DIt handles errors during building.
Attempts:
2 left
💡 Hint
Think about how multiple parts are combined into one output.
🔧 Debug
advanced
2:00remaining
Why does this buildBlock implementation cause a compile error?
Examine this buildBlock function inside a Swift result builder. Why does it cause a compile error?
static func buildBlock(_ components: String) -> String {
    components + "!"
}
Swift
static func buildBlock(_ components: String) -> String {
    components + "!"
}
AbuildBlock cannot be static.
BYou cannot use + operator on String in buildBlock.
CbuildBlock must return an Int, not String.
DbuildBlock must accept a variadic parameter, not a single String.
Attempts:
2 left
💡 Hint
Check the parameter type expected by buildBlock.
📝 Syntax
advanced
2:00remaining
Which buildBlock implementation correctly combines Int values into their sum?
You want to create a buildBlock function that sums all Int components passed to it. Which option is correct?
Astatic func buildBlock(_ components: Int...) -> Int { components.add() }
Bstatic func buildBlock(components: Int...) -> Int { components.reduce(0, +) }
Cstatic func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) }
Dstatic func buildBlock(_ components: [Int]) -> Int { components.sum() }
Attempts:
2 left
💡 Hint
Check parameter syntax and method names for summing arrays.
🚀 Application
expert
3:00remaining
How to combine optional String results safely in buildBlock?
You want to combine multiple optional String components in buildBlock, ignoring nil values and joining non-nil with ", ". Which implementation achieves this?
A
static func buildBlock(_ components: String?...) -> String {
    components.compactMap { $0 }.joined(separator: ", ")
}
B
static func buildBlock(_ components: String?...) -> String {
    components.map { $0 ?? "" }.joined(separator: ", ")
}
C
static func buildBlock(_ components: [String?]) -> String {
    components.joined(separator: ", ")
}
D
static func buildBlock(_ components: String?...) -> String {
    components.reduce("") { $0 + $1! + ", " }
}
Attempts:
2 left
💡 Hint
Consider how to safely unwrap optionals and skip nils.

Practice

(1/5)
1. What does BuildBlock do when combining multiple Result values in Swift?
easy
A. It returns failure only if all Result values are failures.
B. It returns failure if any Result is failure, otherwise success.
C. It always returns success regardless of individual results.
D. It merges all success values into an array without checking failures.

Solution

  1. Step 1: Understand BuildBlock behavior

    BuildBlock combines multiple Result values and checks each one.
  2. Step 2: Check failure condition

    If any Result is a failure, the combined result is failure; otherwise, it is success.
  3. Final Answer:

    It returns failure if any Result is failure, otherwise success. -> Option B
  4. Quick Check:

    Failure if any fail = C [OK]
Hint: Remember: one failure causes whole BuildBlock to fail [OK]
Common Mistakes:
  • Assuming it returns success if any result is success
  • Thinking it merges all successes without failure check
  • Believing it fails only if all results fail
2. Which of the following is the correct syntax to combine two Result values using BuildBlock in Swift?
easy
A. BuildBlock(result1, result2)
B. BuildBlock[result1, result2]
C. BuildBlock{result1, result2}
D. BuildBlock->(result1, result2)

Solution

  1. Step 1: Recall BuildBlock usage

    BuildBlock is called like a function with comma-separated arguments inside parentheses.
  2. Step 2: Identify correct syntax

    Only BuildBlock(result1, result2) matches the correct function call syntax.
  3. Final Answer:

    BuildBlock(result1, result2) -> Option A
  4. Quick Check:

    Function call syntax = A [OK]
Hint: Use parentheses for function calls, not brackets or braces [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Using braces which are for closures
  • Using arrow syntax which is invalid here
3. What is the output of this Swift code?
let r1: Result = .success(10)
let r2: Result = .failure(NSError(domain: "", code: 1))
let combined = BuildBlock(r1, r2)
switch combined {
 case .success(let values): print(values)
 case .failure(let error): print("Error code: \((error as NSError).code)")
}
medium
A. Error code: 1
B. 10
C. Error code: 0
D. Compilation error

Solution

  1. Step 1: Analyze input results

    r1 is success with 10, r2 is failure with error code 1.
  2. Step 2: Understand BuildBlock combining

    Since r2 is failure, combined result is failure with that error.
  3. Step 3: Switch prints error code

    The failure case prints "Error code: 1" from the NSError code.
  4. Final Answer:

    Error code: 1 -> Option A
  5. Quick Check:

    Failure present, print error code 1 = A [OK]
Hint: If any result fails, output error info, not success value [OK]
Common Mistakes:
  • Assuming success prints 10 despite failure
  • Confusing error code with 0
  • Expecting compilation error due to BuildBlock
4. Identify the error in this Swift code using BuildBlock:
let r1: Result = .success("Hi")
let r2: Result = .success("There")
let combined = BuildBlock(r1 + r2)
medium
A. BuildBlock cannot combine success values.
B. BuildBlock requires at least three arguments.
C. Result types must be integers to combine.
D. You cannot use + operator directly on Result types.

Solution

  1. Step 1: Check operator usage on Result

    The code tries to add two Result values with +, which is invalid.
  2. Step 2: Understand correct BuildBlock usage

    BuildBlock takes multiple Result arguments separately, not combined with +.
  3. Final Answer:

    You cannot use + operator directly on Result types. -> Option D
  4. Quick Check:

    + operator invalid on Result = D [OK]
Hint: Pass results as separate arguments, not combined with + [OK]
Common Mistakes:
  • Trying to add Result values directly
  • Thinking BuildBlock needs 3+ arguments
  • Believing only integers can be combined
5. You have three Result values:
let r1: Result = .success(5)
let r2: Result = .success(10)
let r3: Result = .success(15)

Using BuildBlock, how can you combine them to get a single Result with an array of all success values?
hard
A. Use BuildBlock(r1, r2, r3) directly; it returns array of values.
B. Use BuildBlock(r1, r2, r3).flatMap { [$0 + $1 + $2] }
C. Use BuildBlock(r1, r2, r3).map { [$0, $1, $2] }
D. Use BuildBlock([r1, r2, r3]) to combine into array.

Solution

  1. Step 1: Understand BuildBlock output

    It combines multiple Result values into a single Result with a tuple of success values.
  2. Step 2: Convert tuple to array

    Use map to transform the tuple ($0, $1, $2) into an array [$0, $1, $2].
  3. Final Answer:

    Use BuildBlock(r1, r2, r3).map { [$0, $1, $2] } -> Option C
  4. Quick Check:

    Map tuple to array after BuildBlock = B [OK]
Hint: BuildBlock returns tuple; map it to array for combined values [OK]
Common Mistakes:
  • Expecting BuildBlock to return array directly
  • Using flatMap incorrectly to sum values
  • Passing array of results instead of separate arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard