Python interview questions, with the code explained.
Practice a Python coding question and a classic concept question, free, with full answers and the follow-ups interviewers ask.
Published November 1, 2026
Python interviews usually check two things: can you solve a small problem cleanly, and do you understand how the language behaves. Here’s one of each.
Free question 1 (coding): First character that doesn’t repeat
Write a function that takes a string and returns the index of the first character that appears only once. If there isn’t one, return -1. For “swiss”, return 1 (the “w”).
Model answer
Say the plan first: “I’ll count every character in one pass, then walk the string again and return the first one with a count of 1.”
from collections import Counter
def first_unique(s):
counts = Counter(s)
for i, ch in enumerate(s):
if counts[ch] == 1:
return i
return -1
Walk-through for “swiss”: the counts are s: 3, w: 1, i: 1. Walking the string, “s” has 3, then “w” has 1, so return 1.
Complexity: O(n) time, two passes over the string. Extra space is O(k), where k is the number of different characters.
Edge cases to mention: an empty string returns -1; a string where every character repeats, like “aabb”, returns -1; ask whether “A” and “a” count as the same character.
Follow-up you may get: “Can you do it without Counter?” Use a normal dictionary: counts[ch] = counts.get(ch, 0) + 1.
What interviewers listen for: you state the plan before typing, you pick a dictionary instead of nested loops (which would be O(n squared)), and you test with the example.
Free question 2 (concept): Why does this function remember old values?
What does this print, and why?
def add_item(item, items=[]):
items.append(item)
return items
print(add_item("a"))
print(add_item("b"))
Model answer
It prints [‘a’] and then [‘a’, ‘b’], not [‘b’].
Why: Python creates a default value once, when the function is defined, not each time it’s called. So every call that doesn’t pass items shares the same list. The first call adds “a” to it, and the second call adds “b” to that same list.
The fix: use None as the default and create a new list inside the function.
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items
Then connect it to the bigger idea: this only bites with mutable defaults, like lists, dictionaries and sets, because they can be changed in place. Numbers, strings and tuples are immutable, so they’re safe as defaults.
Follow-up you may get: “What’s the difference between a list and a tuple?” A list can be changed after it’s made. A tuple can’t. Because of that, a tuple of numbers or strings can be used as a dictionary key, and a list can’t.
What interviewers listen for: you explain when default values are created, you give the standard fix, and you link it to mutable versus immutable types.
The full Python interview guide
One printable PDF. Part 1 is the prep section: what to wear, when to arrive or log in, key topics to review for this role, questions to ask, talking about pay, a story bank, the day before and the day itself, and a thank you note. Part 2 is 20 questions for this role, each with a possible answer and lined space for your notes. A checklist for the night before is included. Print it on US Letter and write in it.
- A full prep section: what to wear, when to arrive or log in, key topics to review, questions to ask, talking about pay, a story bank, the day before and the day itself, and a thank you note
- 6 coding questions: strings, lists, dictionaries, sorting, recursion and a simple class, with time and space notes
- 4 concept questions: generators, decorators, copying objects and error handling
- 5 behavioural questions with sample STAR answers, plus lines for your own stories
- 5 situational questions, such as a bug in production or unclear requirements
- A checklist for the night before
One PDF, $24 CAD / $17 USD, paid once on this page and emailed to you. Visitors in Canada pay $24 CAD. Everyone else pays $17 USD.
About these questions
Are these Python interview questions for jobs in Canada?
Yes. Python questions come up in Canadian interviews for developer, data analyst, data scientist and automation roles. Most start with a short coding problem, then move to how Python itself works, like the two questions here.
Is there a full interview guide?
Yes. The full printable Python interview guide is $24 CAD / $17 USD. You pay on this page and we email you the PDF. It has a prep section (what to wear, when to arrive or log in, key topics for this role, questions to ask, talking about pay, a story bank, the day before and the day itself, and a thank you note), 20 Python questions with possible answers and lined space for notes, and a checklist for the night before. The two questions on this page are free to use now.
Not getting interviews yet? Our software and IT resumé help shows what hiring managers screen for first.
More roles: interview questions by role.
Don't see your job? Tell us.
Tell us the job you're interviewing for, and we'll write interview questions and model answers for it. It's free to ask. We read every request. We can't promise to add every role, but the most requested ones go first.
Thanks. We got your request.