FactSet Interview Question

Apply amortized cost analysis to Arrays vs Vectors.

Interview Answer

Anonymous

Nov 29, 2015

In amortized analysis, you take an average over sequences of operations, so even though the worst case analysis shows that a vector would take O(N) to perform an add(), an amortized analysis would show add() as taking constant time because the worst case only happens every so often in which you have to allocate a new vector and copy all the items over, and the rest of the time the add operation is O(1). In relation to Arrays, assuming a dynamic implementation, the same applies. Adding elements is constant time in the amortized analysis.