Swift interview questions
53 questions from real iOS interviews. Topics: Optionals, generics, protocols, value and reference types, closures, Codable.
- SwiftMultiple choice
Optional unwrapping
Which of these unwrap an optional safely? Pick all that apply.
#optional #unwrap
- SwiftMultiple choice
Value semantics
What does this code print? …
#struct #class #value-semantics #let
- SwiftMultiple choice
Type casting
Given let view: UIView = UILabel(), which of these casts can crash at runtime?
#type-casting #as #downcast #optional
- SwiftShort answer
Why optionals exist
Why does Swift have optionals?
#optional #enum #nil #safety
- SwiftMultiple choice
Collection types
You have tens of thousands of user IDs and need to check repeatedly whether a given ID is among them. Which collection fits best?
#array #set #dictionary #hashable
- SwiftMultiple choiceMeesho
Optionals and IUOs
Which of these three lines compile? Pick all that apply. …
#optional #iuo #implicitly-unwrapped #nil
- SwiftShort answerCapital One
Combining higher-order functions
Take ["a", "b", "c"] as input and produce ["c-0", "b-1", "a-2"]. You can't use a for loop, only higher-order functions. Which functions would you combine, and …
#higher-order #map #enumerated #reversed
- SwiftShort answerAdobe
Tuples: uses and limits
When is it appropriate to use a tuple in Swift?
#tuple #struct #multiple-return #destructuring
- SwiftMultiple choiceOura
Choosing a struct
In which of these cases is a struct the right choice over a class in Swift? Pick all that apply.
#struct #class #value-type #reference-type
- SwiftMultiple choiceInfosysinterview report
guard statement rules
Which statements about Swift's guard statement are correct? Pick all that apply.
#guard #early-exit #optional-binding #control-flow
- SwiftShort answer브랜디interview report
Force unwrap rule
Your team's code review rules include "no force unwrapping (!)." Why have a rule like that?
#optional #force-unwrap #iuo #code-review
- SwiftShort answerAppleinterview report
Value vs reference types
In Swift, what's the fundamental difference between a struct and a class?
#struct #class #value-type #cow
- SwiftShort answer
Escaping closures
This code doesn't compile. What is the compiler asking for? …
#closure #escaping #non-escaping #capture
- SwiftMultiple choice
Access control
In a framework module, what's the difference between declaring open class and public class? Pick all correct statements.
#access-control #open #public #module
- SwiftShort answer
Protocol-oriented design
A teammate wants to create a BaseViewModel class and have every screen inherit from it. If you proposed a protocol-oriented alternative, how would you design i…
#protocol #pop #oop #protocol-extension
- SwiftShort answer
mutating and value types
Which line in this code fails to compile, and why? …
#mutating #struct #value-semantics #inout
- SwiftMultiple choice
defer ordering
What's the output order when you call this function? …
#defer #scope #cleanup
- SwiftShort answer
Any, AnyObject, NSObject
What can Any, AnyObject, and NSObject each hold?
#any #anyobject #nsobject #existential
- SwiftMultiple choice
Enum rules
Which statements about Swift enum are correct? Pick all that apply.
#enum #associated-value #raw-value #pattern-matching
- SwiftMultiple choice
Higher-order functions
What does this code print? …
#map #compactmap #flatmap #higher-order-function
- SwiftMultiple choice
lazy properties
Which statements about a lazy var stored property are correct? Pick all that apply.
#lazy #stored-property #initialization #thread-safety
- SwiftShort answer
KeyPath values
In Swift, what exactly does a KeyPath like \.name represent as a value?
#keypath #writablekeypath #referencewritablekeypath #kvo
- SwiftShort answer라인
Serialization model types
When you build model types to serialize and deserialize a server's JSON response, should you use a class or a struct? Give your reasons.
#struct #class #codable #serialization
- SwiftMultiple choiceMeesho
Recursive value types
What happens when you compile this code? And if you change the same definition to class Person? …
#struct #class #recursive #memory-layout
- SwiftMultiple choiceZomato
Swift type rules
Which of these statements about Swift language rules are correct? Pick all that apply.
#initializer #deinit #access-control #closure
- SwiftShort answerGojek
private vs fileprivate
In one file you have class A { fileprivate func secret() {} } and class B. Can B call A().secret(), and what changes if you switch it to private?
#access-control #private #fileprivate #extension
- SwiftShort answerWalmart
Implementing compactMap
Extend Array with a myCompactMap that behaves like the standard library's compactMap. How would you write the generic signature?
#generics #rethrows #extension #compactmap
- SwiftShort answerSwiggy
Extending Optional
Extend Optional so that any optional can be asked for its nil status, like someOptional.isNil. How would you implement it?
#optional #extension #enum #pattern-matching
- SwiftMultiple choiceDiDi
Init and observer rules
Which of these statements about class initialization, deinitialization, and property observers in Swift are correct? Pick all that apply.
#deinit #required #property-observer #set
- SwiftShort answerIBMinterview report
Generics vs Any
During code review you find the same logic overloaded per type: func larger(_ a: Int, _ b: Int) -> Int, larger(_ a: Double, _ b: Double) -> Double, larger(_ a:…
#generics #any #specialization #type-safety
- SwiftMultiple choiceTinderinterview report
inout parameters
Which statements about Swift's inout parameters (func bump(_ x: inout Int)) are correct? Pick all that apply.
#inout #copy-in-copy-out #exclusivity #value-type
- SwiftShort answerSAP
Protecting the original
You passed a class instance to a function, changed a property inside it, and the caller's original changed too. How do you protect the original?
#reference-type #deep-copy #shallow-copy #nscopying
- SwiftShort answerJD.com
How Swift enums differ
A colleague who mostly writes Java or C asks whether a Swift enum is just a list of constants in the end. What's fundamentally different about a Swift enum com…
#enum #associated-values #sum-type #exhaustive-switch
- SwiftShort answerCapgeminiinterview report
How optional chaining behaves
When you chain several optional accesses like this and hit nil partway through, what happens? …
#optional-chaining #optional #short-circuit #flatten
- SwiftShort answerCognizantinterview report
Type inference cost
Swift has type inference, so you rarely have to write types, yet your team convention says "annotate types on complex expressions and public APIs." How does ty…
#type-inference #compile-time #let #var
- SwiftShort answer
Opaque vs existential types
From the caller's side, what's the difference between func make() -> some Shape and func make() -> any Shape?
#some #any #existential #opaque
- SwiftShort answerUber
Generics and type erasure
You have an Iterator protocol with getNext() and hasNext(), and you want to change it from Int-only to generic with associatedtype Element. After the change, b…
#generic #associatedtype #type-erasure #any
- SwiftShort answer
Error handling
When designing a network layer, which would you make the default: throws functions, returning Result<T, E>, or Swift 6 typed throws (throws(NetworkError))?
#error #throws #result #typed-throws
- SwiftShort answer
Codable decoding strategies
The server's JSON uses snake_case keys, and dates come as strings like "2026-09-11T10:00:00Z". What would you set up on the decoder to decode this response wit…
#codable #decodable #codingkeys #json
- SwiftShort answer
String internals
Why can't you index a String with an integer like text[3], and why is text.count O(n)?
#string #unicode #grapheme #index
- SwiftShort answerTikTok
Method dispatch
In Swift, which method calls use static, table, and message dispatch?
#dispatch #static-dispatch #dynamic-dispatch #vtable
- SwiftMultiple choice
Property wrappers
What does this code print? …
#property-wrapper #projectedvalue #wrappedvalue
- SwiftShort answer
Initialization rules
Why doesn't this code compile? Can you explain it using the two-phase initialization rules? …
#initializer #designated #convenience #two-phase
- SwiftShort answer네이버웹툰
ContiguousArray vs Array
Say your resume claims "ContiguousArray is faster than Array." In a pre-interview the interviewer asks, "Then why don't developers use ContiguousArray much in …
#array #contiguousarray #performance #bridging
- SwiftShort answerAirbnb
An iterator of iterators
Given a 'collection of collections' like [[1, 2], [], [3], [4, 5, 6]], how would you design an iterator that hands out elements one at a time in flat order (1,…
#iteratorprotocol #sequence #flatten #generics
- SwiftShort answerXiaohongshu
Extensions and stored properties
You want to add a property to an existing type with an extension. Computed properties work, so why not stored properties?
#extension #stored-property #memory-layout #associated-object
- SwiftShort answerVerizoninterview report
Large collections in structs
You need to put a very large Dictionary, hundreds of thousands of entries, into a struct property and pass that struct around by value. What's the 'fastest' wa…
#cow #dictionary #value-type #performance
- SwiftShort answerGrabinterview report
Designing a custom Optional
Suppose there were no question-mark optional syntax and no standard Optional. If you had to design a type in Swift that expresses 'a value or nothing,' how wou…
#optional #enum #generics #map
- SwiftShort answer야놀자interview report
Using a C++ library
You need to use a C++ video-processing library from a Swift app. Would you go with the old approach (an Objective-C++ wrapper) or Swift 5.9's C++ interop?
#cpp-interop #objective-c++ #bridging #ownership
- SwiftMultiple choice
Protocol extension dispatch
What does this code print? …
#protocol #extension #dispatch #witness-table
- SwiftShort answer
Implementing copy-on-write
You need a struct ImageBuffer that wraps a large pixel buffer, keeps value semantics, and avoids needless copies. If you implemented copy-on-write yourself, wh…
#cow #copy-on-write #isknownuniquelyreferenced #value-semantics
- SwiftShort answer
Objective-C interop
A Swift module has to call into legacy Objective-C code and be called from it. How do you set up bridging in both directions?
#objc #bridging #interop #dynamic
- SwiftShort answer
ABI and library evolution
You're about to ship an internal shared module as an XCFramework binary. What do Swift ABI stability and Library Evolution (BUILD_LIBRARY_FOR_DISTRIBUTION) eac…
#abi #library-evolution #frozen #inlinable