The most asked Swift interview questions with structured answers — a quick answer, a clear explanation and working code for each. Covers basics for freshers through advanced, scenario and coding questions for experienced developers.
Updated November 2026 Reviewed by Abhishek Kumar 120 min read
// Check palindrome in SwiftfuncisPalindrome(_str: String) -> Bool {
let cleaned = str.lowercased().filter { $0.isLetter ||$0.isNumber }
return cleaned ==String(cleaned.reversed())
}
print(isPalindrome("racecar")) // trueprint(isPalindrome("hello")) // false// Two-pointer approachfuncisPalindromeTwoPointer(_str: String) -> Bool {
let chars =Array(str.lowercased().filter { $0.isLetter ||$0.isNumber })
var left =0var right = chars.count -1while left < right {
if chars[left] != chars[right] {
returnfalse
}
left +=1
right -=1
}
returntrue
}
print(isPalindromeTwoPointer("A man a plan a canal Panama")) // true
Quick answerCalculate factorial using recursion or iteration.
Recursive: n * factorial(n-1)
Iterative: Loop with multiplication
Base case: n <= 1
Edge cases: 0! = 1
Swift
// Factorial in Swiftfuncfactorial(_n: Int) -> Int {
if n <=1 {
return1
}
return n * factorial(n -1)
}
print(factorial(5)) // 120// Iterative versionfuncfactorialIterative(_n: Int) -> Int {
var result =1for i in2...n {
result *= i
}
return result
}
print(factorialIterative(5)) // 120// Using reducefuncfactorialReduce(_n: Int) -> Int {
return (1...n).reduce(1, *)
}
print(factorialReduce(5)) // 120
Quick answerCalculate Fibonacci using recursion, iteration, or memoization.
Recursive: fib(n-1) + fib(n-2)
Iterative: Loop with variables
Memoization: Cache results in dictionary
Complexity: O(n) with memoization
Swift
// Fibonacci in Swiftfuncfibonacci(_n: Int) -> Int {
if n <=1 {
return n
}
return fibonacci(n -1) + fibonacci(n -2)
}
print(fibonacci(8)) // 21// Iterative versionfuncfibonacciIterative(_n: Int) -> Int {
if n <=1 {
return n
}
var a =0, b =1for_in2...n {
let temp = a + b
a = b
b = temp
}
return b
}
print(fibonacciIterative(8)) // 21// Memoized versionvar fibCache: [Int: Int] = [:]
funcfibonacciMemo(_n: Int) -> Int {
if n <=1 {
return n
}
iflet cached = fibCache[n] {
return cached
}
let result = fibonacciMemo(n -1) + fibonacciMemo(n -2)
fibCache[n] = result
return result
}
print(fibonacciMemo(8)) // 21
Quick answerFind missing number using formula or XOR method.
Formula: total - sum
XOR: XOR all numbers and indices
Complexity: O(n) time
Edge cases: Empty array, missing first or last
Swift
// Find missing number in SwiftfuncfindMissing(_arr: [Int]) -> Int {
let n = arr.count +1let total = n * (n +1) /2let sum = arr.reduce(0, +)
return total - sum
}
print(findMissing([1, 2, 4, 5, 6])) // 3// Using XORfuncfindMissingXOR(_arr: [Int]) -> Int {
let n = arr.count +1var xorSum =0for i in1...n {
xorSum ^= i
}
for num in arr {
xorSum ^= num
}
return xorSum
}
print(findMissingXOR([1, 2, 4, 5, 6])) // 3
Quick answerCalculate sum using reduce or manual iteration.
reduce: arr.reduce(0, +)
Manual: Iterate and accumulate
forEach: arr.forEach { total += $0 }
Complexity: O(n) time
Swift
// Sum of array in SwiftfuncsumArray(_arr: [Int]) -> Int {
return arr.reduce(0, +)
}
print(sumArray([1, 2, 3, 4, 5])) // 15// Manual implementationfuncsumArrayManual(_arr: [Int]) -> Int {
var total =0for num in arr {
total += num
}
return total
}
print(sumArrayManual([1, 2, 3, 4, 5])) // 15// Using forEachfuncsumArrayForEach(_arr: [Int]) -> Int {
var total =0
arr.forEach { total +=$0 }
return total
}
Swift is a modern programming language developed by Apple for building iOS, macOS, watchOS, and tvOS applications. It provides safety, performance, and developer-friendly features.
What are Optionals in Swift?
Optionals in Swift represent values that can either contain a value or be nil. They help prevent runtime crashes caused by accessing missing values.
What is ARC in Swift?
ARC (Automatic Reference Counting) is Swift's memory management system that automatically tracks and releases unused class instances to prevent memory leaks.
What are closures in Swift?
Closures are self-contained blocks of functionality in Swift that can be passed around and used in code. They are similar to anonymous functions or lambda expressions.
What are protocols in Swift?
Protocols define a blueprint of methods, properties, and requirements that classes, structures, and enumerations can adopt to provide specific functionality.
What is the difference between Struct and Class in Swift?
Structs are value types and are copied when assigned, while classes are reference types and share the same instance. Classes support inheritance, whereas structs do not.
What is SwiftUI?
SwiftUI is Apple's modern framework for creating user interfaces across Apple platforms using a declarative Swift syntax.
What is async/await in Swift?
Async/await in Swift simplifies asynchronous programming by allowing developers to write asynchronous code in a cleaner and more readable way.
What are extensions in Swift?
Extensions allow developers to add new functionality to existing classes, structures, enumerations, and protocols without modifying their original implementation.
How can I prepare for a Swift interview?
Prepare for Swift interviews by practicing Swift syntax, OOP concepts, Optionals, ARC, closures, protocols, SwiftUI, concurrency, memory management, and real-world iOS development scenarios.
AK
Written and reviewed by Abhishek Kumar
Founder, InterviewPitch · 10+ years in IT. Questions are collected from real interview rounds and refreshed regularly. Spotted an error? Tell us.