Introduction to Arrays - aloalgo.com

Introduction to Arrays

An array is the most fundamental data structure in programming. It stores elements in contiguous memory locations, allowing direct access to any element using its index. Arrays are the building block for many other data structures and algorithms.

What is an Array?

An array is a collection of elements stored at contiguous memory locations. Each element can be accessed directly using its index, starting from 0.

Basic Array Operations

Accessing Elements

Array access is O(1) because elements are stored contiguously. The memory address of any element can be calculated directly from the base address and index.

Modifying Elements

Modifying an element at a known index is also O(1).

Adding Elements

Adding to the end is O(1) amortized, but inserting at a specific position requires shifting elements, making it O(n).

Removing Elements

Like insertion, removal from the end is O(1), but removal from elsewhere is O(n) due to shifting.

Iterating Over Arrays

Slicing Arrays

Python provides powerful slicing syntax to extract portions of arrays.

Common Array Patterns

Finding Min/Max

Summing and Counting

Searching

Time Complexity Reference

OperationTime ComplexityNotes
Access by indexO(1)arr[i]
Modify by indexO(1)arr[i] = x
Append to endO(1)*Amortized
Pop from endO(1)arr.pop()
Insert at indexO(n)Shifts elements
Delete at indexO(n)Shifts elements
Search by valueO(n)Linear search
LengthO(1)len(arr)
*Amortized O(1) means O(1) on average, though occasionally O(n) when the array needs to resize.

Common Interview Tips

  1. Consider edge cases: Empty array, single element, duplicates, negative numbers.
  2. Watch for off-by-one errors: Remember arrays are 0-indexed and slicing end indices are exclusive.
  3. Think about in-place vs. extra space: Many problems can be solved both ways with different trade-offs.
  4. Know your time complexity: Nested loops are O(n²), sorting is O(n log n), single pass is O(n).
Was this helpful?
© 2026 aloalgo.com. All rights reserved.