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……………
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:
- Using the sort() method
a_list = [25,67,89,12,3,2]
a_list.sort()
print(a_list[1]) #Output - 3This 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]…






