The len variable is equal to 5, meaning that array1 has 5 elements, whereas array2 has 6 elements:

array1 = [1, 2, 3, 4, 5]
array2 = [1, 2, 3, 4, 5, 6]

When array1 is assigned to array2, a copy of array1 is actually created and assigned.

The reason is that swift arrays are value types (implemented as structs) and not reference types (i.e. classes). When a value type is assigned to a variable, passed as argument to a function or method, or otherwise moved around, a copy of it is actually created and assigned or passed. Note that swift dictionaries are also value types, implemented as structs.

The second line uses the stringForKey method of NSUserDefaults, which returns an optional, to account for the key not being found, or for the corresponding value not being convertible to a string.

During its execution, if the key is found and the corresponding value is a string, the above code works correctly. But if the key doesn’t exist, or the corresponding value is not a string, the app crashes with the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

The reason is that the forced unwrapping operator ! is attempting to force unwrap a value from a nil optional. The forced unwrapping operator should be used only when an optional is known to contain a non-nil value.

The solution consists of making sure that the optional is not nil before force-unwrapping it:

The String struct doesn’t provide a count or length property or method to count the number of characters it contains. Instead a global countElements<T>() function is available.
When applied to strings, what’s the complexity of the countElements function:

Swift strings support extended grapheme clusters. Each character stored in a string is a sequence of one or more unicode scalars that, when combined, produce a single human readable character. Since different characters can require different amounts of memory, and considering that an extreme grapheme cluster must be accessed sequentially in order to determine which character it represents, it’s not possible to know the number of characters contained in a string upfront, without traversing the entire string. For that reason, the complexity of the countElements function is O(n).

In Swift enumerations, what’s the difference between raw values and associated values?

Raw values are used to associate constant (literal) values to enum cases.
The value type is part of the enum type, and each enum case must specify a unique raw value (duplicate values are not allowed).

The following example shows an enum with raw values of type Int:

enum IntEnum : Int {
case ONE = 1
case TWO = 2
case THREE = 3
}

An enum value can be converted to its raw value by using the rawValue property:

Whereas the type(s) associated to a case are part of the enum declaration, the associated value(s) are instance specific, meaning that an enum case can have different associated values for different enum instances.

Swift defines the AnyObject type alias to represent instances of any reference type, and it’s internally defined as a protocol.

Consider the following code:

var array = [AnyObject]()
struct Test {}
array.append(Test())

This code generates a compilation error, with the following error message:

Type 'Test' does not conform to protocol 'AnyObject'

The failure is obvious because a struct is a value and not a reference type, and as such it doesn’t implement and cannot be cast to the AnyObject protocol.

The array array is filled in with values of type respectively int, double, string, array and dictionary. All of them are value types and not reference types, and in all cases no error is reported by the compiler. Why?

result1 is an array of strings, containing the list of the planet names
result2 is a double, calculated as the sum of the distance of all planets

The map method of the Array<T> struct type performs a transformation of the source array into an array of another type, whose values are obtained by executing the closure passed as parameter to each element of the array.
In the above code, the closure returns the name property, so the map method in the above code returns an array of planet names.

Given an initial value and a closure, the reduce method of the Array<T> struct type returns a single value obtained by recursively applying the closure to each element of the array. The closure takes the value calculated at the previous step (or the initial value if it’s the first iteration) and the current array element, and is expected to return a value of the same type of the initial value.

In the above code, the closure returns the sum of what calculated at the previous step, plus the value of the distanceFromSun property for the current element. The end result is the sum of the distances of all planets.

There is a strong reference cycle between Master and Detail, with Master creating an instance of Detail and storing a reference to it, and Detail storing a reference to the instance of its Master creator. In both cases, references are strong, which means that none of the 2 instances will ever be deallocated, causing a memory leak.

To solve the problem it’s necessary to break at least one of the 2 strong relationships, by using either the weak or unowned modifier.
The differences between the 2 modifiers is:

unowned: the reference is assumed to always have a value during its lifetime - as a consequence, the property must be of non-optional type.

weak: at some point it’s possible for the reference to have no value - as a consequence, the property must be of optional type.

In the above code example, the proper solution is to define in Detail the reference to Master as unowned:

x is of type [String] and of value ["john", "james", "louis", "vincent"].

* There is more to interviewing than tricky technical questions, so these are intended merely as a guide. Not every “A” candidate worth hiring will be able to answer them all, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.

Submit an interview question

Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

Our editorial staff will review it shortly. Please note that submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

Paul has been a professional software developer for over 25 years—predominantly as a freelancer on a wide range of projects with a focus on iOS and macOS app development. He is an American currently living in Thailand. Paul is diligent, communicates well, and loves solving hard problems.

Sergey is a senior iOS developer with more than four years of software development experience writing highly readable, clean, maintainable source code. He has worked with various technology stacks: REST and socket APIs, social media integration, geolocation, audio/image/video processing, complex UI, and animation customizations.

Cristian-Leonard is passionate about both the IT field and the visual arts. He has written several apps that mixes these passions—the best example would be miniDraw 2—an IPad and iPhone vector illustration app which can be found at the App Store. Currently he owns a small software company and is now in search of new projects and challenges.