DevPrep

All topics › What does it print? Interview puzzles

What does this array-of-classes code print? Copy-on-Write

Middle PRO

Task

class Human {
    var name: String
    init(name: String) { self.name = name }
}

let man = Human(name: "Artem")
let woman = Human(name: "Dasha")
let family = [man, woman]
let anotherFamily = family
anotherFamily.last?.name = "Vika"
print(family.last?.name)

What prints? How would you make it print Dasha? What is copy-on-write and which types have it? Does CoW work for every struct?

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store