Top Python Coding Interview Questions and Answers (2026)

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

nohtyP

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

Palindrome

3. Find the Factorial of a Number

Solution


num = 5
factorial = 1
for i in range(1, num + 1):
    factorial *= i
print(factorial)

Output

120

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

0 1 1 2 3 5 8 13 21 34

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

Prime

6. Find the Largest Number in a List

Solution


numbers = [10, 45, 12, 78, 34]
largest = max(numbers)
print(largest)

Output

78

7. Remove Duplicates from a List

Solution


numbers = [1, 2, 2, 3, 4, 4, 5]
unique = list(set(numbers))
print(unique)

Output

[1, 2, 3, 4, 5]

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

4

9. Swap Two Numbers Without Using a Third Variable

Solution


a = 10
b = 20
a, b = b, a
print(a, b)

Output

20 10

10. Find the Sum of Digits

Solution


num = 1234
total = sum(int(digit) for digit in str(num))
print(total)

Output

10

11. Find the Second Largest Element in a List

Solution


numbers = [10, 20, 40, 30, 50]
numbers.sort()
print(numbers[-2])

Output

40

12. Count Character Frequency

Solution


text = "python"
frequency = {}
for char in text:
    frequency[char] = frequency.get(char, 0) + 1
print(frequency)

Output

{‘p’: 1, ‘y’: 1, ‘t’: 1, ‘h’: 1, ‘o’: 1, ‘n’: 1}

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

[3, 4]

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

{‘b’: 1, ‘c’: 2, ‘a’: 3}

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

4

16. Check if Two Strings are Anagrams

Solution


str1 = "listen"
str2 = "silent"
if sorted(str1) == sorted(str2):
    print("Anagram")
else:
    print("Not Anagram")

Output

Anagram

Solution


numbers = [1, 2, 3, 4, 5]
count = 0
for item in numbers:
    count += 1
print(count)

Output

5

18. Find the Maximum Occurring Character

Solution


text = "programming"
result = max(text, key=text.count)
print(result)

Output

g

19. Merge Two Dictionaries

Solution


dict1 = {"a": 1}
dict2 = {"b": 2}
merged = {**dict1, **dict2}
print(merged)

Output

{‘a’: 1, ‘b’: 2}

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

[2, 4, 6]

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

[2, 3]

22. Count Words in a Sentence

Solution


sentence = "Python is easy to learn"
words = sentence.split()
print(len(words))

Output

5

23. Find the Largest Word in a Sentence

Solution


sentence = "Python programming language"
largest = max(
    sentence.split(),
    key=len
)
print(largest)

Output

programming

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

[2, 4]

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

[1, 2, 3, 4, 5, 6]

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

Interview Coding Python

29. Find the First Non-Repeating Character

Solution


text = "programming"
for char in text:
    if text.count(char) == 1:
        print(char)
        break

Output

p

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.

Related Python Tutorials