Expert · Python in the real world · topic 50 of 50 · 30 min
Python interview preparation
Explanation
How the process usually works
- Online test: MCQs on Python output and logic, plus one or two coding problems with a timer.
- Coding round: you solve a problem while an interviewer watches, on a shared editor or paper.
- Technical round: questions about Python concepts and your projects.
- HR round: your background, why this company, notice period and salary expectations.
A method for every coding question
- Repeat the problem in your own words and ask about edge cases (empty list? negative numbers?).
- Work one small example by hand.
- Say a simple solution first, even if slow, and its big-O.
- Improve it if you can (often with a set or dict).
- Write clean code with good names, then test it with your example and an edge case.
Classic problem: two-sum
Given a list of numbers and a target, find the positions of two numbers that add up to the target. The slow way checks every pair (O(n²)). The fast way remembers numbers already seen in a dict (O(n)).
def two_sum(nums, target):
seen = {} # number -> index
for i, n in enumerate(nums):
need = target - n
if need in seen:
return (seen[need], i)
seen[n] = i
return None
print(two_sum([2, 7, 11, 15], 9))Theory questions with short answers
| Question | Short answer |
|---|---|
| List vs tuple? | A list can change (mutable); a tuple cannot (immutable). Tuples suit fixed records and can be dict keys. |
| is vs ==? | == compares values; is checks whether two names point to the very same object. |
| Shallow vs deep copy? | A shallow copy copies the outer list only; inner lists are shared. copy.deepcopy copies everything. |
| *args and **kwargs? | args collects extra positional arguments as a tuple; kwargs collects extra keyword arguments as a dict. |
| What is a generator? | A function with yield that produces values one at a time, saving memory. |
| What is a decorator? | A function that wraps another function to add behaviour, written with @. |
| What is the GIL? | The Global Interpreter Lock lets only one thread run Python code at a time in standard CPython, so threads help with waiting (network, files) more than with heavy maths. |
a = [[1, 2], [3, 4]]
b = a.copy() # shallow
b[0].append(99)
print(a) # [[1, 2, 99], [3, 4]] - inner list is shared
print([1] == [1], [1] is [1])Talk about your projects
Be ready to explain one project in two minutes: the problem, what you built, one hard bug you fixed and what you would improve. The projects from this course (grade calculator, expense tracker, quiz, password checker) are good starting points; extend one and put it on GitHub.
Watch the lesson
Python interview preparation · 1/7
This lesson prepares you for Python job interviews, from the online test to the HR round.
Prefer a video by a teacher? Find free videos on “python interview questions and answers for freshers” on YouTube ↗
In real life: A campus placement coding round
In a campus drive, Karthik gets the question "find two numbers in a list that add up to a target". He first explains the simple double loop and says it is O(n²). Then he suggests a dict to remember numbers he has seen, writes it, and tests it with an empty list too. Even though another candidate typed faster, Karthik is selected because his thinking was clear.
def is_palindrome(text):
clean = text.replace(" ", "").lower()
return clean == clean[::-1]
print(is_palindrome("Never odd or even"))Practice (0/3 done)
Write a function is_palindrome(text) that returns True if text reads the same forwards and backwards, ignoring spaces and capital letters. "Never odd or even" is a palindrome.
FizzBuzz: write a function fizzbuzz(n) that returns a list of strings for the numbers 1 to n. Use "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples of both, and the number itself (as a string) otherwise.
Write a function two_sum(nums, target) that returns a pair (i, j) with i < j where nums[i] + nums[j] == target, or None if there is no such pair. Try to use a dict so the list is read only once.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.