Chapter 1: DSA Arrays
DSA Arrays – Let’s understand this topic like a real teacher sitting next to you, explaining slowly, with lots of examples, visuals in words, and real coding feel.
What is an Array in DSA?
An array is the simplest and most fundamental data structure.
It is:
- A collection of items
- All items are of the same type (in most strict languages like C++, Java)
- Items are stored in contiguous memory locations (one after another)
- Each item can be accessed very quickly using an index (position number)
Think of it like this real-life analogy:
Imagine a row of 10 lockers in your school corridor:
- Locker 1, Locker 2, Locker 3, …, Locker 10
- Each locker can hold one thing (one book, one tiffin, etc.)
- You can instantly open Locker 5 if you know the number
- The lockers are fixed next to each other (contiguous)
This is exactly how an array works in computer memory.
Key Characteristics of Arrays
| Property | Meaning / Explanation | Example |
|---|---|---|
| Fixed size | In most languages (C, C++, Java) you decide size at creation and cannot change it | int arr[10]; |
| 0-based indexing | First element is at index 0, second at 1, last at (size-1) | arr[0], arr[1]… |
| Contiguous memory | All elements are stored one after another — no gaps | Fast access |
| Random access | You can jump to any position instantly using index | O(1) time |
| Homogeneous | All elements usually same data type (in strict languages) | All integers or all strings |
| Dynamic arrays | In Python (list), JavaScript (array), Java (ArrayList) — size can grow automatically | Very flexible |
Visual Representation of an Array
|
0 1 2 3 4 5 6 7 8 |
Index: 0 1 2 3 4 5 6 Value: 45 12 78 23 56 89 34 Memory: [45][12][78][23][56][89][34] ← each box takes same space |
Most Common Operations on Arrays
| Operation | Time Complexity | How it works / Example |
|---|---|---|
| Access (get value) | O(1) | arr[4] → instantly gives 56 |
| Update (change value) | O(1) | arr[2] = 99 |
| Traversal (visit all) | O(n) | for loop over all elements |
| Insertion at end (if space) | O(1) | |
| Insertion at beginning / middle | O(n) | Have to shift all elements |
| Deletion at beginning / middle | O(n) | Shift all remaining elements |
| Search (unsorted) | O(n) | Linear search |
| Search (sorted) | O(log n) | Binary search (very important) |
Real-life Examples where Arrays are used
| Real Situation | How Array is used |
|---|---|
| Your playlist of songs | Array of song names / file paths |
| Marks of 50 students in a class | Array of 50 integers |
| Pixels in one row of an image | Array of colors (RGB values) |
| Recent 10 messages in chat | Array (newest at end or beginning) |
| Top 100 leaderboard scores | Array of scores + names |
Very Simple Code Examples (Python – easiest to read)
1. Creating and accessing array (Python list is dynamic array)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Creating array (list in Python) numbers = [10, 25, 7, 83, 41, 19] # Accessing print(numbers[0]) # 10 print(numbers[3]) # 83 print(numbers[-1]) # 19 (last element) # Updating numbers[2] = 99 print(numbers) # [10, 25, 99, 83, 41, 19] |
2. Traversing (visiting every element)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
marks = [78, 92, 65, 88, 45, 71] total = 0 for mark in marks: total += mark print("Total marks =", total) print("Average =", total / len(marks)) |
3. Finding maximum element
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def find_max(arr): if not arr: return None max_value = arr[0] for num in arr: if num > max_value: max_value = num return max_value scores = [34, 89, 12, 67, 95, 41] print(find_max(scores)) # 95 |
4. Very common interview question: Reverse an array
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def reverse_array(arr): left = 0 right = len(arr) - 1 while left < right: # swap arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1 return arr a = [1, 2, 3, 4, 5, 6] print(reverse_array(a)) # [6, 5, 4, 3, 2, 1] |
5. Another very common question: Move all zeros to end
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def move_zeros(arr): non_zero_index = 0 # Move all non-zero elements forward for i in range(len(arr)): if arr[i] != 0: arr[non_zero_index] = arr[i] non_zero_index += 1 # Fill remaining positions with 0 while non_zero_index < len(arr): arr[non_zero_index] = 0 non_zero_index += 1 return arr print(move_zeros([0, 1, 0, 3, 12, 0, 5])) # Output: [1, 3, 12, 5, 0, 0, 0] |
Important Variations of Arrays you should know
| Name / Type | Language | Can change size? | Example |
|---|---|---|---|
| Static Array | C, C++ | No | int arr[100]; |
| Dynamic Array | Python (list) | Yes | numbers = [1,2,3] |
| ArrayList | Java | Yes | ArrayList<Integer> list = new ArrayList<>(); |
| Vector | C++ | Yes | vector<int> v; |
| std::array | C++ (modern) | No (fixed) | array<int, 5> a; |
When should you use Arrays?
Use array when:
- You know (or can estimate) the number of elements
- You need fast access by position (index)
- You mostly do traversal, update, or end insertion
- You want simple & memory efficient structure
Don’t use array when:
- You need to insert/delete frequently in the beginning or middle
- Size changes a lot and unpredictably
- You need to search very frequently by value (not position) → better use HashMap
Quick Summary – Arrays in one view
| Feature | Rating / Note |
|---|---|
| Ease of understanding | ★★★★★ (easiest) |
| Speed of access by index | ★★★★★ (fastest) |
| Insert/delete at start/middle | ★☆☆ (slow) |
| Insert at end (amortized) | ★★★★☆ |
| Memory usage | Very good |
| Most common in interviews | Extremely common (almost 30–40% questions) |
Arrays are the foundation of almost every other data structure.
Next topics usually build on arrays:
- Two Pointers on arrays
- Sliding Window
- Prefix Sum
- Kadane’s Algorithm
- Sorting algorithms
- Binary Search on arrays
- Rotate array
- Merge intervals
- Stock buy-sell problems
Would you like me to explain any of these array-based patterns next in the same detailed style? Examples:
- Two pointers technique with 4–5 problems
- Sliding window pattern
- How to solve “Maximum Subarray Sum” (Kadane)
- Prefix Sum technique with examples
- Common interview questions on arrays
Just tell me the next topic you want — I’ll teach it step-by-step like we’re doing a live session! 😄
