DevPrep

All topics › Algorithms & data structures

What is ContiguousArray and why use it?

Senior PRO

The gist

ContiguousArray<Element> is an array that always stores its elements in one contiguous block of memory. Its API matches Array (it is also a RandomAccessCollection, RangeReplaceableCollection, a value type with CoW).

The difference is the storage. On Apple platforms Array can be bridged to NSArray: if the array came from Objective-C (NSArray as [Model]), a Cocoa buffer sits underneath, and every index access goes through a "native or bridged buffer" check and an objectAtIndex: call. ContiguousArray doesn't bridge to NSArray, so that check doesn't exist.

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