Daily iOS
Swift

Implementing copy-on-write

You need a struct ImageBuffer that wraps a large pixel buffer, keeps value semantics, and avoids needless copies. If you implemented copy-on-write yourself, what would the code skeleton and the key API be?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Keep the storage in a final class that the struct references, and right before a mutation check isKnownUniquelyReferenced; if you're not the sole owner, clone first. The standard collections work this way.

Share a question