Daily iOS
CS fundamentals and algorithms

Array insert cost

A live feed calls posts.insert(newPost, at: 0) every time a new post arrives. What goes wrong once 50,000 posts have piled up?

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

Inserting at the front is an O(n) shift, so at 50,000 posts every insert moves hundreds of kilobytes. If something else also references that array, a full COW copy lands on top.

Share a question