In this article, we are going to discuss 10 simple Python coding interview questions and answers that are entirely for freshers. The following are the questions that we are going to cover in this article:
- Find duplicate numbers in the given array.
- Find the maximum and minimum number in an unsorted array.
- Find all pairs of integers in an array whose sum is equal to a given number.
- Find the missing number in an array.
- Check given two strings are anagrams or not.
- Find the factorial of a number.
- Find given number is prime or not.
- Find leap years between 2000 and 2050.
- Search an element in an array using a linear search algorithm.
- Search an element in an array using a binary search algorithm.
Before getting into the python coding interview questions and answers, I let you know about what python is actually: an interpreted, object-oriented, high-level, simple, and easy-to-use programming language. Python supports modules and packages, which encourages programme modularity and code reuse.
1. Find duplicate numbers in the given array
Our first python coding interview question is about finding duplicate numbers in the given array. The following Python programme prints how many times the number is duplicated. If no number is duplicated, then the Python programme will not print anything.
# FINDING DUPLICATE NUMBERS IN GIVEN ARRAY
arr = [6, 1, 2, 7, 1, 9, 1, 0, 4, 0, 2, 5] # input array
frequency = dict()
for ele in arr:
if ele in frequency:
frequency[ele] = frequency[ele] + 1
else:
frequency[ele] = 1
for i in frequency:
if frequency[i] > 1:
print(f"{i} is repeated {frequency[i]} times")
Output:
1 is repeated 3 times
2 is repeated 2 times
0 is repeated 2 times
2. Find the maximum and minimum number in an unsorted array
This python program is to print the maximum and minimum number in the given unsorted array. The program first sorts the given array using Bubble Sort Algorithm (You can use the built-in sort() function in python). Then, it will print the first (zeroth index) number as the minimum number and the last (length of the array -1) number as the maximum number.
# FINDING THE MAXIMUM AND MINIMUM NUMBER IN UNSORTED ARRAY
arr = [6, 1, 2, 7, 1, 9, 1, 0, 4, 0, 2, 5, 89, -1]
# sorts using Bubble Sort Algorithm
for i in range(len(arr)-1):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
print(f"Maximum number in the unsorted array: {arr[-1]}")
print(f"Minimum number in the unsorted array: {arr[0]}")
Output:
Maximum number in the unsorted array: 89
Minimum number in the unsorted array: -1
3. Find all pairs of integers in an array whose sum is equal to a given number
This Python programme will print all the number combinations whose sum equals the given number.In my case, I am going to store the given number in the sum variable. Let’s look into the program:
# FIND ALL PAIRS OF INTEGER ARRAYS WHOSE SUM IS EQUAL TO A GIVEN NUMBER.
from itertools import combinations
sum = 7
arr = [2, 4, 3, 5, 6, -2, 4, 7, 8, 9]
combinations = list(combinations(arr, 2))
for combination in combinations:
if (combination[0] + combination[1]) == sum:
print(combination)
Output:
(2, 5)
(4, 3)
(3, 4)
(-2, 9)
4. Find the missing number in an array
This python programme prints all the missing numbers in an array. It will first sort the array using the built-in sort() function. Because if the array is sorted, then finding the missing number becomes easier. Let’s get into the program:
# FIND THE MISSING NUMBER IN AN ARRAY
def finding_missing_numbers(array):
array.sort()
return f"The missing numbers are, \n{sorted(set(range(array[0], array[-1]+1)) - set(array))}"
array = [20, 18, 15]
print(finding_missing_numbers(array))
Output:
The missing numbers are,
[16, 17, 19]
5. Check given two strings are anagrams or not
This python programme prints whether the given two integers are anagrams or not. Before that, an anagram is simply a word or phrase formed by rearranging the letters of another word or phrase in a different order. Here, the condition is very simple. If the length of two strings is the same and the characters in both strings are the same, then they will be anagrams. If it does not satisfy either condition, then it is not an anagram:
# CHECK GIVEN TWO STRINGS ARE ANAGRAM OR NOT
str_1 = "fried"
str_2 = "fired"
# CONDITION CHECKING
if len(str_1) == len(str_2):
if sorted(str_1) == sorted(str_2):
print("Given strings are anagrams")
else:
print("Given strings are not anagrams")
Output:
Given strings are anagrams
6. Find the factorial of a number
This python programme prints the factorial of the given number (n). A factorial is a mathematical operation that is expressed as: n! Actually, it is the multiplication of all numbers between 1 and n. For example, if n is equal to 5, then the factorial will be 120 (i.e., 5 x 4 x 3 x 2 x 1 = 120):
# FIND FACTORIAL OF A NUMBER
def fact(n):
if n == 1:
return 1
return n * fact(n-1)
num = 5
factorial = fact(num)
print(f"Factorial of {num} is {factorial}")
Output:
Factorial of 5 is 120
7. Find given number is prime or not
This Python programme prints whether the given number is a prime number or not. You already know that a prime number is a number that is only divisible by 1 and itself. Let’s get into the program:
# FIND GIVEN NUMBER IS PRIME OR NOT
def isPrime(num):
prime = False
if num == 2:
prime = True
return prime
else:
for i in range(2, num):
if num % i == 0:
prime = True
break
return prime
num = 10
if isPrime(num):
print("Given number is prime number")
else:
print("Given number is not a prime number")
Output:
Given number is prime number
8. Find leap years between 2000 and 2050
This Python programme prints all the leap years between 2000 and 2050. You already know that a leap year is the year that has 29 days in February instead of 28. This change occurs every four years. This is called a leap year:
# FINDING LEAP YEAR BETWEEN 2000 AND 2050
def isleap(year):
if (year % 400 == 0) or (year % 100 != year % 4 == 0):
print(f"{year} is leap year")
for i in range(2000, 2051):
isleap(i)
Output:
2000 is leap year
2004 is leap year
2008 is leap year
2012 is leap year
2016 is leap year
2020 is leap year
2024 is leap year
2028 is leap year
2032 is leap year
2036 is leap year
2040 is leap year
2044 is leap year
2048 is leap year
9. Search an element in an array using a linear search algorithm
This python programme searches for the element in an array and prints where the element is located. This is accomplished by employing the linear search algorithm, which is nothing more than a method of searching the array’s elements one by one to the end. If it finds the element, then it stops its iteration and prints the element with the place value (index value) of the element. If the element is not in the array, then it will print the element that is not present in the array:
list_1 = [67,12,34,65,87,90,54,78,23]
search_value = int(input('Enter the value to search : '))
i = 0
while i<len(list_1) :
if list_1[i] == search_value :
print(f'The element is present in the list at the {position} {i+1}')
break
i += 1
else :
print('The element is not present in the list')
Output:
Enter the value to search: 72
The element is not present in the list
Enter the value to search: 90
The element is present in the list at the position 6
10. Search an element in an array using a binary search algorithm
This python programme searches for the element in an array and prints where the element is located. This is done by using the binary search algorithm, also known as half-interval search, logarithmic search, or binary chop, which is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array:
# BINARY SEARCH ALGORITHM
# The input array should be sorted
list1 = [10,20,30,40,50,60,70,80,90,100]
search_element = int(input('Enter the element to search : '))
start = 0
end = len(list1)-1
while end >= start:
mid = (start+end)//2
if list1[mid] == search_element:
print('The element %d is present in the list at positon %d'%(search_element,mid+1))
break
elif list1[mid] > search_element:
end = mid-1
elif list1[mid] < search_element:
start = mid+1
else:
print('The element %d is not present in the list'%search_element)
Output:
Enter the element to search : 100
The element 100 is present in the list at position 10