Interview Qs Decoded - # 1

Cat - Jun 2 '20 - - Dev Community

Hello everyone! Welcome to the first in a series! I'm going to try to explain a common software engineering interview question to better understand it and hopefully remember it when the time comes!

These problems will primarily be solved in JavaScript, as that is my language of choice when testing (and I just want to become a good front-end dev. 🥺)


Q: Find the second largest number in a given array.

Params: We are given an array of whole, positive integers (no negative numbers or floats). We are to write a function and return the second largest integer.

Let's start!

We'll write the skeleton of our function, setting the input/argument as "arr" for array:

function secondLargest(arr){};
Enter fullscreen mode Exit fullscreen mode

Then, we'll need to set two empty variables: largest and second.

Why? We will need placeholders for both our prospective largest and second largest numbers as we loop through our array.
We want to keep track of each integer that is in the array and measure the value against the others

function secondLargest(arr){ 
    let largest = '';
    let second = '';
}
Enter fullscreen mode Exit fullscreen mode

...Which brings us to our next step: create a for-loop!
As we iterate through the array, we will measure each value against each other, comparing the variable "largest" to the current iteration value (arr[i]).

function secondLargest(arr){
    let largest = '';
    let second = '';
    //
    for(let i=0; i < arr.length; i++){};
    //
};
Enter fullscreen mode Exit fullscreen mode

To compare, we will create an if-statement comparing largest to arr[i].

If arr[i] is greater than largest, then replace it with the current value of arr[i] by redeclaring largest and setting it equal to arr[i]

function secondLargest(arr){
    let largest = '';
    let second = '';
    for(let i=0; i < arr.length; i++){
        //
        if(arr[i] > largest){
           largest = arr[i]
        };
       //
    };
};
Enter fullscreen mode Exit fullscreen mode

We found the largest number! But how do we get the second largest?
We did find it already (kind of): we'll just set the former "largest" number to the "second" variable.

HOWEVER, we must declare the second variable BEFORE we declare the new largest number, simply because order matters-- JavaScript executes code from the top-down.

function secondLargest(arr){
    let largest = '';
    let second = '';
    for(let i=0; i < arr.length; i++){
        if(arr[i] > largest){
           //
           second = largest;
           //
           largest = arr[i];
        }; 
    };
};
Enter fullscreen mode Exit fullscreen mode

Speaking of order and specificity, it's time we find the "true" second-largest number in the array.

Let's create another if-statement with more specific parameters:

If the arr[i] is GREATER THAN second AND LESS THAN largest, then set arr[i] as second

function secondLargest(arr){
    let largest = '';
    let second = '';
    for(let i=0; i < arr.length; i++){
        if(arr[i] > largest){
           second = largest;
           largest = arr[i];
        };
        //
        if(arr[i] > second && arr[i]< largest){
           second = arr[i];
        };
        // 
    };
};
Enter fullscreen mode Exit fullscreen mode

Finally, we'll return our second variable to complete the requirement.

function secondLargest(arr){
    let largest = '';
    let second = '';
    for(let i=0; i < arr.length; i++){
        if(arr[i] > largest){
           second = largest;
           largest = arr[i];
        };
        if(arr[i] > second && arr[i]< largest){
           second = arr[i];
        };
    };
    //
    return second;
    //
};
Enter fullscreen mode Exit fullscreen mode

And there you have it! It's a fairly simple solution, a bit long since we're using a traditional for-loop, but it works!

Feel free to post your own solutions in your programming language of choice in the comments!


Thanks for reading!

If you'd like to keep in touch, please reach out to me on Twitter!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .