Interview Q&A · Python · 45 questions in this topic

Python Coding Interview Questions — Python Interview Q&A

All Python topics

45 classic coding problems with the expected approach, a step-by-step walkthrough, an idiomatic Python solution and its complexity — the rounds where you write code. Part of Python Coding Interview Questions · 375 questions in the Python bank.

Reverse a String

Answer

Problem: Given a string s, return the string reversed.

Expected approach: Python slicing (s[::-1]) is the idiomatic one-liner. If asked to do it "manually," use two pointers swapping from both ends, or build the result with a loop.

Complexity — time O(n) · space O(n) (a new string/list is created; O(1) extra space if mutating an in-place character array in a language that allows it)

Key points

  1. 1If slicing is allowed, s[::-1] walks the string backward from the last character to the first.
  2. 2For a manual approach: convert to a list (strings are immutable), swap characters at positions i and len(s)-1-i while i moves inward, then rejoin.

Example

Example.pypython
def reverse_string(s: str) -> str:
    return s[::-1]
# Manual, two-pointer version (what an interviewer often wants to see live)
def reverse_string_manual(s: str) -> str:
    chars = list(s)
    left, right = 0, len(chars) - 1
    while left < right:
        chars[left], chars[right] = chars[right], chars[left]
        left += 1
        right -= 1
    return "".join(chars)

# Example run
# Input: "hello"
# Output: "olleh"

💡 If you reach straight for s[::-1], immediately offer the manual two-pointer version too — interviewers ask this specifically to see whether you can implement the underlying logic, not just recall the idiom.

Check if a String Is a Palindrome

Answer

Problem: Given a string s, determine whether it reads the same forwards and backwards.

Expected approach: Compare the string to its reverse, or use two pointers converging from both ends — the two-pointer approach is what interviewers usually want, since it generalizes to the "ignore punctuation/case" variant (Q289).

Complexity — time O(n) · space O(1) for the two-pointer version; O(n) for the slicing version (creates a reversed copy)

Key points

  1. 1Set left = 0 and right = len(s) -
  2. 2While left < right: if s[left]!= s[right], return False; otherwise move both pointers inward.
  3. 3If the loop completes without mismatches, the string is a palindrome.

Example

Example.pypython
def is_palindrome(s: str) -> bool:
    left, right = 0, len(s) - 1
    while left < right:
        if s[left] != s[right]:
            return False
        left += 1
        right -= 1
    return True
# One-liner equivalent
def is_palindrome_simple(s: str) -> bool:
    return s == s[::-1]

# Example run
# Input: "racecar"
# Output: True
# Input: "hello"
# Output: False

💡 Mention the space complexity difference between the two versions explicitly — it's a common, easy way to demonstrate you think about trade-offs even for "trivial" problems.

Find Duplicate Elements in a List

Answer

Problem: Given a list, return all elements that appear more than once.

Expected approach: Track seen elements in a set as you iterate; anything seen a second time is a duplicate. This is O(n), versus an O(n²) nested-loop comparison.

Complexity — time O(n) · space O(n)

Key points

  1. 1Create an empty seen set and an empty duplicates set.
  2. 2For each element: if it's already in seen, add it to duplicates; otherwise add it to seen.
  3. 3Return duplicates (as a list, if order or a specific type is required).

Example

Example.pypython
def find_duplicates(items):
    seen = set()
    duplicates = set()
    for item in items:
        if item in seen:
            duplicates.add(item)
        else:
            seen.add(item)
    return list(duplicates)

# Example run
# Input: [1, 2, 3, 2, 4, 1, 5]
# Output: [1, 2] (order may vary, since sets are unordered)

💡 If asked for the count of each duplicate rather than just which ones exist, pivot immediately to collections.Counter — it's the more idiomatic tool for that specific variant.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits