A Simple Guide to Problem-Solving: Master Algorithms and Ace Coding Interviews

Rajesh Dhiman
5 min readSep 11, 2024

--

If you’re trying to crack coding interviews and ace your problem-solving skills, you’re definitely in the right place. Understanding algorithms and data structures is essential for excelling in coding interviews, especially when applying to major tech companies. This guide introduces some fundamental algorithmic techniques with simple examples and JavaScript solutions to help you grasp these concepts.

A Simple Guide to Problem-Solving

1. Binary Search: An Efficient Search Method

Binary Search is a classic algorithm used to efficiently find an item in a sorted array by repeatedly dividing the search range in half. Instead of checking every element, it narrows down the possible locations by comparing the target value to the middle element of the array.

Problem:

Given a sorted array [1, 3, 5, 7, 9, 11, 13, 15], find the index of the number 11 using binary search.

Solution (JavaScript):

function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid; // Target found
} else if (arr[mid] < target) {
left = mid + 1; // Search the right half
} else {
right = mid - 1; // Search the left half
}
}
return -1; // Target not found
}
console.log(binarySearch([1, 3, 5, 7, 9, 11, 13, 15], 11)); // Output: 5

Explanation:

  • Start in the middle of the array, check if the target is smaller or larger, and narrow the search area accordingly.
  • This algorithm has a time complexity of O(log n), making it much faster than linear search.

2. Sliding Window Technique: Optimizing Sequential Operations

The Sliding Window technique is used to optimize algorithms that require examining subarrays or substrings by maintaining a window of fixed size, which “slides” over the data.

Problem:

Find the sum of every subarray of size 3 in the array [1, 3, 2, 6, 2, 8, 1].

Solution (JavaScript):

function slidingWindowSum(arr, k) {
let result = [];
let windowSum = 0;
// Sum of the first window
for (let i = 0; i < k; i++) {
windowSum += arr[i];
}
result.push(windowSum);
// Slide the window over the array
for (let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
result.push(windowSum);
}
return result;
}
console.log(slidingWindowSum([1, 3, 2, 6, 2, 8, 1], 3)); // Output: [6, 11, 10, 16, 11]

Explanation:

  • The window starts at the first k elements, then shifts to the right while subtracting the leftmost element and adding the next element in the window.
  • This avoids recalculating the entire sum for each window, making the algorithm more efficient with a time complexity of O(n).

3. Hashing: Quick Lookups for Fast Access

Hashing allows for quick storage and retrieval of data by mapping values to a fixed-size table using a hash function. It’s useful for counting occurrences or checking if an element exists in constant time.

Problem:

Count the number of occurrences of each letter in the string "applebanana".

Solution (JavaScript):

function countLetterOccurrences(str) {
let letterCount = {};
for (let char of str) {
letterCount[char] = (letterCount[char] || 0) + 1;
}
return letterCount;
}
console.log(countLetterOccurrences("applebanana"));
// Output: { a: 4, p: 2, l: 1, e: 1, b: 1, n: 2 }

Explanation:

  • Each character is processed, and its count is updated in the hash table (or object in JavaScript). This operation runs in O(n) time, where n is the length of the string.

4. Two-Pointer Technique: Efficient Array Processing

The Two-Pointer technique uses two indices to traverse the array, either starting from both ends or from one end. This method is particularly effective for problems that involve comparing or merging two parts of an array.

Problem:

Reverse the array [1, 2, 3, 4, 5] in place.

Solution (JavaScript):

function reverseArray(arr) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
// Swap elements at left and right pointers
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr;
}
console.log(reverseArray([1, 2, 3, 4, 5])); // Output: [5, 4, 3, 2, 1]

Explanation:

  • Two pointers are used to swap elements from the start and end of the array until they meet in the middle.
  • The time complexity is O(n), where n is the number of elements in the array.

5. Prefix and Suffix Sums: Fast Range Queries

Prefix sums allow us to compute cumulative sums up to a certain index efficiently. It’s useful for solving range sum queries in constant time after the initial setup.

Problem:

Calculate the sum of elements from index 1 to 4 in the array [1, 2, 3, 4, 5].

Solution (JavaScript):

function prefixSum(arr) {
let prefix = [arr[0]];
// Build prefix sum array
for (let i = 1; i < arr.length; i++) {
prefix[i] = prefix[i - 1] + arr[i];
}
return prefix;
}
function rangeSum(prefix, start, end) {
return start > 0 ? prefix[end] - prefix[start - 1] : prefix[end];
}
let arr = [1, 2, 3, 4, 5];
let prefix = prefixSum(arr);
console.log(rangeSum(prefix, 1, 4)); // Output: 14

Explanation:

  • The prefix sum array is built by adding each element to the sum of the previous elements. To calculate the sum of a range, subtract the prefix sum of the starting index from the end index.
  • This allows us to answer range queries in O(1) time after preprocessing the array in O(n).

6. Knuth-Morris-Pratt (KMP) Algorithm: Efficient String Matching

The KMP algorithm allows for efficient pattern matching by avoiding unnecessary comparisons, which is helpful when searching for a substring within a larger string.

Problem:

Check if the substring "apple" exists in the string "bananaappleorange".

Solution (JavaScript):

function buildPrefixTable(pattern) {
let prefixTable = new Array(pattern.length).fill(0);
let j = 0;
for (let i = 1; i < pattern.length; i++) {
while (j > 0 && pattern[i] !== pattern[j]) {
j = prefixTable[j - 1];
}
if (pattern[i] === pattern[j]) {
j++;
}
prefixTable[i] = j;
}
return prefixTable;
}
function kmpSearch(text, pattern) {
let prefixTable = buildPrefixTable(pattern);
let j = 0;
for (let i = 0; i < text.length; i++) {
while (j > 0 && text[i] !== pattern[j]) {
j = prefixTable[j - 1];
}
if (text[i] === pattern[j]) {
j++;
}
if (j === pattern.length) {
return i - j + 1; // Match found
}
}
return -1; // No match found
}
console.log(kmpSearch("bananaappleorange", "apple")); // Output: 6

Explanation:

  • The KMP algorithm builds a prefix table to avoid rechecking characters that have already been matched.
  • It then uses this table to skip unnecessary comparisons during the search process, providing an efficient string matching solution in O(n + m) time, where n is the length of the text and m is the length of the pattern.

Mastering these algorithm techniques — like binary search, hashing, and the two-pointer trick — will not only help you crack coding interviews but also make you a better problem-solver. Keep practicing, and soon you’ll tackle problems like a pro!

--

--

Rajesh Dhiman
Rajesh Dhiman

Written by Rajesh Dhiman

Rajesh Dhiman is a full-stack developer, technical architect, and mentor with 12+ years of experience. https://www.rajeshdhiman.in/

No responses yet