Interview Qs Decoded - # 1
Tags: codenewbie, beginners, career, javascript
Published: 2020-06-02 20:30:22

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*:

```javascript
function secondLargest(arr){};
```
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 

```javascript
function secondLargest(arr){ 
    let largest = '';
    let second = '';
}
```

...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]).

```javascript
function secondLargest(arr){
    let largest = '';
    let second = '';
    //
    for(let i=0; i < arr.length; i++){};
    //
};
```
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]**

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

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.

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

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*

```javascript
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];
        };
        // 
    };
};
```

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

```javascript
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;
    //
};
```
---

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](https://twitter.com/catcarbn)!