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:
[10] [20] [30] [40]
0 1 2 3 ← indicesTo 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
| Operation | Time complexity | Description |
|---|---|---|
| Access by index | O(1) | Fast - direct addressing. |
| Search for an element | O(n) | Requires scanning the whole array. |
| Insert/delete | O(n) | Requires shifting the rest of the elements. |
| Iterate over all elements | O(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
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 readyA concise answer to help you respond confidently on this topic during an interview.