JavaScript Interview Questions: Part 1 — Arrays

JavaScript Interview Questions: Part 1 — Arrays

Buy Me a Coffee☕ ️It will help me to make my content more informative.

While preparing for an interview one of the most important things to learn is to solve problems. You might impress the interviewer by giving theory-related answers but if you lack problem-solving skills it's a red flag. In this blog, I will discuss 4 questions with their solution related to Array Datastructure in Javascript. These are the questions I usually ask when I take an interview to test a candidate's basic programming-solving skills.

Problem 1: Find the Maximum Number Without using .max in the built-in function

Given an array find the maximum value from that array without using .max js in-built function.

const numbers = [3, 7, 2, 8, 1];

Now there are several methods to solve this. but the most basic and easy one is to loop the array and check whether the current iteration is greater than the previous one or not.

function findMax(arr) {
  let max = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}

Problem 2: Reverse An Array

Given an array reverse it without using any in-built function like .reverse.

const originalArray = [1, 2, 3, 4, 5];

We can solve it by making an empty array and then looping the original array in reverse and then just pushing the current iteration into the new array.

function reverseArray(arr) {
  const reversedArr = [];
  for (let i = arr.length - 1; i >= 0; i--) {
    reversedArr.push(arr[i]);
  }
  return reversedArr;
}

Problem 3: Remove Duplicates Value From Array

Given an array remove duplicate value from it. Now this one is my favorite because you cannot use includes a built-in function which will make it a little.

const arrayWithDuplicates = [1, 2, 2, 3, 4, 4, 5];

Now first I will show you the code and then will try to explain as much as possible

// Function to check if an element already exists in an array
function hasDuplicate(arr, element) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === element) {
      return true;
    }
  }
  return false;
}

// Function to remove duplicates from an array
function removeDuplicates(arr) {
  const uniqueArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (!hasDuplicate(uniqueArr, arr[i])) {
      uniqueArr.push(arr[i]);
    }
  }
  return uniqueArr;
}

Now the above code is written in a little structured way which is the preferred way otherwise, you can write it in one block.

function removeDuplicates(arr) {
  const uniqueArr = [];
  for (let i = 0; i < arr.length; i++) {
    let isDuplicate = false;
    for (let j = 0; j < uniqueArr.length; j++) {
      if (arr[i] === uniqueArr[j]) {
        isDuplicate = true;
        break;
      }
    }
    if (!isDuplicate) {
      uniqueArr.push(arr[i]);
    }
  }
  return uniqueArr;
}

Now let's understand the code using the first example.

// Function to check if an element already exists in an array
function hasDuplicate(arr, element) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === element) {
      return true;
    }
  }
  return false;
}

The hasDuplicate function takes two parameters: an array arr and an element to be checked for duplication. It iterates through the array using a for loop and compares each element to the element parameter. If a match is found, the function returnsz, indicating that the element is already present in the array. If no match is found after iterating through the entire array, it returns false.

// Function to remove duplicates from an array
function removeDuplicates(arr) {
  const uniqueArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (!hasDuplicate(uniqueArr, arr[i])) {
      uniqueArr.push(arr[i]);
    }
  }
  return uniqueArr;
}

The removeDuplicates function is responsible for creating a new array uniqueArr and populating it with unique elements from the input array arr. It uses the hasDuplicate function to check whether an element is already in uniqueArr. If the element is not a duplicate, it's added to uniqueArr using .push().

Finally, the function returns uniqueArr, which contains the unique elements from the original array.

Problem 4:Find the Average of Array Elements

Given the below array, which calculates the average of all elements in an array of numbers.

const numbers = [3, 7, 2, 8, 1];

Now the approach will be the same as previously we will iterate over the array and then sum all the array elements but now we will return the average by dividing the sum by the length of the array.

function calculateAverage(arr) {
  if (arr.length === 0) {
    return 0;
  }

  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
  return sum / arr.length;
}

Conclusion

I hope the previous question has helped you grasp the basics of arrays. We’ll see you in the next blog with some new content. Until then, goodbye! If you have any queries, you can reach me on LinkedIn, and you can also follow my YouTube channel. I might not be consistent on YouTube, but you will still find helpful content.

Did you find this article valuable?

Support Muhammad Younus by becoming a sponsor. Any amount is appreciated!