1. Arrays & Strings
Foundation data structures - contiguous memory and character sequences
Arrays & Strings
Data Structure Module - Learn how data is stored and organized
Arrays and Strings are the building blocks of programming. Every other data structure builds on these concepts!
What You'll Learn
By the end of this module, you will be able to:
- Understand how arrays store data in memory
- Perform common array operations (access, insert, delete)
- Work with 2D arrays (matrices)
- Manipulate strings efficiently
1. Arrays - The Foundation
What is an Array? (Simple Explanation)
Think of an array like a row of lockers in a school:
- Each locker has a number (index) - 0, 1, 2, 3...
- Each locker can hold one item (value)
- All lockers are side by side (contiguous memory)
- You can go directly to locker #5 without checking lockers 0-4
Real-world examples:
- Your contacts list on phone
- A row of seats in a movie theater
- Shopping cart items
Creating and Using Arrays
# Creating an array (list in Python)
fruits = ["apple", "banana", "cherry", "date"]
# index 0 index 1 index 2 index 3
# ACCESS: Get item at index 2 - Super fast! O(1)
print(fruits[2]) # Output: "cherry"
# MODIFY: Change item at index 1
fruits[1] = "blueberry"
print(fruits) # ["apple", "blueberry", "cherry", "date"]
# ADD: Append to end - Fast! O(1)
fruits.append("elderberry")
# INSERT: Add at specific position - Slow! O(n)
# Why slow? All elements after must shift right
fruits.insert(0, "avocado") # Add at beginning
# DELETE: Remove by value - Slow! O(n)
fruits.remove("cherry")
# LENGTH: How many items? - Fast! O(1)
print(len(fruits)) # 5
Why Some Operations Are Slow?
Inserting at the beginning (O(n)): Imagine inserting a person at the front of a queue - everyone must step back!
Before: [10, 20, 30, 40]
Insert 5 at position 0:
Step 1: [10, 20, 30, 40, _] โ Make space
Step 2: [_, 10, 20, 30, 40] โ Shift all right
Step 3: [5, 10, 20, 30, 40] โ Insert 5
Time Complexity Summary
| Operation | Time | Why? |
|---|---|---|
| Access by index | O(1) | Direct jump to location |
| Search for value | O(n) | Must check each element |
| Add at end | O(1) | Just add after last item |
| Add at beginning | O(n) | Shift all elements right |
| Delete | O(n) | Shift all elements left |
2. 2D Arrays (Matrices)
A 2D array is like a table with rows and columns - think of Excel spreadsheet!
# Creating a 3x3 matrix
matrix = [
[1, 2, 3], # Row 0
[4, 5, 6], # Row 1
[7, 8, 9] # Row 2
]
# Col0 Col1 Col2
# Access element at row 1, column 2
print(matrix[1][2]) # Output: 6 (row 1, col 2)
# Traverse all elements (row by row)
for row in range(3):
for col in range(3):
print(matrix[row][col], end=" ")
print() # New line after each row
# Output:
# 1 2 3
# 4 5 6
# 7 8 9
Common uses:
- Game boards (chess, tic-tac-toe)
- Image pixels
- Maps and grids
3. Strings - Array of Characters
A string is essentially an array of characters!
# Strings in Python
name = "Hello World"
# Access character (just like array!)
print(name[0]) # 'H'
print(name[6]) # 'W'
# Get length
print(len(name)) # 11 (includes space)
# Slicing - Get part of string
print(name[0:5]) # "Hello"
print(name[6:]) # "World"
# IMPORTANT: Strings are IMMUTABLE in Python
# This means you can't modify them directly
# name[0] = 'h' โ This would cause ERROR!
# Instead, create a new string:
name_lower = name.lower() # "hello world"
Common String Operations
text = "hello world"
# Find position of substring
position = text.find("world") # Returns 6
# Replace text
new_text = text.replace("world", "python") # "hello python"
# Split into list
words = text.split(" ") # ["hello", "world"]
# Join list into string
joined = "-".join(words) # "hello-world"
# Check start/end
text.startswith("hello") # True
text.endswith("world") # True
Key Takeaways
| Concept | Array | String |
|---|---|---|
| Access | O(1) - Instant | O(1) - Instant |
| Modify | O(1) - Can change | O(n) - Create new |
| Insert | O(n) - Shift needed | O(n) - Create new |
| Search | O(n) - Check each | O(n) - Check each |
Remember:
- Arrays = Mutable (can change)
- Strings = Immutable (cannot change in Python)
Common Interview Patterns
- Two Pointers - Use left and right pointers
- Sliding Window - Move a window through array
- Frequency Count - Count occurrences using dict
- Reverse - Swap from both ends
Test your understanding
Practice Quiz
12 questions ยท 90s per question
Each question has a 90-second time limit. Unanswered questions will be auto-submitted when time runs out.