← Snapchat Interview Insights

Snapchat·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Snapchat software engineering interview with a Swift concurrency question that looks deceptively simple until you actually have to explain it out loud.

Questions Asked (1)

Q1

In Swift, predict the print output and order for this code snippet using two async dispatches on the main queue, and explain why the second closure prints '123' instead of '13' even though String is a value type.

Technical Trade-offsAlgorithms & Data Structures
Author's notes

This one got me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the code snippet and the exact dispatch mechanism (e.g., DispatchQueue.main.async). Then, reason step-by-step about the execution order: the async blocks are enqueued and executed later on the main queue, so the print statements inside them occur after the current synchronous code. Finally, explain that the second closure prints '123' because it captures a reference to the same mutable String variable, and by the time it runs, the variable has been mutated to '123'.

Pro tip: Mention that this behavior is due to reference capture of variables in closures, not value semantics of String itself. Also, note that if the variable were a let constant or if the closure captured a copy (e.g., via capture list), the output would differ.

1. Identify the dispatch mechanism

Recognize that DispatchQueue.main.async enqueues the closure to run later on the main queue, after the current synchronous code completes.

2. Determine execution order

Since both async blocks are enqueued, they will execute in FIFO order after the current run loop iteration. Any synchronous print statements outside the blocks will execute first.

3. Analyze variable capture

The closures capture the variable (not its value) by reference. So both closures see the same mutable storage. The second closure sees the final mutated value.

4. Explain the '123' output

Even though String is a value type, the variable itself is captured by reference. When the second closure runs, the variable has been changed to '123', so it prints '123' instead of '13'.

5. Address the value type misconception

Clarify that value type semantics apply to the String instance, but the closure captures the variable (a reference to the storage), not a copy of the String. This is why mutation is visible.

Key Points to Mention

  • DispatchQueue.main.async enqueues work to be executed later on the main queue.
  • Closures capture variables by reference, not by value, unless a capture list is used.
  • The order of execution: synchronous code first, then async blocks in FIFO order.
  • String is a value type, but the variable holding it is captured by reference.
  • Mutation of the variable before the closure runs affects the captured value.
  • Using a capture list (e.g., [str]) would capture the value at closure creation time, changing the output.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.