Top 50 Python Coding Interview Ques ons & Solu ons
Category 1: Basic String & List Manipula on
1. Reverse a string.
Python
s = "Python"
print(s[::-1])
2. Check if a string is a Palindrome.
Python
s = "radar"
is_palindrome = s == s[::-1]
print(is_palindrome)
3. Remove duplicates from a list.
Python
nums = [1, 2, 2, 3, 4, 4, 5]
unique_nums = list(set(nums))
print(unique_nums)
4. Find the Second Largest number in a list.
Python
nums = [10, 20, 4, 45, 99]
[Link]()
print(nums[-2])
5. Count occurrences of each character in a string.
Python
s = "banana"
freq = {char: [Link](char) for char in set(s)}
print(freq)
6. Reverse words in a given string.
Python
s = "I love Python"
words = [Link]()
print(" ".join(words[::-1]))
7. Check if two strings are Anagrams.
Python
s1, s2 = "listen", "silent"
print(sorted(s1) == sorted(s2))
8. Find common elements in two lists.
Python
l1, l2 = [1, 2, 3], [2, 3, 4]
print(list(set(l1) & set(l2)))
9. Extract vowels from a string.
Python
s = "Machine Learning"
vowels = [char for char in s if [Link]() in 'aeiou']
print(vowels)
10. Find the length of a string without using len().
Python
s = "Hello"
count = 0
for char in s:
count += 1
print(count)
Category 2: Mathema cal Logic & Numbers
11. Factorial of a number (Recursive).
Python
def fact(n):
return 1 if n == 0 else n * fact(n-1)
print(fact(5))
12. Fibonacci series up to $n$ terms.
Python
n=5
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
13. Check if a number is Prime.
Python
num = 11
is_prime = all(num % i != 0 for i in range(2, int(num**0.5) + 1))
print(is_prime)
14. Swap two numbers without a third variable.
Python
a, b = 5, 10
a, b = b, a
print(a, b)
15. Sum of digits of a number.
Python
n = 123
print(sum(int(d) for d in str(n)))
16. Check for Armstrong Number (e.g., $153 = 1^3 + 5^3 + 3^3$).
Python
num = 153
s = str(num)
power = len(s)
print(num == sum(int(i)**power for i in s))
17. Find the GCD (Greatest Common Divisor).
Python
import math
print([Link](12, 18))
18. Convert Decimal to Binary.
Python
print(bin(10).replace("0b", ""))
19. Check if a year is a Leap Year.
Python
year = 2024
print((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0))
20. Find all divisors of a number.
Python
n = 10
print([i for i in range(1, n + 1) if n % i == 0])
Category 3: List & Array Logic
21. Find the Maximum and Minimum in a list.
Python
l = [5, 2, 9, 1]
print(max(l), min(l))
22. Merge two dic onaries.
Python
d1 = {'a': 1}
d2 = {'b': 2}
[Link](d2)
print(d1)
23. List Comprehension: Square of even numbers.
Python
nums = [1, 2, 3, 4]
print([x**2 for x in nums if x % 2 == 0])
24. Fla en a nested list.
Python
nested = [[1, 2], [3, 4]]
flat = [item for sublist in nested for item in sublist]
print(flat)
25. Move all Zeros to the end of a list.
Python
l = [0, 1, 0, 3, 12]
[Link](key=lambda x: x == 0)
print(l)
26. Find the intersec on of two sets.
Python
s1 = {1, 2, 3}
s2 = {2, 3, 4}
print([Link] on(s2))
27. Sort a dic onary by its values.
Python
d = {'apple': 10, 'orange': 5, 'banana': 15}
sorted_d = dict(sorted([Link](), key=lambda item: item[1]))
print(sorted_d)
28. Find the missing number in an array of 1 to $n$.
Python
arr = [1, 2, 4, 5, 6] # 3 is missing
n=6
expected_sum = n * (n + 1) // 2
print(expected_sum - sum(arr))
29. Check if a list is sorted.
Python
l = [1, 2, 3, 5, 4]
print(l == sorted(l))
30. Right rotate a list by $k$ posi ons.
Python
l, k = [1, 2, 3, 4, 5], 2
k = k % len(l)
print(l[-k:] + l[:-k])
Category 4: Intermediate Logic & Forma ng
31. Generate a random number between 1 and 100.
Python
import random
print([Link](1, 100))
32. Count the number of words in a sentence.
Python
s = "Python is fun"
print(len([Link]()))
33. Convert a list of characters into a string.
Python
chars = ['a', 'b', 'c']
print("".join(chars))
34. Remove whitespace from a string.
Python
s = " Hello World "
print([Link]())
35. Find the ASCII value of a character.
Python
print(ord('A'))
36. Print the mul plica on table of a number.
Python
n=5
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
37. Filter nega ve numbers from a list.
Python
l = [-1, 2, -3, 4]
print([x for x in l if x >= 0])
38. Find the Cumula ve Sum of a list.
Python
import itertools
l = [1, 2, 3, 4]
print(list([Link](l)))
39. Use zip to pair two lists.
Python
keys = ['name', 'age']
vals = ['Alice', 25]
print(dict(zip(keys, vals)))
40. Check if a string is alphanumeric.
Python
print("Python3".isalnum())
Category 5: Advanced-Basic (Pa erns & Files)
41. Read the first $n$ lines of a file.
Python
# with open('fi[Link]') as f:
# print([next(f) for _ in range(n)])
42. Star Pyramid Pa ern.
Python
n=5
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
43. Transpose a Matrix.
Python
m = [[1, 2], [3, 4]]
print([[row[i] for row in m] for i in range(len(m[0]))])
44. Convert seconds into hours, minutes, and seconds.
Python
s = 3661
h = s // 3600
m = (s % 3600) // 60
sec = s % 60
print(f"{h}:{m}:{sec}")
45. Find the most frequent element in a list.
Python
l = [1, 2, 3, 1, 2, 1]
print(max(set(l), key=[Link]))
46. Check if a string contains any special characters.
Python
import re
s = "Hello@World"
print(bool([Link](r'[^a-zA-Z0-9]', s)))
47. Generate a list of squares using map().
Python
l = [1, 2, 3]
print(list(map(lambda x: x**2, l)))
48. Find the largest word in a string.
Python
s = "I love programming in Python"
print(max([Link](), key=len))
49. Check if a list is empty.
Python
l = []
if not l: print("Empty")
50. Calculate the execu on me of a code block.
Python
import me
start = me. me()
# code here
print( me. me() - start)