yoklainterview sim

Python Backend Interview Questions

450 verified Python Backend interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Python Concurrency AsyncDifficulty 1
import threading

def greet(name):
    print(f"hello {name}")

t = threading.Thread(target=greet, args=("ada",))
t.start()
t.join()
print("done")

What does this print?
  • aIt raises TypeError because args must be a dict, not a tuple
  • b"done" then "hello ada", since the main thread doesn't wait for t by default
  • c"hello ada" then "done" — join() blocks until t finishes
  • dIt prints nothing because target= expects a bound function call, not a reference
Explanation:threading.Thread(target=greet, args=("ada",)) schedules greet("ada") to run in a new thread once start() is called. join() blocks the calling thread until t finishes, so "hello ada" is guaranteed to print before "done". args takes a tuple/iterable of positional arguments, not a dict.
Python Concurrency AsyncDifficulty 1
import asyncio

async def compute():
    print("computing")
    return 42

result = compute()
print(result)

What happens?
  • aIt prints something like <coroutine object compute at 0x...>; "computing" is never printed
  • bIt raises TypeError because compute() must be called with await
  • cIt prints "computing" immediately, then 42 right after
  • dIt prints 42 immediately, since compute() runs synchronously
Explanation:Calling an async def function does not execute its body — it creates and returns a coroutine object. The body only starts running when the coroutine is awaited (e.g. inside asyncio.run(compute()) or await compute()). Since result is never awaited here, "computing" never prints, and Python may also warn "coroutine was never awaited".
Python Concurrency AsyncDifficulty 1
import asyncio

async def main():
    print("start")
    await asyncio.sleep(0.1)
    print("end")

asyncio.run(main())

What does running this script print?
  • a"end" then "start"
  • bIt raises RuntimeError because main() has no return value
  • cOnly "start"; asyncio.run does not wait for the coroutine to finish
  • d"start" then "end", with a short pause in between
Explanation:asyncio.run(main()) creates a new event loop, runs the main() coroutine to completion, and closes the loop. Execution proceeds top to bottom: "start" prints, then await asyncio.sleep(0.1) suspends main for roughly 100ms without blocking anything else, then "end" prints.
Python Concurrency AsyncDifficulty 1
import threading

def worker():
    print("working")

t = threading.Thread(target=worker)
t.start()
t.join()
print("main continues")

What is guaranteed about the order of the two prints?
  • a"main continues" always prints first because the main thread has priority
  • b"working" is guaranteed to print before "main continues"
  • cThe order is undefined even with join(), since threads are scheduled randomly
  • dNothing prints because join() must be called before start()
Explanation:t.join() blocks the calling (main) thread until t has fully finished running. Since "main continues" is only printed after join() returns, and join() cannot return until worker() has completed (and thus printed "working"), the ordering is guaranteed.
Python Concurrency AsyncDifficulty 1
In CPython, what is the Global Interpreter Lock (GIL)?
  • aA mutex allowing only one thread to execute Python bytecode at a time
  • bA lock that prevents more than one process from running Python at the same time on a single machine
  • cA lock automatically acquired by every with statement to make code thread-safe
  • dA configuration flag that disables multithreading entirely in the threading module
Explanation:The GIL is a mutex internal to the CPython interpreter that ensures only one thread executes Python bytecode at any given moment, even on multi-core machines. It exists to simplify memory management (reference counting) but limits true parallel execution of pure-Python code across threads within one process.
Python Concurrency AsyncDifficulty 1
How does asyncio achieve concurrency for multiple coroutines?
  • aBy assigning each coroutine to its own OS thread automatically
  • bBy spawning a separate process for every async def function
  • cBy running them cooperatively on a single thread, switching between coroutines at await points
  • dBy using the GIL to run coroutines truly in parallel on multiple cores
Explanation:asyncio runs an event loop on a single thread. Coroutines cooperate by voluntarily yielding control at await points; the event loop then picks another ready coroutine to run. No preemption and no extra OS threads or processes are involved by default — concurrency here means interleaving, not parallel execution.

Test yourself against the 3300-question Backend bank.

Start interview