Swift ARC Interview Questions: Memory Management Practice
Reviewed by Mark Dickie · Last updated
4 questions on this page you can answer and see marked on the spot. Go to the first one
Swift's Automatic Reference Counting (ARC) is a compile-time memory management system that tracks the number of strong references to each class instance and frees that instance when the count drops to zero. For interviews, you need to know how strong, weak, and unowned references differ, how retain cycles form and how to break them, and what happens when you capture self inside a closure. You should also understand the distinction between escaping and non-escaping closures in the context of ARC, and when the system can safely use an unowned reference instead of a weak one.
What does a Swift ARC interview test?
| Reference type | Retain count impact | When to use | Risk if misused |
|---|---|---|---|
| strong | increments by 1 | default ownership | retain cycle if mutual |
| weak | no increment | optional back-reference | nils out when object deallocates |
| unowned | no increment | non-optional, same or shorter lifetime | crash if accessed after dealloc |
Interviewers typically probe whether you can spot a retain cycle in code and explain the fix:
- Two class instances holding strong references to each other, such as a parent and child both using
strong. - A closure that captures
selfstrongly and is stored as a property onself, creating a cycle that outlives the intended scope. - Fixing the cycle with a capture list:
[weak self]or[unowned self]inside the closure, choosing based on whetherselfcan become nil. - Using
weakfor delegate properties andunownedwhen the referenced object is guaranteed to live at least as long as the holder.
How do closures interact with ARC in Swift?
Closures capture variables by reference by default. When a closure is stored as a property of a class instance and references that same instance, the closure holds a strong reference to self and self holds a strong reference to the closure, creating a classic retain cycle. Capture lists break the cycle by letting you specify a weak or unowned capture of self at the point the closure is created. Escaping closures (marked @escaping) are stored or called after the function returns, so they may outlive the scope where they were defined; non-escaping closures are called within the function and cannot create the same kind of cycle.
Key facts
- Tarmac has 11 Swift interview questions on this topic, 10 of them on this page, at difficulty 1–5 of 5.
- Tarmac tracked 180 job postings asking for Swift in November 2026.
- Roles asking for Swift advertise a median base salary of US$169,050, across 55 job postings as of November 2026.
- Tarmac last reviewed these Swift interview questions on 5 October 2026.
At a glance
| Questions | 10 shown · 11 in the bank |
|---|---|
| Difficulty | 1–5 of 5 |
| Formats | True / false, Short answer, Fill in the blank, Code output, Multiple choice, Flashcard, Ordering, Find the bug |
What you'll review
- arc
Practice questions
Try one before you open the answer. Pick an option and press Check; it's marked on the spot.
True or false: In Swift, if two class instances hold strong references to each other, a retain cycle is created and neither instance is deallocated even when there are no other references to either one.#
Options
Show answer
True. In Swift, two class instances that hold strong references to each other form a retain cycle, and neither is deallocated once outside references are gone. Each instance keeps the other's reference count at one or more, so ARC never drops either count to zero. Making one side weak or unowned breaks the cycle.
Each instance keeps the other's reference count at or above 1 through its strong reference, so neither count ever reaches zero and ARC never deallocates either instance, even after all external references are gone. This is a classic strong reference cycle, the main pitfall of ARC, which weak or unowned references are used to break.
Swift uses ARC (Automatic Reference Counting) for memory management of class instances. Briefly explain what ARC does and at what point (compile time vs runtime) its reference-counting operations are inserted.#
Show answer
ARC inserts retain and release calls at compile time to track the reference count of each class instance. When the count drops to zero, ARC deallocates the instance automatically; there is no separate runtime garbage collector.
ARC is a compile-time mechanism: the Swift compiler inserts retain/release calls into your code so that every strong reference increments the count and every drop of a reference decrements it. When the count reaches zero the instance's memory is freed. Unlike a tracing garbage collector, ARC does not pause execution at runtime to find unreachable objects—it relies entirely on the retain/release calls it inserted at compile time.
In Swift, ARC manages memory only for instances of reference types such as classes. When an instance's strong reference count drops to zero, ARC deallocates it, and just before that happens the class's deinitializer, written _____, is called automatically. To refer to an instance without increasing its strong reference count, using a non-optional reference that is assumed to always have a value, you mark the reference with the base keyword _____, without any parenthesized qualifier.#
Show answer
In Swift, ARC manages memory only for instances of reference types such as classes. When an instance's strong reference count drops to zero, ARC deallocates it, and just before that happens the class's deinitializer, written **deinit**, is called automatically. To refer to an instance without increasing its strong reference count, using a non-optional reference that is assumed to always have a value, you mark the reference with the base keyword **unowned**, without any parenthesized qualifier.
Swift calls a class's deinitializer, written deinit, right before ARC frees an instance whose strong reference count has dropped to zero. weak and unowned references both leave the strong count unchanged. A weak reference must be optional, and ARC sets it to nil when the instance is freed. The second blank asks for the non-optional, assumed-always-has-a-value form, which is the base keyword unowned. Since Swift 5 an unowned reference can also be declared optional, but the prompt's wording excludes that case. An unowned reference never becomes nil automatically, so reading it after the instance has been freed is a runtime trap rather than a safe nil. The qualified spellings unowned(safe) and unowned(unsafe) are variants of the same keyword and are not the answer key here.
What is printed to the console when this Swift code runs? Assume it is the entire program in a main.swift-style top-level file.#
class Container {
var value: Int
var child: Child?
init(_ v: Int) { self.value = v }
deinit { print(value) }
}
class Child {
weak var parent: Container?
}
var parent: Container? = Container(1)
let child = Child()
child.parent = parent
parent!.child = child
parent = nilShow answer
1
The program prints 1. The Container is held by the strong variable parent and is referenced weakly by child.parent, which does not contribute to its retain count. Its only strong reference is parent, so setting parent = nil drops the count to zero, ARC deallocates the Container, and its deinit prints value, which is 1. The Child is not deallocated because the top-level let child still holds a strong reference to it, so nothing else is printed. The Container's child property is released during deallocation, which only lowers the Child's count from two to one.
What does the following Swift code print to the console?#
class Gadget {Show answer
init X
true
deinit X
true
The Gadget(name: “X”) initializer runs first, printing init X. The weak var weakRef holds a non-owning reference to the same object, so print(weakRef != nil) prints true while the object is still alive. Setting strongRef = nil removes the last strong reference; ARC deallocates the object, triggering deinit which prints deinit X, and the weak reference is automatically set to nil. The final print(weakRef == nil) therefore prints true.
In Swift 5, a class Customer holds a strong reference to a CreditCard, and CreditCard declares unowned let customer: Customer. All strong references to the Customer instance go away, but some other code still holds a strong reference to the CreditCard. What happens when that code then reads card.customer.name?#
Options
Show answer
In Swift, reading a safe unowned reference after its object has been deallocated crashes the program with a runtime trap. unowned does not keep the object alive and is never set to nil; only weak references are zeroed. The runtime checks the access and stops with a fatal error. Skipping that check and reading stale memory only happens with unowned(unsafe).
A safe unowned reference (the default, not unowned(unsafe)) does not keep its target alive, and Swift never sets it to nil. Only weak references are zeroed, which is why they must be optionals. At runtime, accessing an unowned reference after its object is deinitialized fails a check and traps. Swift tracks this with a separate unowned reference count, which keeps the object's memory allocated but deinitialized until the last unowned reference goes away. So no nil is returned, and the object is not kept alive. Reading stale memory without a check only happens with unowned(unsafe).
In Swift ARC (Swift 5 and later), how do weak and unowned references differ, and when is unowned the better choice?#
Show answer
weak references must have an Optional type, and Swift sets them to nil automatically when the referenced object is deallocated. unowned references do not keep the object alive and are not cleared automatically. Since Swift 5, unowned can also be applied to Optional types, but an Optional unowned reference only holds nil if you initialize it that way. Reading a safe unowned reference (the default, also written unowned(safe)) after the object is deallocated traps at runtime. unowned(unsafe) skips that check, and reading a dangling reference through it is undefined behavior. Choose unowned when the referenced object is guaranteed to outlive the reference holder, for example a CreditCard holding unowned let customer: Customer when a card cannot exist without its customer. Choose weak when the referenced object may be deallocated first, such as a delegate.
Both weak and unowned are non-owning references, so neither keeps the referenced object alive and both avoid retain cycles. The key difference is what happens after deallocation: a weak reference becomes nil automatically, while a safe unowned reference traps when read. Swift 5 allows unowned on Optional types, so the nil-ness of the type is not the distinguishing feature; the lifetime guarantee is. Use unowned when the referrer cannot outlive the referenced object, and weak when the referenced object may die first.
Consider this complete Swift program (top-level main.swift, run with the current Swift toolchain):#
Put these in order
Show answer
The console shows: start, Derived deinit, Base deinit, Tracker deinit, end, w is nil: true. In Swift, deinitializers run from the subclass up to the superclass. Stored properties are released only after every deinit body has finished. All of this happens as soon as the last strong reference is removed, so the weak reference already reads as nil on the final line.
obj holds the only strong reference; w is weak and doesn't add to the count. After "start" prints, obj = nil drops the strong count to zero, so ARC deinitializes the object right away, during that assignment. Deinitializers run from the subclass up: the Derived deinit body runs first, and the superclass deinit is then called automatically. Only after every deinit body has finished does Swift destroy the stored properties. Releasing t drops the Tracker count to zero, so "Tracker deinit" prints after "Base deinit". Finally the weak reference reads as nil, so the last line ends in true.
Consider the following Swift program. Monitor has a weak property peer and a probe closure that captures another Monitor instance via a [weak] capture-list entry. Trace the exact console output when the program runs to completion in a standard Swift runtime (ARC, no optimizations that alter deallocation timing).#
class Monitor { let tag: String var probe: (() -> String)? weak var peer: Monitor? init(tag: String) { self.tag = tag } deinit { if let p = probe { print(“deinit “ + tag + “: probe=” + p()) } else { print(“deinit “ + tag + “: probe=nil”) } } } var m1: Monitor? = Monitor(tag: “alpha”) var m2: Monitor? = Monitor(tag: “beta”) m1?.peer = m2 m2?.peer = m1 m1?.probe = { [weak m2] in m2?.tag ?? “gone” } m2?.probe = { [weak m1] in m1?.tag ?? “gone” } m2 = nil m1 = nil print(“finished”)Show answer
deinit beta: probe=alpha
deinit alpha: probe=gone
finished
At the point both m1 and m2 are created, each has a strong reference count of 1 (from the local variables). The peer properties are weak, so they contribute no extra retains. The probe closures capture their targets with [weak], which also adds no retains. When m2 = nil drops m2's last strong reference, its refcount hits zero and its deinit fires. Inside that deinit, m2.probe is still alive (properties are released only after deinit returns), and the closure's [weak m1] capture finds m1 still alive (refcount 1 from var m1), so p() returns “alpha” — printing deinit beta: probe=alpha. After m2's deinit completes, the runtime releases m2's stored properties and clears every weak reference to m2, including the weak m2 inside m1's probe closure, which becomes nil. When m1 = nil then drops m1's last strong reference, m1's deinit runs. Its probe closure's [weak m2] capture now resolves to nil (m2 was already fully deallocated), so p() returns “gone” — printing deinit alpha: probe=gone. Finally print(“finished”) produces the last line.
In a release (-O) build, this function sometimes prints nothing. What is the bug?#
final class Handler {
func handle() { print("handled") }
}
final class Worker {
weak var handler: Handler?
func run() { handler?.handle() }
}
func start() {
let worker = Worker()
let handler = Handler()
worker.handler = handler
worker.run()
}Options
Show answer
Swift does not guarantee that a local strong reference lives until the end of its lexical scope. The last use of handler is the assignment to the weak property, so ARC may release it before worker.run(). That leaves the weak reference nil. The fix is to wrap the call in withExtendedLifetime(handler) { worker.run() }.
ARC in Swift ties an object's lifetime to the last use of its strong reference, not to the end of the lexical scope. After worker.handler = handler, the local handler is never read again. The optimizer may therefore put the final release right after that assignment, and the Handler instance is deallocated while the only remaining reference to it is weak. When worker.run() executes, handler?.handle() finds nil and prints nothing. Debug builds usually keep locals alive longer, which is why the failure shows up only sometimes and only under -O. The documented fix is withExtendedLifetime(handler) { worker.run() }, or making the reference strong. The other explanations are wrong. There is no retain cycle, because Worker holds Handler weakly and Handler holds nothing. Optional chaining on a weak reference behaves the same in every build. A final class can be weakly referenced like any other class.
Related interview questions
Job market
See swift salaries and hiring demand from live job postings.
The other 1 question
This page shows 10 and marks what you pick. That's as far as a page can go. A free account opens the other 1 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.
Free · the whole bank · 100 answers per 30 days, each marked with written feedback