Sitemap
Python’s Gurus

Python’s Gurus is a Journal, composed by high skilled and knowledgeable Writers from Computer Science World. We’re Devs, Masters, PhDs and Experts in our domaines, possessing deep understanding and proficiency in Python & Several Techs. Sharing real solution for real problems.

Member-only story

Interview: Python Coding Question #1

--

In this python coding series, I will be sharing some of the coding questions I got in my interviews so far.

Let deep dive into the first coding problem……………

Press enter or click to view image in full size
Photo by charlesdeluvio on Unsplash

Question:

Write a python program to find the second smallest number in a list!!

So this is the coding question which was asked and its kind of a common coding round question as well in python interviews. Lets see how to solve this:

There could be several approaches to solving this problem, lets see the first approach:

  1. Using the sort() method
a_list = [25,67,89,12,3,2]
a_list.sort()
print(a_list[1]) #Output - 3

This approach is the simplest and efficient solution to find the second smallest number in a list, so here a_list.sort() will give a sorted list and its easy to access the element by indexing to get the second smallest number from the list.

2. Iterative approach

def find_second_smallest(numbers):
smallest = float('inf')
second_smallest = float('inf')

for num in numbers:
if num < smallest:
second_smallest = smallest
smallest = num
elif num < second_smallest and num != smallest:
second_smallest = num

return second_smallest


numbers = [5, 9, 2, 8, 1]…

--

--