0
0
Rest APIprogramming~5 mins

Contract testing in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Contract testing
O(n)
Understanding Time Complexity

When we test contracts between services in a REST API, we want to see how the time to verify these contracts changes as the number of API calls grows.

We ask: How does the testing time grow when we add more API endpoints or more test cases?

Scenario Under Consideration

Analyze the time complexity of the following contract testing code snippet.


for (const endpoint of apiEndpoints) {
  const contract = getContractFor(endpoint);
  const response = callApi(endpoint);
  verifyContract(contract, response);
}
    

This code tests each API endpoint by fetching its contract, calling the API, and verifying the response matches the contract.

Identify Repeating Operations
  • Primary operation: Looping through each API endpoint to test its contract.
  • How many times: Once for every endpoint in the list.
How Execution Grows With Input

As the number of API endpoints increases, the total testing time grows in a straight line.

Input Size (n)Approx. Operations
1010 contract checks
100100 contract checks
10001000 contract checks

Pattern observation: Doubling the number of endpoints doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the testing time grows directly in proportion to the number of API endpoints tested.

Common Mistake

[X] Wrong: "Testing one endpoint means testing all endpoints at once, so time stays the same no matter how many endpoints there are."

[OK] Correct: Each endpoint requires its own call and check, so more endpoints mean more work and more time.

Interview Connect

Understanding how testing time grows with the number of API endpoints helps you design efficient tests and explain your reasoning clearly in interviews.

Self-Check

"What if we added nested contract checks inside each endpoint test? How would the time complexity change?"