How would you demonstrate Copy-on-Write behavior with a custom struct?

Demonstrate Copy-on-Write with a value-typed linked list that shares storage on assignment and copies it only when a mutation occurs using `isKnownUniquelyReferenced`.

Answer

The core idea

Copy-on-Write (COW) allows a value type to share heap storage until a mutation would violate its value semantics. Assignment is cheap because the storage is shared. A copy is made only before mutation, and only if the storage is shared.

The short interview answer is: keep the backing storage in a private reference type, share that storage on assignment, and before mutating call isKnownUniquelyReferenced—if the storage is shared, copy it first.

1. A linked list with COW

List is the public value type. Storage owns the linked nodes. Before mutating, append ensures the storage is uniquely referenced.

swift 5.2
final class ListNode {
    var value: Int
    var next: ListNode?

    init(_ value: Int, next: ListNode? = nil) {
        self.value = value
        self.next = next
    }

    func deepCopy() -> ListNode {
        ListNode(value, next: next?.deepCopy())
    }
}

struct List {
    private final class Storage {
        var head: ListNode?
    }

    private var storage = Storage()

    mutating func append(_ value: Int) {
        copyStorageIfNeeded()

        let node = ListNode(value)
        guard let head = storage.head else {
            storage.head = node
            return
        }

        var current = head
        while let next = current.next {
            current = next
        }
        current.next = node
    }

    private mutating func copyStorageIfNeeded() {
        guard !isKnownUniquelyReferenced(&storage) else { return }

        let copy = Storage()
        copy.head = storage.head?.deepCopy()
        storage = copy
    }
}

var left = List()
left.append(1)
left.append(2)

var right = left // Shares storage
right.append(3)  // Copies, then mutates only `right`

After var right = left, both lists reference the same Storage instance. When right.append(3) is called, the node chain is deep-copied first, and only the copy is mutated. left remains 1 → 2, while right becomes 1 → 2 → 3.

2. Why it matters

Swift collections provide value semantics: after var right = left, mutating right must not affect left. That's the guarantee callers rely on, and the same behavior provided by Array, String, and Dictionary.

Without checking whether the storage is uniquely referenced, two values would continue sharing the same backing storage. Mutating one value would unexpectedly change the other, violating value semantics. Copy-on-Write preserves that guarantee while keeping assignments inexpensive.

Copy-on-Write is not a synchronization mechanism. isKnownUniquelyReferenced only determines whether the storage has a single strong owner at the moment of the check. It does not make concurrent mutation safe. Mutating the same value from multiple threads without synchronization is still a data race.

Interview angle

Lead with the rule: structs are expected to provide value semantics, and Copy-on-Write shares private reference storage until mutation. Show a linked-list example with a private Storage class, isKnownUniquelyReferenced before append, and a deep copy of the node chain when the storage is shared. Finish by mentioning isKnownUniquelyReferenced(_:)), the standard library API used to determine whether storage can be safely mutated in place.