Coding Prep9 September 2026•15 min read•592 words

Top 20 Python Coding Questions for Technical Interviews (With Solutions)

Comprehensive collection of the top 20 Python coding problems tested in technical interviews. Complete code solutions, explanations, time complexity analysis, and Pythonic patterns.

Abu Thahir

Abu Thahir

Founder & Career Mentor at GetJobWithAbu

Python has become the preferred language for coding rounds at tech companies due to its clean syntax and powerful standard library. However, interviewers do not just look for code that passes; they evaluate whether you write idiomatic, efficient Python and understand time and space complexity.

Here are the most frequently asked Python coding questions with complete explanations and optimal solutions.

---

1. Two Sum Problem

Problem: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

```python

def two_sum(nums: list[int], target: int) -> list[int]:

seen = {} # value -> index

for i, num in enumerate(nums):

complement = target - num

if complement in seen:

return [seen[complement], i]

seen[num] = i

return []

print(two_sum([2, 7, 11, 15], 9)) # Output: [0, 1]

```

  • Time Complexity: O(N) — Single pass hash map lookup.
  • Space Complexity: O(N) — Storing elements in dictionary.

---

2. Valid Anagram

Problem: Given two strings s and t, return True if t is an anagram of s, and False otherwise.

```python

from collections import Counter

def is_anagram(s: str, t: str) -> bool:

if len(s) != len(t):

return False

return Counter(s) == Counter(t)

print(is_anagram("anagram", "nagaram")) # True

print(is_anagram("rat", "car")) # False

```

  • Time Complexity: O(N)
  • Space Complexity: O(1) since English alphabet size is bounded to 26 characters.

---

3. Reverse Words in a String

Problem: Given an input string s, reverse the order of the words while trimming multiple spaces.

```python

def reverse_words(s: str) -> str:

words = s.split()

return " ".join(words[::-1])

print(reverse_words(" the sky is blue ")) # "blue is sky the"

```

---

4. Find the First Non-Repeating Character

Problem: Return the index of the first non-repeating character in a string. If it does not exist, return -1.

```python

from collections import Counter

def first_uniq_char(s: str) -> int:

count = Counter(s)

for idx, char in enumerate(s):

if count[char] == 1:

return idx

return -1

print(first_uniq_char("leetcode")) # 0 ('l')

print(first_uniq_char("loveleetcode")) # 2 ('v')

```

---

5. Group Anagrams

Problem: Given an array of strings strs, group the anagrams together.

```python

from collections import defaultdict

def group_anagrams(strs: list[str]) -> list[list[str]]:

anagram_map = defaultdict(list)

for word in strs:

key = tuple(sorted(word))

anagram_map[key].append(word)

return list(anagram_map.values())

print(group_anagrams(["eat","tea","tan","ate","nat","bat"]))

```

---

6. Merge Two Sorted Lists

Problem: Merge two sorted linked lists into one sorted linked list.

```python

class ListNode:

def __init__(self, val=0, next=None):

self.val = val

self.next = next

def merge_two_lists(l1: ListNode, l2: ListNode) -> ListNode:

dummy = ListNode(-1)

current = dummy

while l1 and l2:

if l1.val <= l2.val:

current.next = l1

l1 = l1.next

else:

current.next = l2

l2 = l2.next

current = current.next

current.next = l1 if l1 else l2

return dummy.next

```

  • Time Complexity: O(N + M)
  • Space Complexity: O(1)

---

7. Longest Substring Without Repeating Characters

```python

def length_of_longest_substring(s: str) -> int:

char_map = {}

left = 0

max_len = 0

for right, char in enumerate(s):

if char in char_map and char_map[char] >= left:

left = char_map[char] + 1

char_map[char] = right

max_len = max(max_len, right - left + 1)

return max_len

print(length_of_longest_substring("abcabcbb")) # 3 ("abc")

```

---

8. Python Idioms That Impress Interviewers

When coding in Python during an interview, leverage built-in language features:

  1. List Comprehensions: [x**2 for x in nums if x % 2 == 0] instead of verbose loops.
  2. `enumerate()`: Never write range(len(arr)) when you need both index and element.
  3. `zip()`: Use to iterate over parallel arrays simultaneously.
  4. `collections.defaultdict`: Avoids tedious if key not in dict: checks.
  5. `collections.Counter`: Instant frequency counting.
Abu Thahir - Author

Written by Abu Thahir

Founder & Career Mentor

IT career advisor, technical interview coach, and observability specialist with years of hands-on experience in the tech industry.

📅 Last updated: Learn more →

Related Articles