# Common Interview Questions

> Certain coding questions appear in Python interviews with remarkable frequency. While the algorithms behind them are straightforward, interviewers use...

Canonical: https://www.internhack.in/learn/python/python-interview-essentials/common-interview-questions
Last updated: 2026-11-28

Difficulty: Advanced

Certain coding questions appear in Python interviews with remarkable frequency. While the algorithms behind them are straightforward, interviewers use them to assess your Python fluency, ability to handle edge cases, and whether you can write clean, efficient code under pressure.

Reversing a string is the simplest of these but reveals a lot about your Python knowledge. The Pythonic way is slicing: \`s\[::-1\]\`. You can also use \`reversed()\` with \`''.join()\`. Knowing multiple approaches and their trade-offs shows depth.

Palindrome checking builds on string reversal: a string is a palindrome if it reads the same forwards and backwards. The key is handling case sensitivity and non-alphanumeric characters. A clean solution filters and lowercases first, then compares with the reverse.

Anagram detection asks whether two strings contain exactly the same characters in different orders. The Pythonic approach uses \`sorted()\` on both strings, or \`collections.Counter\` for O(n) comparison. Interviewers expect you to discuss time complexity: sorting is O(n log n) while Counter is O(n).

Flattening a nested list is a recursion classic. The challenge is handling arbitrary nesting depth. A recursive solution checks if each element is itself a list and recurses into it. Iterative solutions using a stack are also acceptable.

Finding duplicates in a list tests your knowledge of sets and dictionary counting. The most Pythonic approach uses a set to track seen elements, or \`Counter\` to count occurrences. Interviewers care about O(n) time complexity and O(n) space.

## Code examples

### String reversal techniques

```
s = "hello world"

# Slicing
print("slicing:", s[::-1])

# reversed() + join
print("reversed:", "".join(reversed(s)))

# Manual loop
result = ""
for char in s:
    result = char + result
print("manual:", result)
```

Slicing with \[::-1\] is the most Pythonic and fastest approach. reversed() returns an iterator that join() consumes.

### Palindrome check with cleaning

```
def is_palindrome(s):
    cleaned = "".join(c.lower() for c in s if c.isalnum())
    return cleaned == cleaned[::-1]

print(is_palindrome("racecar"))
print(is_palindrome("A man, a plan, a canal: Panama"))
print(is_palindrome("hello"))
```

First strip non-alphanumeric characters and lowercase, then compare with the reverse. This handles punctuation and mixed case.

### Anagram detection

```
from collections import Counter

def are_anagrams(s1, s2):
    return Counter(s1.lower()) == Counter(s2.lower())

print(are_anagrams("listen", "silent"))
print(are_anagrams("hello", "world"))
print(are_anagrams("Dormitory", "Dirty room"))
```

Counter creates a frequency map of characters. Two strings are anagrams if their frequency maps are equal. Note: spaces are counted unless explicitly filtered.

### Finding duplicates with a set

```
def find_duplicates(lst):
    seen = set()
    duplicates = set()
    for item in lst:
        if item in seen:
            duplicates.add(item)
        seen.add(item)
    return sorted(duplicates)

nums = [1, 3, 5, 3, 7, 1, 9, 5]
print("duplicates:", find_duplicates(nums))
```

Using a set for 'seen' items gives O(1) lookups. When an item is already in 'seen', it is a duplicate. This runs in O(n) time.

## Key points

- String slicing \[::-1\] is the fastest and most Pythonic way to reverse a string.
- Always clarify edge cases with the interviewer: empty strings, single characters, case sensitivity, spaces.
- Counter is O(n) for anagram detection while sorted() is O(n log n) -- mention the trade-off.
- For flatten, clarify whether the input can have non-list iterables (tuples, generators) and how deep nesting can go.

## Interview tips

- Start with the brute-force approach, explain its complexity, then optimize -- interviewers want to see your thought process.
- For string problems, always ask about Unicode, case sensitivity, and special characters before coding.
- When using Counter or sorted, mention the time complexity to show algorithmic awareness.

## Concepts covered

string reversal, palindrome, anagram, flatten list, find duplicates

Previous: [Pythonic Patterns](https://www.internhack.in/learn/python/python-interview-essentials/pythonic-patterns) · Next: [Python Internals](https://www.internhack.in/learn/python/python-interview-essentials/python-internals) · [All of Interview Essentials](https://www.internhack.in/learn/python/python-interview-essentials)

## More Interview Essentials lessons

- [Mutable vs Immutable Types](https://www.internhack.in/learn/python/python-interview-essentials/mutable-vs-immutable)
- [Python Gotchas](https://www.internhack.in/learn/python/python-interview-essentials/python-gotchas)

## Related

- [Learn](https://www.internhack.in/learn)
- [Python](https://www.internhack.in/learn/python)
- [Interview Essentials](https://www.internhack.in/learn/python/python-interview-essentials)
