Python interview questions
BetaThe Python interviewers expect: data model, comprehensions, generators, typing and the standard library.
Subtopics
- Data types & mutability7
- Lists, dicts, sets & comprehensions8
- Functions, closures & decorators8
- Iterators & generators8
- Classes, dataclasses & dunder methods7
- Exceptions & context managers6
- Modules, packages & imports6
- Type hints & typing6
- async/await & asyncio6
- The GIL, threads & multiprocessing6
- String formatting & encoding6
- Testing with pytest6
Sample questions
- CodingJuniorasync/await & asyncio
Write an async function
double_later(x)that doesawait asyncio.sleep(0)(a no-op await, just to make it a real coroutine that yields control) and then returnsx * 2. Then writeentryPoint(numbers)that runsdouble_laterconcurrently for every - TheoryJuniorasync/await & asyncio
What does defining a function with
async defactually create? If you writeresult = my_coro_func()but neverawaitit, what happens (or does not happen), and what doesawaitdo at the point it appears in the code? - CodingJuniorClasses, dataclasses & dunder methods
Write a class
Pointwith__init__(self, x, y), a dunder method__eq__that treats two points as equal when theirxandymatch, and a dunder method__add__that returns a newPointwhose coordinates are the sums. Then writeentryPoint(a, b), whereaand - TheoryJuniorClasses, dataclasses & dunder methods
Explain the difference between a class attribute and an instance attribute in Python. What goes wrong if a class attribute is a mutable object such as a list, and how do you avoid the problem?
- CodingJuniorData types & mutability
Write
entryPoint(point, dx, dy)wherepointis a two-element list[x, y]. Return a list of two items: the moved point[x + dx, y + dy]and the original point exactly as it was passed in. The input list must not be modified. - CodingJuniorData types & mutability
Write
entryPoint(rows, cols, fill, updates)that builds a grid ofrowsrows andcolscolumns where every cell starts asfill, then applies each update[r, c, value]in order, and returns the grid as a list of row lists. Updates that fall outside the grid are ignored.