Swift Interview Questions and Answers

In this tutorial, you’ll work through a series of Swift-specific interview questions and answers. By Bill Morefield.

Leave a rating/review
Save for later
Share
Update note: Bill Morefield updated this article for XCode 10.2 and Swift 5. Antonio Bello wrote the original.

Swift is only four years old, but it’s already become the default language for iOS development. As Swift has evolved to version 5.0, it’s turned into a complex and powerful language embracing both object-oriented and functional paradigms. Each release brings more evolution and change.

But how well do you really know Swift? In this article you’ll find some sample Swift interview questions.

You can use these questions to interview candidates to test their Swift knowledge. Or you can test your own! If you don’t know an answer, don’t worry: Each question has a solution so you can learn.

You’ll find the questions separated into three levels:

  • Beginner: Suitable for a beginner to Swift. You’ve read a book or two on the subject and have worked with Swift in your own apps.
  • Intermediate: Suitable for someone with a strong interest in the language. You’ve been reading a lot about Swift and experimenting with it further.
  • Advanced: Suitable for the most experienced developers — the folks who enjoy thoroughly exploring the language and using cutting-edge techniques.

At each level, you’ll find two types of questions:

  • Written questions: Good for take-by-email programming tests, since these can involve writing code.
  • Verbal questions: Good to ask over the phone or in a face-to-face interview, as your prospect can answer them verbally.

While you work through these questions and answers, keep a playground open so that you can play with the code attached to the question before you answer. We have tested all of the answers against Xcode 10.2 and Swift 5.

Beginner Written Questions

Question #1

Consider the following:

struct Tutorial {
  var difficulty: Int = 1
}

var tutorial1 = Tutorial()
var tutorial2 = tutorial1
tutorial2.difficulty = 2

What are the values of tutorial1.difficulty and tutorial2.difficulty? Would this be any different if Tutorial was a class? Why or why not?

[spoiler title=”Answer”]

tutorial1.difficulty is 1, whereas tutorial2.difficulty is 2.

Structures in Swift are value types. You copy value types by value rather than reference. The following code creates a copy of tutorial1 and assigns it to tutorial2:

var tutorial2 = tutorial1

A change to tutorial2 is not reflected in tutorial1.

If Tutorial were a class, both tutorial1.difficulty and tutorial2.difficulty would be 2. Classes in Swift are reference types. When you change a property of tutorial1, you’ll see it reflected in tutorial2 and vice versa.

[/spoiler]

Question #2

You’ve declared view1 with var, and you’ve declared view2 with let. What’s the difference, and will the last line compile?

import UIKit

var view1 = UIView()
view1.alpha = 0.5

let view2 = UIView()
view2.alpha = 0.5 // Will this line compile?

[spoiler title=”Answer”]

Yes, the last line will compile. view1 is a variable, and you can reassign it to a new instance of UIView. With let, you can assign a value only once, so the following code would not compile:

view2 = view1 // Error: view2 is immutable

However, UIView is a class with reference semantics, so you can mutate the properties of view2 — which means that the last line will compile:

let view2 = UIView()
view2.alpha = 0.5 // Yes!

[/spoiler]

Question #3

This complicated code sorts an array of names alphabetically. Simplify the closure as much as you can.

var animals = ["fish", "cat", "chicken", "dog"]
animals.sort { (one: String, two: String) -> Bool in
    return one < two
}
print(animals)

[spoiler title="Answer"]

The type inference system automatically calculates both the type of the parameters in the closure and the return type, so you can get rid of them:

animals.sort { (one, two) in return one < two }

You can substitute the $i notation for the parameter names:

animals.sort { return $0 < $1 }

In single statement closures, you can omit the return keyword. The value of the last statement becomes the return value of the closure:

animals.sort { $0 < $1 }

Finally, since Swift knows that the elements of the array conform to Equatable, you can simply write:

animals.sort(by: <)

[/spoiler]

Question #4

This code creates two classes: Address and Person. It then creates two Person instances to represent Ray and Brian.

class Address {
  var fullAddress: String
  var city: String
  
  init(fullAddress: String, city: String) {
    self.fullAddress = fullAddress
    self.city = city
  }
}

class Person {
  var name: String
  var address: Address
  
  init(name: String, address: Address) {
    self.name = name
    self.address = address
  }
}

var headquarters = Address(fullAddress: "123 Tutorial Street", city: "Appletown")
var ray = Person(name: "Ray", address: headquarters)
var brian = Person(name: "Brian", address: headquarters)

Suppose Brian moves to the new building across the street; you'll want to update his record like this:

brian.address.fullAddress = "148 Tutorial Street"

This compiles and runs without error. If you check the address of Ray now, he's also moved to the new building.

print (ray.address.fullAddress)

What's going on here? How can you fix the problem?

[spoiler title="Answer"]

Address is a class and has reference semantics so headquarters is the same instance, whether you access it via ray or brian. Changing the address of headquarters will change it for both. Can you imagine what would happen if Brian got Ray's mail or vice versa? :]

The solution is to create a new Address to assign to Brian, or to declare Address as a struct instead of a class.

[/spoiler]

You're good, but you can't claim Jedi status yet. How will you do with more open-ended questions on theory and practice?

To answer some of these questions, you might need to play with the code in a playground.

Beginner Verbal Questions

Question #1

What is an optional and which problem do optionals solve?

[spoiler title="Answer"]

An optional lets a variable of any type represent a lack of value. In Objective-C, the absence of value is available only in reference types using the nil special value. Value types, such as int or float, do not have this ability.

Swift extends the lack of value concept to both reference and value types with optionals. An optional variable can hold either a value or nil, indicating a lack of value.

[/spoiler]