1. Junior

    Why should you measure before optimizing performance?

    Because the real bottleneck is rarely where we expect it, and optimizing without measuring adds complexity that may change nothing, or improves a part that is only a tiny share of the total time.

    Read the full explanation
  2. Mid-level

    What is the role of cProfile in performance analysis?

    It records the number of calls of each function, the own time (tottime), and the cumulative time (cumtime), helping you identify the hot paths.

    Read the full explanation
  3. Mid-level

    When do you use collections.deque instead of list?

    Use deque when you need to add or remove elements at both ends in O(1), such as queues and sliding windows, and use list for indexed access and appending at the end.

    Read the full explanation
  4. Junior

    When do generators improve memory use and when do they not improve time?

    Generators lower peak memory use because they produce items on demand, but they do not necessarily speed up processing, and may be slower if you need to pass over the data several times.

    Read the full explanation
  5. Mid-level

    How do you use functools.lru_cache safely?

    It stores function results by their arguments up to a limit (maxsize), and suits pure functions whose arguments are hashable and whose result does not depend on changing external state.

    Read the full explanation
  6. Junior

    Why is str.join usually preferred for assembling strings?

    Because strings are immutable, repeated += in a loop can create a new string at each step at quadratic cost, whereas join computes the total size and builds the result once.

    Read the full explanation
  7. Mid-level

    How do you reduce the cost of inserting many rows into SQLite?

    Batch the inserts into a single transaction and use executemany, instead of running a commit for every row.

    Read the full explanation
  8. Senior

    How do you deal with heavy object creation in a hot loop?

    Measure first, then reduce unnecessary temporary objects, use built-in structures and functions written in C, or move the computation to NumPy or a compiled extension if the profiler shows the loop is the bottleneck.

    Read the full explanation
  9. Mid-level

    What does tracemalloc measure and how do you use it to find memory leaks?

    tracemalloc tracks the memory allocations Python makes and where they happen in the code, and lets you take snapshots and compare them to see which lines' memory is growing.

    Read the full explanation
  10. Senior

    How does the choice of data structure change a program's performance?

    The time complexity of the basic operations determines performance more than any other optimization: a membership check in a set or dict averages O(1), while in a list it is linear O(n), and this difference grows with data size.

    Read the full explanation