The All-Powerful Array: Mastering the Foundation of Data Structures & Algorithms

Jackson Beytebiere
4 min readSep 12, 2023

--

Picture an unbroken chain of pearls, each exuding its unique luster. This string of pearls is a tangible analogy for an array — a foundational data structure that forms the backbone of programming. Intrigued? Let’s delve deeper into this bedrock of computer science to equip you with the techniques that turn array-related problems into child’s play.

In this article:
Core Concepts — Introduces the fundamentals of arrays, explaining their structure and function.
Techniques to Master — More advanced methods for tackling array-related problems.
Quiz — A practical application of the concepts and techniques discussed

Core Concepts

Contiguous Memory Allocation

Just as each pearl on the string is located next to its immediate neighbor, elements in an array are stored in contiguous blocks of memory. This contiguous memory allocation is critical for quick data retrieval. When you request the nth pearl from the chain, your hand naturally knows where to reach. Similarly, the computer knows precisely where to look in memory to retrieve an element of an array, making data access incredibly efficient.

Indexing

Moreover, consider each pearl as marked with a unique number starting from zero. That number is akin to the array’s index, a unique identifier for each element. This concept of indexing is not just a convenience but a boon for operations like search, sort, and iteration. Indexing accelerates these operations, as knowing the index is like knowing the exact location of each pearl on our imaginary string.

Techniques to Master

Armed with the basics of arrays — our foundational string of pearls — we’re ready to explore specialized techniques. These include the Two-Pointer Technique, Sliding Window, and Divide and Conquer, which offer tailored solutions for more intricate designs. We’ll also look at when a straightforward approach is the best way to maintain the string’s integrity.

Two-Pointer Technique:
Ideal for problems focusing on pairs or sets of numbers, the Two-Pointer Technique speeds up the search process significantly. One pointer starts at the beginning, the other at the end, and they work their way toward each other. This approach narrows down your search field, making the algorithm incredibly efficient.

image from: https://afteracademy.com/blog/what-is-the-two-pointer-technique/

Identification Question: Does the problem involve searching for pairs or sets of numbers?

function findPairWithTargetSum(arr, target) {
let left = 0;
let right = arr.length - 1;


while (left < right) {
const sum = arr[left] + arr[right];


if (sum === target) {
return [arr[left], arr[right]];
} else if (sum < target) {
left++;
} else {
right--;
}
}
return null;
}


const result = findPairWithTargetSum([1, 2, 4, 5, 6], 8);
console.log(result); // Output: [2, 6]

Sliding Window:
When the task involves contiguous subarrays, the Sliding Window technique saves the day. This method uses a ‘window’ of fixed size that slides over the array, avoiding redundant calculations and providing answers in a single pass.

image from: https://apdoelsaed.hashnode.dev/sliding-window-technique-in-javascript

Identification Question: Does the problem require working with contiguous subarrays?

function findMaxSubarraySum(arr, k) {
let maxSum = 0;
let windowSum = 0;
let windowStart = 0;


for (let windowEnd = 0; windowEnd < arr.length; windowEnd++) {
windowSum += arr[windowEnd];

if (windowEnd >= k - 1) {
maxSum = Math.max(maxSum, windowSum);
windowSum -= arr[windowStart];
windowStart++;
}
}
return maxSum;
}


const result = findMaxSubarraySum([2, 3, 4, 1, 5], 3);
console.log(result); // Output: 10

Divide and Conquer:
If a problem appears overwhelmingly complex, breaking it into smaller, similar problems can be the key. This method is particularly useful for sorting arrays or finding medians.

Identification Question: Can the problem be fragmented into smaller, similar tasks?

 function mergeSort(arr) {
if (arr.length <= 1) return arr;

const mid = Math.floor(arr.length / 2);
const left = arr.slice(0, mid);
const right = arr.slice(mid);

return merge(mergeSort(left), mergeSort(right));
}

function merge(left, right) {
let result = [], i = 0, j = 0;

while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
}
}

return result.concat(left.slice(i)).concat(right.slice(j));
}

const result = mergeSort([4, 1, 3, 9, 7]);
console.log(result); // Output: [1, 3, 4, 7, 9]

The Straightforward Technique:

Some problems are best tackled without any specific techniques. A keen analytical mind can often produce a simple, elegant solution.

Blind’s Top 75 Leetcode Questions

Blind’s list stands as an essential resource for anyone preparing for tech interviews. This compilation of commonly asked questions serves as an excellent platform to apply the array manipulation techniques covered earlier. To further hone your skills, I’ve prepared a quiz that specifically targets these techniques. Be mindful that some questions are best tackled without specialized methods, requiring instead a straightforward approach for optimal solutions.

Conclusion

Understanding arrays is both a rite of passage and a joy for budding programmers. Armed with these techniques, you’re well on your way to turning programming problems into pearls of wisdom.

--

--

Jackson Beytebiere

I write about programming and specifically CSS | HTML | JavaScript | Ruby | React.js | Redux.