How does CPython handle list growth (amortized complexity) to ensure O(1) appends?

Python interview question for Advanced practice.

Answer

It over-allocates memory (e.g., ~1.125x + constant) to avoid frequent reallocations.

Explanation

CPython uses a specific over-allocation algorithm (approximately 1.125x growth) to ensure thatappendoperations are amortized O(1), minimizing expensiverealloccalls.

Related Questions