0
0
C Sharp (C#)programming~5 mins

Multiple interface implementation in C Sharp (C#) - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Multiple interface implementation
O(n)
Understanding Time Complexity

When a class implements multiple interfaces, it must provide code for each interface's methods. We want to understand how this affects the time it takes to run the program.

How does adding more interfaces change the work the program does?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


interface IA { void MethodA(); }
interface IB { void MethodB(); }

class MyClass : IA, IB {
    private int n;
    public MyClass(int n) { this.n = n; }
    public void MethodA() {
        for (int i = 0; i < n; i++) {
            // some operation
        }
    }
    public void MethodB() {
        for (int j = 0; j < n; j++) {
            // some operation
        }
    }
}
    

This class implements two interfaces, each with a method that loops n times doing some work.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Two separate loops, each running n times.
  • How many times: Each loop runs independently n times when its method is called.
How Execution Grows With Input

Each method runs a loop that grows with n. If both methods are called once, total work is about 2 times n.

Input Size (n)Approx. Operations
10About 20 operations (10 + 10)
100About 200 operations (100 + 100)
1000About 2000 operations (1000 + 1000)

Pattern observation: The total work grows linearly with n, doubling because of two methods.

Final Time Complexity

Time Complexity: O(n)

This means the total work grows in a straight line as n grows, even with multiple interfaces.

Common Mistake

[X] Wrong: "Implementing more interfaces always makes the program slower by a lot."

[OK] Correct: Each interface method runs separately, so total work adds up linearly, not exponentially or worse.

Interview Connect

Understanding how multiple interfaces affect time helps you explain how your code scales. It shows you can think about how adding features changes performance.

Self-Check

"What if each interface method called the other method inside its loop? How would the time complexity change?"