Programming foundations

Stage 4 · Lesson 2 of 10

Arrays and dynamic arrays

Data structures and Big-O

An stores items side by side in memory, in one contiguous block. That is why reading or writing any index is O(1): the address is just the start plus the index times the item size. The cost is that inserting or deleting in the middle shifts everything after it, which is O(n).

The list type you already use (Python list, JavaScript array, Java , C++ vector, C# List) is a dynamic array. When it is full, it grabs a bigger block of memory, typically 1.5 to 2 times larger, and copies everything across. That copy is rare enough that appending at the end is O(1) on average, which is called . Removing from the front, though, is O(n) every time, a common hidden cost.

A question to check yourself, an AI practice session and 1 cheat sheet entry come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • Step through linked lists, hash tables and sorts with your own input

  • Containers library: vector, list, unordered_map and deque, with complexity for each call

Also worth your time

Mark this stage as not done?