■ Python Interview Coding Questions (30 with
Solutions)
1. Reverse a string
s = "hello"
print(s[::-1])
2. Check if a string is palindrome
s = "madam"
print(s == s[::-1])
3. Count vowels and consonants
def count_vowels_consonants(text):
vowels = "aeiouAEIOU"
v = c = 0
for ch in text:
if ch.isalpha():
if ch in vowels:
v += 1
else:
c += 1
return v, c
print(count_vowels_consonants("Hello World"))
4. Factorial of a number (recursion)
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
print(factorial(5))
5. Check if number is prime
num = 29
is_prime = num > 1 and all(num % i != 0 for i in range(2, int(num**0.5)+1))
print(is_prime)
6. Fibonacci sequence
n = 10
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a+b
7. Largest element in a list
arr = [10, 25, 7, 98, 56]
print(max(arr))
8. Second largest element in a list
arr = [10, 25, 7, 98, 56]
arr = list(set(arr))
arr.sort()
print(arr[-2])
9. Check Armstrong number
num = 153
order = len(str(num))
print(num == sum(int(d)**order for d in str(num)))
10. Reverse a number
num = 12345
rev = int(str(num)[::-1])
print(rev)