Coding interview Preparation: Find The Second Largest Number

You are given an array or Python list of integers : 1,3,4,5,0,2

We are expected to write a function second_largest(sample_list), which takes the list as input and returns the second largest number.

Rule number one, before we jump to coding out the solution, which will be wrong as it is based on assumptions.

We need to ask the interviewer some questions and not assume we understand the problem fully.

Remember you are rated on 4 points :

1) Data Structures and Algorithms 2)Coding Skills

3) Communication skills

4)Problem Solving skills

· Our first question should be, could the array or Python list be empty?

· If the interviewer answers yes, second largest(a) should return None in this matter, we already know what is expected of us.

· Note that None is used to define no value at all, many mistake it as 0, false or an empty sting.

· Second question is could the array/list have only one element inside it?

· The interviewer answers yes, second_largest (a) should return None as well, we are on the same page with the interviewer.

So we are in the clear and it’s time to solve the problem talking and showing the interviewer what we are doing.

You tell the interviewer, that you are going to loop through each element in the list.

As you iterate through the list you are keeping track of the largest number you come across.

Step1: Declare our variables Largest and second largest

We initialise them to None at the start.

Step2: We Iterate Through The loop, still explaining to our interviewer what we are doing.

· We look at the first element, the largest becomes 1 and second largest still remains None

· Largest = 1

· Second_largest = None

· We then move to our second element the largest number is 2 and the second largest becomes 1.

· Largest = 2

· Second_largest = 1

· We iterate until the last element and our largest is 5 with the second largest as 4

· Largest = 5

· Second_largest = 4

After iterating through the loop we should return the second largest from the function.

Only after seeking clarity and breaking down the problem into simpler parts, we can code.

So we are going to write a function named second_largest that will find and return the second largest_number In our given array.

So we have initialised our two variables largest and second largest to keep track of the largest number so far as we iterate through the loop.

Step 2: We are going to iterate through the list using the For loop within our function.

2.1)

We are checking if the largest is still equal to None, this means the current number we are looking at is the first number in the list.

So we set largest to the current number, if largest is not equal to Nome we move on to our next statement.

2.2)

We are checking is the largest still equal to None, meaning the current number is the first number in the list.

We set the largest to current number ( remember we are keeping track).

Now if largest is not equal to None we move on to our next element.

2.3)

We are checking to see if the current number is bigger than the largest, then we know the current number will be the new largest number, we have also set the second largest to largest as we do not want to loose the value of the largest.

If our condition is not true we move on to the second largest number.

We can check to see if second largest number is None, then we have now shifted our focus to the second largest number in the list and we can assign the second largest to current number.

2.4)

We can already see that current number is not the largest number we have seen thus far.

If the second largest number is not equal to None and if current number is bigger than second largest, we can set second largest to current number.

2.5)

Then we check if the current number is bigger than the second largest and we assign second largest to current number.

If this statement is also not True together with the other statements , we move to the else statement.

2.6)

If none of the conditions are met the loop will stop here and return to us the second largest number in the specified loop.

## More from Thenjiwe kubheka

Software Engineer from South Africa, who loves mathematics. Fascinated by machines, plays around with a lot of data, constantly researching AI.