How arrays store data in memory and what each operation costs
An array keeps all its elements in one contiguous memory block of equal-sized items, so the address of any index can be computed directly. Because of this, reading or writing an element by index takes constant time, while searching an unsorted array takes linear time. Inserting or deleting in the middle is costly because later elements must shift to keep the block in order. Dynamic arrays make appends amortised constant time through spare capacity and occasional resizing. The article also notes that a few one-pass array patterns solve many common interview problems.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in