Python Programming · Lesson 15 of 15 · Updated ·
Top Python Programs for Interviews — Solved and Explained
These programs come up in almost every TCS, Infosys, Wipro and Accenture technical round. Understand each one — don't memorise — because interviewers often change one detail.
Interview tip: write each solution as a function, explain your idea before coding, and mention the time complexity.
1. Prime check — O(√n)
def is_prime(n):
if n < 2:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
print([x for x in range(1, 30) if is_prime(x)])
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Why √n? If n = a × b, one of a or b is at most √n, so checking up to √n is enough.
2. Fibonacci series
def fibonacci(n):
a, b = 0, 1
result = []
for _ in range(n):
result.append(a)
a, b = b, a + b
return result
print(fibonacci(10))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
3. Reverse a number and palindrome number
def reverse_number(n):
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
return rev
print(reverse_number(12345), reverse_number(1221) == 1221)
54321 True
(Interviewers often ask you to do it without converting to a string — this is how.)
4. Armstrong number
A number equal to the sum of its digits, each raised to the number of digits (153 = 1³ + 5³ + 3³).
def is_armstrong(n):
digits = str(n)
return n == sum(int(d) ** len(digits) for d in digits)
print([x for x in range(100, 1000) if is_armstrong(x)])
[153, 370, 371, 407]
5. Factorial (loop)
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
return result
print(factorial(0), factorial(6))
1 720
6. Anagram check
Two words are anagrams if they use the same letters the same number of times.
from collections import Counter
def is_anagram(a, b):
return Counter(a.lower()) == Counter(b.lower())
print(is_anagram("Listen", "Silent"), is_anagram("apple", "paper"))
True False
7. First non-repeating character
def first_unique(s):
counts = {}
for ch in s:
counts[ch] = counts.get(ch, 0) + 1
for ch in s:
if counts[ch] == 1:
return ch
return None
print(first_unique("swiss"), first_unique("aabb"))
w None
8. Binary search — O(log n)
def binary_search(nums, target):
lo, hi = 0, len(nums) - 1
while lo <= hi:
mid = (lo + hi) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
print(binary_search([3, 8, 15, 23, 42, 56], 23), binary_search([3, 8, 15], 4))
3 -1
The list must be sorted. More in our binary search tutorial.
9. FizzBuzz
for i in range(1, 16):
if i % 15 == 0:
print("FizzBuzz", end=" ")
elif i % 3 == 0:
print("Fizz", end=" ")
elif i % 5 == 0:
print("Buzz", end=" ")
else:
print(i, end=" ")
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
Check 15 first — otherwise 15 prints "Fizz".
10. Count vowels, consonants, digits
def classify(s):
v = c = d = 0
for ch in s.lower():
if ch in "aeiou":
v += 1
elif ch.isalpha():
c += 1
elif ch.isdigit():
d += 1
return v, c, d
print(classify("Skills101 Rocks"))
(2, 9, 3)
Practice on Skills101
Solve similar problems with our judge on the coding practice page — your accepted solutions count towards your verified profile.
Common mistakes
- Prime checks that loop to n instead of √n (too slow for big n).
- FizzBuzz checking 3 before 15.
- Binary search on an unsorted list.
Quick recap
- Write each solution as a function; explain, then code; state the complexity.
- Know the digit tricks:
n % 10(last digit),n //= 10(drop last digit). - Counter for anagrams and frequencies; a dictionary for "first unique".