# Top Python Programs for Interviews — Solved and Explained

Source: https://skills101.in/learn/python/python-interview-programs — Skills101 free tutorial (Python Programming). Updated 2026-11-09.

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.

![A function: name, parameters and default value, with return sending the result back — every program below is written as a small function](https://skills101.in/learn-img/python/function-anatomy.svg)

> **Interview tip:** write each solution as a **function**, explain your idea before coding, and mention the time complexity.

## 1. Prime check — O(√n)

```python
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)])
```

```text
[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

```python
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))
```

```text
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
```

## 3. Reverse a number and palindrome number

```python
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)
```

```text
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³).

```python
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)])
```

```text
[153, 370, 371, 407]
```

## 5. Factorial (loop)

```python
def factorial(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

print(factorial(0), factorial(6))
```

```text
1 720
```

## 6. Anagram check

Two words are anagrams if they use the same letters the same number of times.

```python
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"))
```

```text
True False
```

## 7. First non-repeating character

```python
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"))
```

```text
w None
```

## 8. Binary search — O(log n)

```python
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))
```

```text
3 -1
```

The list **must be sorted**. More in our [binary search tutorial](https://skills101.in/learn/dsa/binary-search).

## 9. FizzBuzz

```python
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=" ")
```

```text
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

```python
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"))
```

```text
(2, 9, 3)
```

## Practice on Skills101

Solve similar problems with our judge on the [coding practice page](https://skills101.in/practice/code) — 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".


Practice quiz and free certificate: https://skills101.in/learn/python/python-interview-programs