Skip to main content

What is an array as a data structure?

An array is a basic data structure in which elements are stored in contiguous memory cells and have the same type (for example, all numbers or all strings).

The main property of an array is direct access by index, meaning you can instantly get the element with the needed number.


1. How it works

Picture a row of memory cells, where each element sits strictly one after another:

javascript
[10] [20] [30] [40] 0 1 2 3 ← indices

To get the element at index 2, the computer simply calculates the address: start_address + (element_size × index) - and immediately accesses the needed cell. That's why accessing an element takes O(1) - constant time.


2. Main operations

OperationTime complexityDescription
Access by indexO(1)Fast - direct addressing.
Search for an elementO(n)Requires scanning the whole array.
Insert/deleteO(n)Requires shifting the rest of the elements.
Iterate over all elementsO(n)Linear time.

3. Pros

  • Fast access by index.
  • Simple structure and implementation.
  • Efficient memory use (cells go one after another).

4. Cons

  • Fixed size (in classic arrays).
  • Slow inserts and deletes in the middle.
  • Memory is not freed automatically when elements are removed.

5. Example in Python

python
arr = [10, 20, 30, 40] print(arr[2]) # 30

(Although in Python this is a list, under the hood it is implemented as a dynamic array.)


Summary:

An array is a data structure with fast index-based access and sequential storage of elements, optimal for cases where the amount of data is known in advance and insertions happen rarely.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.