👋 Hi there, coding explorer!
Welcome back! This week, we’re diving into Arrays - one of the most fundamental yet powerful data structures in programming. Whether you’re prepping for coding interviews or building real-world apps, mastering arrays will give you a huge edge.
We’ll break everything into easy, bite-sized parts - from basic concepts to pro-level tricks.
Part 1: What is an Array?
An array is a collection of elements stored in contiguous memory and accessed using an index.
Think of it as a row of lockers: each locker holds a value, and you can find it by its number (index).
Example
nums = [10, 20, 30, 40]
print(nums[0]) # 10
let nums = [10, 20, 30, 40];
console.log(nums[0]); // 10
Key Array Terms
Term | Description | Example |
|---|---|---|
Index | Position of an element (starts from 0) | nums[2] → 30 |
Length | Number of elements | len(nums) / nums.length |
Element | Individual item in the array | 20 in [10,20,30] |
Contiguous Memory | All elements stored next to each other | Fast random access (O(1)) |
Part 2: Array Operations
1️⃣ Traversal
Iterating through every element.
nums = [1, 2, 3]
for n in nums:
print(n)
let nums = [1, 2, 3];
for (let n of nums) console.log(n);
2️⃣ Insertion
Add elements at a specific position.
nums.insert(1, 99) # insert 99 at index 1
print(nums)
nums.splice(1, 0, 99);
console.log(nums);
3️⃣ Deletion
Remove elements by index or value.
nums.remove(99) # by value
del nums[0] # by index
nums.splice(1, 1); // remove element at index 1
4️⃣ Searching
Find if an element exists.
if 20 in nums:
print("Found!")
if (nums.includes(20)) console.log("Found!");
5️⃣ Sorting
Arrange elements in order.
nums = [5, 1, 4]
nums.sort()
print(nums) # [1,4,5]
nums = [5, 1, 4];
nums.sort((a, b) => a - b);
console.log(nums); // [1,4,5]
Part 3: Common Interview Patterns
1️⃣ Find Maximum/Minimum Element
nums = [4, 7, 1, 9]
max_num = max(nums)
print(max_num) # 9
let nums = [4, 7, 1, 9];
console.log(Math.max(...nums)); // 9
2️⃣ Reverse an Array
nums = [1, 2, 3, 4]
print(nums[::-1]) # [4,3,2,1]
let nums = [1, 2, 3, 4];
console.log(nums.reverse());
3️⃣ Sum of All Elements
nums = [1, 2, 3]
print(sum(nums)) # 6
let nums = [1, 2, 3];
let total = nums.reduce((a, b) => a + b, 0);
console.log(total); // 6
4️⃣ Find Second Largest Number
nums = [10, 5, 8, 20]
nums.sort()
print(nums[-2]) # 10
let nums = [10, 5, 8, 20];
nums.sort((a,b)=>a-b);
console.log(nums[nums.length-2]); // 10
Part 4: Advanced Patterns
🔹 Two Pointers
Useful when searching pairs in sorted arrays.
Example: Find if two numbers sum to target.
nums = [1,2,4,7,10]
target = 9
left, right = 0, len(nums)-1
while left < right:
s = nums[left] + nums[right]
if s == target:
print(nums[left], nums[right])
break
elif s < target:
left += 1
else:
right -= 1
let nums = [1,2,4,7,10];
let target = 9;
let left = 0, right = nums.length-1;
while (left < right) {
let sum = nums[left] + nums[right];
if (sum === target) { console.log(nums[left], nums[right]); break; }
sum < target ? left++ : right--;
}
🔹 Sliding Window
Used for subarray problems like "maximum sum of K elements."
Example (sum of 3 consecutive numbers)
nums = [2,1,5,1,3,2]
k = 3
max_sum = 0
window_sum = sum(nums[:k])
for i in range(k, len(nums)):
window_sum += nums[i] - nums[i-k]
max_sum = max(max_sum, window_sum)
print(max_sum)
let nums = [2,1,5,1,3,2];
let k = 3;
let maxSum = 0, windowSum = 0;
for(let i=0;i<k;i++) windowSum+=nums[i];
maxSum=windowSum;
for(let i=k;i<nums.length;i++){
windowSum+=nums[i]-nums[i-k];
maxSum=Math.max(maxSum,windowSum);
}
console.log(maxSum);
🔹 Prefix Sum
Used for quick range sum queries.
nums = [1,2,3,4]
prefix = [0]
for n in nums:
prefix.append(prefix[-1] + n)
print(prefix) # [0,1,3,6,10]
let nums = [1, 2, 3, 4];
let prefix = [0];
for (let n of nums) {
prefix.push(prefix[prefix.length - 1] + n);
}
console.log(prefix); // [0, 1, 3, 6, 10]🔹 2D Arrays (Matrices)
matrix = [[1,2,3],[4,5,6],[7,8,9]]
for row in matrix:
print(row)
let matrix = [[1,2,3],[4,5,6],[7,8,9]];
matrix.forEach(row => console.log(row));
Part 5: Mini Challenges
1️⃣ Sum of Even Numbers
Write a function to sum only even numbers in an array.
2️⃣ Reverse an Array (without using reverse())
Use a loop and swap elements manually.
3️⃣ Find the Maximum Product of Two Elements.
4️⃣ Matrix Challenge: Print the diagonal elements of a 3×3 matrix.
Expert Challenge
Find the maximum sum of any subarray using Kadane’s Algorithm.
nums = [-2,1,-3,4,-1,2,1,-5,4]
max_sum = cur_sum = nums[0]
for n in nums[1:]:
cur_sum = max(n, cur_sum + n)
max_sum = max(max_sum, cur_sum)
print(max_sum) # 6
let nums = [-2,1,-3,4,-1,2,1,-5,4];
let maxSum = nums[0], curSum = nums[0];
for(let i=1;i<nums.length;i++){
curSum = Math.max(nums[i], curSum + nums[i]);
maxSum = Math.max(maxSum, curSum);
}
console.log(maxSum); // 6
AI Tip
Ask AI:
“Explain and generate examples of Two Pointer and Sliding Window techniques on arrays.”
Use it to generate variations and practice multiple array patterns quickly.
✅ Sign-off
That’s your Arrays Bite - Beginner to Expert! 🎉
You’ve now learned how arrays work, how to loop, modify, search, and optimize them using key patterns like two pointers and sliding windows.
Next time, we’ll dive into Strings - one of the most frequently tested interview topics!