Python coding interviews are designed to test a candidate’s problem-solving ability, programming knowledge, and understanding of Python concepts. Interviewers often ask coding questions ranging from basic string manipulation to advanced data structures and algorithms.
In this article, we’ll cover some of the most commonly asked Python coding interview questions with solutions and explanations.
1. Write a Program to Reverse a String
Solution
text = "Python"
reversed_text = text[::-1]
print(reversed_text)
Output
Explanation
The slicing syntax [::-1] reverses the string.
2. Check Whether a String is a Palindrome
A palindrome reads the same forward and backward.
Solution
text = "madam"
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Output
3. Find the Factorial of a Number
Solution
num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(factorial)
Output
4. Find Fibonacci Series
Solution
n = 10
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
Output
5. Check Whether a Number is Prime
Solution
num = 17
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print("Prime")
else:
print("Not Prime")
Output
6. Find the Largest Number in a List
Solution
numbers = [10, 45, 12, 78, 34]
largest = max(numbers)
print(largest)
Output
7. Remove Duplicates from a List
Solution
numbers = [1, 2, 2, 3, 4, 4, 5]
unique = list(set(numbers))
print(unique)
Output
8. Count Vowels in a String
Solution
text = "Python Programming"
count = 0
for char in text.lower():
if char in "aeiou":
count += 1
print(count)
Output
9. Swap Two Numbers Without Using a Third Variable
Solution
a = 10
b = 20
a, b = b, a
print(a, b)
Output
10. Find the Sum of Digits
Solution
num = 1234
total = sum(int(digit) for digit in str(num))
print(total)
Output
11. Find the Second Largest Element in a List
Solution
numbers = [10, 20, 40, 30, 50]
numbers.sort()
print(numbers[-2])
Output
12. Count Character Frequency
Solution
text = "python"
frequency = {}
for char in text:
frequency[char] = frequency.get(char, 0) + 1
print(frequency)
Output
13. Find Common Elements Between Two Lists
Solution
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = list(set(list1) & set(list2))
print(common)
Output
14. Sort a Dictionary by Value
Solution
data = {
"a": 3,
"b": 1,
"c": 2
}
sorted_data = dict(
sorted(data.items(), key=lambda x: x[1])
)
print(sorted_data)
Output
15. Find Missing Number in a Sequence
Solution
numbers = [1, 2, 3, 5]
n = 5
expected_sum = n * (n + 1) // 2
actual_sum = sum(numbers)
print(expected_sum - actual_sum)
Output
16. Check if Two Strings are Anagrams
Solution
str1 = "listen"
str2 = "silent"
if sorted(str1) == sorted(str2):
print("Anagram")
else:
print("Not Anagram")
Output
Solution
numbers = [1, 2, 3, 4, 5]
count = 0
for item in numbers:
count += 1
print(count)
Output
18. Find the Maximum Occurring Character
Solution
text = "programming"
result = max(text, key=text.count)
print(result)
Output
19. Merge Two Dictionaries
Solution
dict1 = {"a": 1}
dict2 = {"b": 2}
merged = {**dict1, **dict2}
print(merged)
Output
20. Find Even Numbers from a List
Solution
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [
num
for num in numbers
if num % 2 == 0
]
print(even_numbers)
Output
21. Find the Intersection of Two Lists
Solution
list1 = [1, 2, 3]
list2 = [2, 3, 4]
result = list(
set(list1).intersection(list2)
)
print(result)
Output
22. Count Words in a Sentence
Solution
sentence = "Python is easy to learn"
words = sentence.split()
print(len(words))
Output
23. Find the Largest Word in a Sentence
Solution
sentence = "Python programming language"
largest = max(
sentence.split(),
key=len
)
print(largest)
Output
24. Generate a Random Password
Solution
import random
import string
password = "".join(
random.choices(
string.ascii_letters +
string.digits,
k=8
)
)
print(password)
25. Find Duplicate Elements in a List
Solution
numbers = [1, 2, 2, 3, 4, 4]
duplicates = []
for num in numbers:
if numbers.count(num) > 1 and num not in duplicates:
duplicates.append(num)
print(duplicates)
Output
26. Flatten a Nested List
Solution
nested = [
[1, 2],
[3, 4],
[5, 6]
]
flat = [
item
for sublist in nested
for item in sublist
]
print(flat)
Output
27. Find Whether a Number is Armstrong
Solution
num = 153
digits = str(num)
result = sum(
int(digit) ** len(digits)
for digit in digits
)
if result == num:
print("Armstrong Number")
else:
print("Not Armstrong Number")
28. Reverse Words in a Sentence
Solution
sentence = "Python Coding Interview"
result = " ".join(
sentence.split()[::-1]
)
print(result)
Output
29. Find the First Non-Repeating Character
Solution
text = "programming"
for char in text:
if text.count(char) == 1:
print(char)
break
Output
30. Implement FizzBuzz
Solution
for num in range(1, 21):
if num % 3 == 0 and num % 5 == 0:
print("FizzBuzz")
elif num % 3 == 0:
print("Fizz")
elif num % 5 == 0:
print("Buzz")
else:
print(num)
Conclusion
Python coding interviews evaluate your problem-solving skills, logical thinking, and programming knowledge. Questions commonly involve strings, lists, dictionaries, recursion, searching, sorting, and basic algorithms.
Whether you’re a fresher or an experienced developer, regular coding practice is the key to success in Python interviews.