DevPrep

All topics › What does it print? Interview puzzles

Copy-on-write: what is a1.count after append?

Middle PRO

Task

var a1 = [1, 2, 3, 4, 5]
var a2 = a1

a2.append(6)
let x = a1.count

What is x? When did the copy happen and what did it cost?

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