Interview question: Find the second largest element of an array in Python

Tracyrenee
2 min readJun 7, 2023

If one wants to become a Python developer, he needs to become familiar with the many algorithms in circulation because he may very well be asked to prove his knowledge of the subject at an interview.

One coding question that could very well come up is how to find the second largest element in a list. The simple way to solve this problem is to sort the list and take the second to last element from that list.

The psuedocode to find the second largest element of a list is:-

  1. Define an unsorted list, lst_1.
  2. Define a function, find_sec_largest, which will accept the unsorted list as its input.
  3. Within the function, define the variable, len_lst, which is the length of the unsorted list.
  4. Within the function, use the sorted inbuilt function to sort the list, calling it sorted_lst.
  5. Within the function, define the variable, seeond_largest, which is the penultimate element of the sorted list.
  6. The function will return the variable, second_largest.

Of course there are a number of ways that the list can be sorted, and I have demonstrated a few of those ways in the blog, such as the bubble sort method and the insertion sort method. For brevity, however, I decided to use Python’s inbuilt sort function, which completes the job without all of the fuss.

--

--

Tracyrenee

I have five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.