Python Interview Coding Questions with Solutions for Beginners
Python Interview Coding Questions with Solutions for Beginners
Python interviews require that you not only understand the fundamental concepts of this popular programming language, but that you also showcase your practical skills through coding challenges presented to you during the interview. To make sure that you are able to solve these challenges satisfactorily, it is a good idea to practice answering such questions before you go for your interview.
Here are 10 frequently asked interview questions to help you excel in your Python coding interview. If you are a beginner to Python, these questions should help you assess your pre-interview preparation.
Remember to try solving each of these questions on your own before verifying your answer with the solutions provided.
All the best!
Python Interview Coding Questions
Question 1: Write a Python program to check if a string is a palindrome.
Solution:
def is_palindrome(string):
reversed_string = string[::-1]
return string == reversed_string
# Test the function
word = "madam"
if is_palindrome(word):
print(f"{word} is a palindrome")
else:
print(f"{word} is not a palindrome")
Question 2: Write a Python program to find the factorial of a number.
Solution:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Test the function
number = 5
result = factorial(number)
print(f"The factorial of {number} is {result}")
Question 3: Write a Python program to find the largest element in a list.
Solution:
def find_largest(numbers):
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
return largest
# Test the function
nums = [10, 5, 8, 20, 3]
largest_num = find_largest(nums)
print(f"The largest number is {largest_num}")
Question 4: Write a Python program to reverse a string.
Soltuion:
def reverse_string(string):
return string[::-1]
# Test the function
text = "Hello, World!"
reversed_text = reverse_string(text)
print(reversed_text)
Question 5: Write a Python program to count the frequency of each element in a list.
Solution:
def count_frequency(numbers):
frequency = {}
for num in numbers:
if num in frequency:
frequency[num] += 1
else:
frequency[num] = 1
return frequency
# Test the function
nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4]
frequency_count = count_frequency(nums)
print(frequency_count)
Question 6: Write a Python program to check if a number is prime.
Solution:
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
# Test the function
num = 17
if is_prime(num):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
Question 7: Write a Python program to find the common elements between two lists.
Solution:
def find_common_elements(list1, list2):
common_elements = []
for item in list1:
if item in list2:
common_elements.append(item)
return common_elements
# Test the function
list_a = [1, 2, 3, 4, 5]
list_b = [4, 5, 6, 7, 8]
common = find_common_elements(list_a, list_b)
print(common)
Question 8: Write a Python program to sort a list of elements using the bubble sort algorithm.
Solution:
def bubble_sort(elements):
n = len(elements)
for i in range(n - 1):
for j in range(n - i - 1):
if elements[j] > elements[j + 1]:
elements[j], elements[j + 1] = elements[j + 1], elements[j]
# Test the function
nums = [5, 2, 8, 1, 9]
bubble_sort(nums)
print(nums)
Question 9: Write a Python program to find the second largest number in a list.
Solution:
def find_second_largest(numbers):
largest = float('-inf')
second_largest = float('-inf')
for num in numbers:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
return second_largest
# Test the function
nums = [10, 5, 8, 20, 3]
second_largest_num = find_second_largest(nums)
print(f"The second largest number is {second_largest_num}")
Question 10: Write a Python program to remove duplicates from a list.
Solution:
def remove_duplicates(numbers):
unique_numbers = []
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
return unique_numbers
# Test the function
nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4]
unique_nums = remove_duplicates(nums)
print(unique_nums)
Have you attended Python job interviews before? What kind of questions were you asked? Comment below to help your fellow-Pythonistas out!
No comments