QA
Senior LevelData Structures & Algorithms

Explain Amortised Analysis with Aggregate, Accounting, and Potential methods.

ā“ Interview Prompt: Describe the three methods of amortised analysis — Aggregate, Accounting (Banker's), and Potential (Physicist's) — with examples.

Amortised Analysis assigns average performance per operation over a sequence of operations, even if individual operations are expensive.

1. Aggregate Method:

Total cost of $N$ operations / $N$ = amortised cost.

•
Example: Dynamic array push — $N$ pushes cost $O(N)$ total (occasional $O(N)$ doubling) → $O(1)$ amortised.

2. Accounting (Banker's) Method:

Assign credits to cheap operations to "bank" for future expensive ones.

•
Dynamic array: charge 3 per push (1 for this push, 2 saved to pay for future copy).

3. Potential Method:

Define a potential function $\Phi$ representing "stored energy".

•
Amortised cost = actual cost + $\Delta\Phi$.
•
$\sum$ amortised cost $\geq$ $\sum$ actual cost (valid analysis).

Code Implementation Example

typescript
// Dynamic Array — O(1) amortised push (Aggregate method proof)
class DynamicArray<T> {
  private data: T[] = new Array(1);
  private size = 0;

  push(val: T): void {
    if (this.size === this.data.length) {
      // Double capacity — O(N) but amortised O(1) since done every N pushes
      const newData = new Array(this.data.length * 2);
      for (let i = 0; i < this.size; i++) newData[i] = this.data[i];
      this.data = newData;
    }
    this.data[this.size++] = val; // O(1) most of the time
  }
}
// Total cost of N pushes: 1+1+2+1+1+1+1+4+... = O(N) → O(1) amortised
šŸ’”Dynamic array doubling strategy — O(1) amortised push via aggregate analysis.
šŸŽÆ Interview Key Takeaway

Amortised O(1) is crucial for understanding dynamic arrays, Union-Find, splay trees, and many advanced data structures used in production systems.

#DSA#Amortised Analysis#Theory#Complexity#Senior
Back to all Data Structures & Algorithms Questions→

More Data Structures & Algorithms Interview Questions

Beginner LevelData Structures & Algorithms

What is the difference between an Array and a Linked List?

Beginner LevelData Structures & Algorithms

What is Big-O Notation and why is it important?

Beginner LevelData Structures & Algorithms

What are Stack and Queue data structures and where are they used?