Skip to main content

Time Complexity · #42 · 2026-05-16

What's the Big-O?

JavaScript ·Difficulty 3/3

How to play

Read the code and pick its time complexity from four Big-O choices. Think about loops, recursion, and hidden costs. Press 1–4 or click to answer.

After n insert/delete operations, what is the amortized cost per operation?

class DynamicTable {
  constructor() {
    this.data = new Array(1);
    this.size = 0;
    this.cap = 1;
  }
  insert(val) {
    if (this.size === this.cap) {
      this.cap *= 2;
      const old = this.data;
      this.data = new Array(this.cap);
      for (let i = 0; i < this.size; i++)
        this.data[i] = old[i];
    }
    this.data[this.size++] = val;
  }
  delete() {
    if (this.size === 0) return;
    this.size--;
    if (this.size > 0 && this.size === this.cap / 4) {
      this.cap /= 2;
      const old = this.data;
      this.data = new Array(this.cap);
      for (let i = 0; i < this.size; i++)
        this.data[i] = old[i];
    }
  }
}

Loading your progress...

Press 1 through 4, or tap a numbered choice, to answer. Back to hub