Common Interview Questions

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

Concepts covered

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