0
0
Typescriptprogramming~5 mins

Class implementing multiple interfaces in Typescript - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Class implementing multiple interfaces
O(n)
Understanding Time Complexity

When a class implements multiple interfaces, it must provide all the methods those interfaces require.

We want to see how the time to create and use such a class grows as we add more interfaces or methods.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


interface A {
  methodA(): void;
}
interface B {
  methodB(): void;
}

class Multi implements A, B {
  methodA() { console.log('A'); }
  methodB() { console.log('B'); }
}

const obj = new Multi();
obj.methodA();
obj.methodB();
    

This code defines two interfaces and a class that implements both, then calls their methods.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Calling each method once.
  • How many times: Each method runs exactly once per call.
How Execution Grows With Input

Each method runs independently, so adding more methods means more calls.

Input Size (number of methods)Approx. Operations
22 method calls
1010 method calls
100100 method calls

Pattern observation: The total work grows directly with the number of methods called.

Final Time Complexity

Time Complexity: O(n)

This means the time grows in a straight line with the number of methods you call on the class.

Common Mistake

[X] Wrong: "Implementing more interfaces makes the program slower in a complex way."

[OK] Correct: Each method runs independently, so adding interfaces just adds more simple calls, not complicated slowdowns.

Interview Connect

Understanding how multiple interfaces affect method calls helps you explain design choices clearly and shows you know how code scales.

Self-Check

"What if each method called inside the class runs a loop over an array? How would the time complexity change?"