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 pany 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 pany 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 pany 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 e 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).
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 pany 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 pany 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 pany 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 e 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).
Share Improve this question asked Nov 7, 2017 at 9:29 libiklibik 23k10 gold badges51 silver badges92 bronze badges 5- 1 Probably belongs in the Computer Science Stack Exchange. – Brett DeWoody Commented Nov 7, 2017 at 9:34
- I know this will be primarily opinion based - but this is definitely not a hard question. Are your candidates aware that there will be programming questions? – gurvinder372 Commented Nov 7, 2017 at 9:34
- It is a very trivial question, indeed. – Adriano Commented Nov 7, 2017 at 9:40
- Perhaps more suitable for codegolf.meta.stackexchange. – smac89 Commented Nov 7, 2017 at 9:50
- I wish I'd be given this question. It is far from trivial, and in fact it is about fundamental algorithmic literacy. My answer would be a small intro on Lewis Carroll (who posed it, but solved incorrectly), followed by implementation along the lines of the Stepanov's binary counter. I don't think though that 20 minutes is enough. Stepanov gave 10 lectures on this problem. – user58697 Commented Nov 7, 2017 at 20:04
6 Answers
Reset to default 4It'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:
- How would you find the largest?
- 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
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 plete 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]));
In reality, developers will take the path of least resistance. That is, they will sort the array and take the second last element. Less code to write and less thinking involved. A win in my book.
So the question you have to consider is whether you want developers who can remember how to implement this algorithm correctly from scratch (possibly buggy), or the ones who can reuse already existing methods to find the solution in the least amount of time, with nearly negligible time penalty, AND mostly bug free.
I mean when was the last time you wanted to find the second largest element in an array of millions of elements? Most developers will probably just opt for the method I described initially (sorting and take second largest), and leave the optimisation to O(n) for another time if it is found that the code is the bottleneck to any performance issues.
const _arr = [1, 7, 8, 5, 4, 2];
function findSecondBiggest(arr) {
if(arr.length < 2) {
return undefined;
}
var first = arr[0];
var second = arr[1];
if(second > first) {
[first, second] = [second, first];
}
for(var i = 2; i < arr.length; i++) {
var tmp = arr[i];
if(tmp > first) {
[first, second] = [tmp, first];
} else if(tmp > second) {
[first, second] = [first, tmp];
}
}
return first != second ? second : undefined;
}
console.log(findSecondBiggest(_arr));
No, I wouldn't say this is an exactly hard question. Even though there are a few catches which may easily throw off someone under stress:
- Handling of bad input (not enough input elements)
- Special handling for the head of the input
- Swapping variables if you don't know ES6 syntax
- Mixing up index and value
So while not hard, there are a couple of caches which can get candidates stuck easily, or cause them to get confused.
const findSecondBiggest = arr => {
let max1 = Number.MIN_VALUE;
return arr.reduce((acc, x) => {
if(x > max1) {
acc = max1;
max1 = x;
} else if (x > acc && x < max1) {
acc = x;
}
return acc;
}, Number.MIN_VALUE);
}
var a = [1, 7, 8, 5, 4, 2];
for (var i = 0; i < a.length ; i++) {
var big = 0;
for (var j = 0; j < a.length ; j++) {
if (a[j] > a[i]) big++;
}
if (big == 1) {
return a[i];
}
}
and with predefined methods
a[a.sort().length-2]