Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview live-coding - Find Second Biggest number in array

Quick meta-info:

I was looking for really right place for this question, there is none on stackoverflow. Still answering this question requires programming experience.


I am senior software engineer and lead node.js developer in company with 250 employees, therefore I am doing technical interviews. I am here only for 2 months, so I do not have that much experience with it (previous company was much smaller, so hiring new backend guy was very rare).

The part of interview is live-coding, usually through skype. I think this is quite important, because some people are really not able to do almost anything.

But there is one thing that bugs me - a lot people failing on task "find second biggest number in array" which I think is very easy.

I usually write them this piece of code with only limitation - dont sort an array and dont use any special functions for arrays:

const _arr = [1, 7, 8, 5, 4, 2];

function findSecondBiggest(arr){
}

console.log(findSecondBiggest(_arr));

And task is to write code into function that finds second biggest number. I thought this would be just "check" question and would be "just do it" for most programmers.

Even the developers that look quite promising are often failing on that. I know that they are under pressure, but I am even trying to guide them, calm them and they have at least 20 minutes (often even 30 as I stay 10 minutes more on the call to give them chance to finish it). A lot of developers look very promising - they have good experience, knowledge, are able to evaluate why they are using this framework/technology... But are not able to do this one.

I have consulted it with Technical Lead and he says that if they are not able to do this, we should discontinue - as we are looking for the programmers and we are expecting to write code. Also the company is interested for mid to senior level developers.

Is this task really that hard? Or is it a good one that really shows if you can come up with at least simple algorithm and implement it?

I am also accepting the solution where you use two for-cycles when first one finds the biggest number, the second for-cycle finds the second biggest number as it is still O(n).

like image 812
libik Avatar asked Jan 30 '23 06:01

libik


2 Answers

It's not hard, and it makes you think. I would definitely use this kind of question in an interview. However, I would prepare some guiding questions:

  1. How would you find the largest?
  2. How do you know that a number is the 2nd? etc...

And here is my lovely solution (test cases stolen from @BrettDeWoody's answer). You can use the the standard way of finding the largest number, and then add in the lowest as well.

const _arr = [1, 7, 8, 5, 4, 2];

function findSecondBiggest(arr){
  const r = {};

  for(let i = 0; i < arr.length; i++) {
    if(r.first === undefined || arr[i] > r.first) {
      r.second = r.first;
      
      r.first = arr[i];
    } else if (arr[i] !== r.first && (r.second === undefined || arr[i] > r.second)) {
      r.second = arr[i];
    }
  }
    
  return r.second; // for arrays with length under 2, the answer would be undefined
}

console.log(findSecondBiggest([1, 7, 8, 5, 4, 2])); // 7
console.log(findSecondBiggest([1, 0, 0, 0, -1, -2])); // 0
console.log(findSecondBiggest([2, 2, 1, 0, -1, -2, 2])); // 1
console.log(findSecondBiggest([1, 1, 1, 1, 1])); // undefined
console.log(findSecondBiggest([0, 0, 0, 1])); // 0
console.log(findSecondBiggest([1, 2, 3, 4])); // 3
console.log(findSecondBiggest([Infinity, -Infinity])); // -Infinity
console.log(findSecondBiggest([-Infinity, Infinity])); // -Infinity
console.log(findSecondBiggest([1, 2, 3, 4, 5, Infinity])); // 5
console.log(findSecondBiggest([1])); // undefined
like image 102
Ori Drori Avatar answered Feb 03 '23 07:02

Ori Drori


Completely agree with smac89's sentiment. As a member of an engineering hiring team, and code mentor, I'm not a fan of interview questions with unrealistic constraints.

Instead of asking a question a candidate will never encounter outside a technical interview, pose a challenging question relevant to what the candidate will be working on, and allow the candidate to demonstrate their knowledge of the language and skills they'll be using.

As a great example, a friend recently shared a frontend interview question - build a simple app without any libraries, within a time constraint.

Using a provided API endpoint (which returned paginated results using a ?page=N query param), the app had to take a user's search, retrieve all the results (which might involve multiple pages of results), sort all the results alphabetically, and display all the results at once. This required knowledge (and more importantly, awareness) of fetch, Promise (and Promise.all), array methods, event listeners, and vanilla JavaScript DOM manipulation.

The candidate could consult any docs, and use any built-in methods as needed. The time constraint allowed enough to time to complete as long as the candidate was somewhat familiar with the built-in JS methods, Fetch API, Promises, etc.

IMO, a near-perfect interview question (for frontend devs at least).

My 2¢.

And here's my solution to the question which:

  • Uses ES5
  • Uses no array methods (other than Array.length)
  • Handles edge cases like a homogeneous array, array of length 1, etc

function findSecondBiggest(arr, startInd){
  let start = startInd || 0;
  let largest = arr[start];
  let secondLargest = null;
  const arrLength = arr.length;
  
  if (start === arrLength - start) {
    return null;
  }
    
  for (var i = start + 1; i < arrLength; i++) {
    largest = arr[i] > largest ? setSecond(largest) && arr[i] : largest;
  }
  
  function setSecond(num, check) {
    secondLargest = num;
    return true;
  }
  
  if (secondLargest === null) {
    arr[arrLength] = arr[0];
    return findSecondBiggest(arr, start + 1);
  }
  
  return secondLargest;
}

console.log(findSecondBiggest([1, 7, 8, 5, 4, 2]));
console.log(findSecondBiggest([1, 0, 0, 0, -1, -2]));
console.log(findSecondBiggest([2, 2, 1, 0, -1, -2, 2]));
console.log(findSecondBiggest([1, 1, 1, 1, 1]));
console.log(findSecondBiggest([0, 0, 0, 1]));
console.log(findSecondBiggest([1, 2, 3, 4]));
console.log(findSecondBiggest([Infinity, -Infinity]));
console.log(findSecondBiggest([-Infinity, Infinity]));
console.log(findSecondBiggest([1, 2, 3, 4, 5, Infinity]));
console.log(findSecondBiggest([1]));
like image 32
Brett DeWoody Avatar answered Feb 03 '23 07:02

Brett DeWoody