Array Interview Question


A few weeks ago I had this interview question (via Google Hangouts). I thought I would share here what I did well as well as the mistakes I made. Hopefully others can learn from both :)

The challenge was to find the largest number and index in this array

[1,2,10,3,4]

I immediately panicked because I didn't even know there was supposed to be a coding problem. I also started mumbling about being rusty with my data structures and algorithms. I wasted 2 minutes by starting to code a bubble sort and its nested loop.

I finally caught myself and asked, "Wait, do I need to sort this array? What is the exact output?"

The interviewer typed out Largest number: 10, Index: 2

The reason I wasted 2 minutes was because I made the mistake of not clarifying the question at the very beginning.

I had forgotten what I was taught by a college professor about solving these types of problems. He had taught me to draw a table with the headers Input and Output, and then write out what the problem is specifically asking for before even starting to solve the challenge. In this case it would have been...

Input Output
[1,2,10,3,4] Largest number: 10, Index: 2

The great news is that after I knew the exact problem, I was able to quickly solve the problem in 2 minutes. Check out working the code below:

  //array of numbers, find the largest number,
  //find the largest number's index
  var nums = [1,2,10,3,4];

  //set largest index to the first array item
  var largestIndex=0;

  //set largest number to the first array item
  var largestNum=nums[0];

  //loop through every element in the array except the first element
  for(var i=1;i< nums.length;i++){

    //if the current element is larger than the largest number
    if(nums[i]>largestNum){

      //set this current number to be the largest number
      largestNum=nums[i];

      //set the current index to be the largest index
      largestIndex=i;
    }
  }

  //print out results
  console.log("The largest number is:" + largestNum);
  console.log("The largest number's index is:" + largestIndex);

My work was not completely finished, however, because I was given an additional challenge. The previous array was modified into:

[1,2,10,3,10]

The additional challenge was to find the indices of ALL of the largest numbers.

Input Output
[1,2,10,3,10] Largest number: 10, Indices: 2, 4

Below is the solution I came up with:

  //array of numbers, find the largest number,
  //find the largest number's indices
  var nums = [1,2,10,3,10];

  //set largest number to the first array item
  var largestNum=nums[0];

  //loop through every element in the array except the first element
  for(var i=1;i< nums.length;i++){

    //if the current element is larger than the largest number
    if(nums[i]>largestNum){

      //set this current number to be the largest number
      largestNum=nums[i];
    }
  }

  //print out result
  console.log("The largest number is:" + largestNum);


  //create array to store largest number's indices
  var indicesOfLargestNumbers=[];

  //loop through every element in the array
  for(var i=0;i< nums.length;i++){

    //if the current element equal to the largest number
    if(nums[i]===largestNum){

      //push the index to the array
      indicesOfLargestNumbers.push(i);
    }
  }

  //display the result
  console.log("The largest number's indices are:" + indicesOfLargestNumbers);

I then had to do explain my code and the logic and reasoning behind it.

The biggest lesson I gained was that there are pros and cons to almost every coding decision. The main point is to have well-thought out and solid reasoning. For instance the code I wrote above is not the most efficient because of the two loops (O^2), but it is very easy for others to read and understand. The goal is to be able to explain and defend the code I wrote.

The greatest lesson I have learned so far is that doing these type of coding challenges can be fun and interesting. Furthermore, by solving problems at Code Wars I have learned many features and functions JavaScript. I will keep solving problems at Code Wars and maybe one day become a true Code Warrior...