0
0
Computer Networksknowledge~5 mins

Private vs public IP addresses in Computer Networks - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Private vs public IP addresses
O(1)
Understanding Time Complexity

When working with IP addresses, it helps to understand how the number of addresses affects network operations.

We want to see how the size of private and public IP address ranges impacts tasks like address allocation and routing.

Scenario Under Consideration

Analyze the time complexity of checking if an IP address is private or public.


function isPrivateIP(ip) {  // ip: 32-bit integer representation
  const privateRanges = [
    [167772160, 184549375],     // 10.0.0.0 - 10.255.255.255
    [2886729728, 2887778303],   // 172.16.0.0 - 172.31.255.255
    [3232235520, 3232301055]    // 192.168.0.0 - 192.168.255.255
  ];
  for (const range of privateRanges) {
    if (ip >= range[0] && ip <= range[1]) {
      return true;
    }
  }
  return false;
}

This code checks if an IP address falls within any private IP range by comparing it to known ranges.

Identify Repeating Operations

Look at what repeats when checking an IP address.

  • Primary operation: Looping through the list of private IP ranges.
  • How many times: Exactly 3 times, once for each private range.
How Execution Grows With Input

The number of private IP ranges is fixed and small, so checking an IP address takes about the same time no matter what.

Input Size (number of IPs checked)Approx. Operations per IP
103 checks each
1003 checks each
10003 checks each

Pattern observation: The time to check one IP does not grow with input size; it stays constant.

Final Time Complexity

Time Complexity: O(1)

This means checking if an IP is private or public takes the same amount of time regardless of the IP address.

Common Mistake

[X] Wrong: "Checking if an IP is private gets slower as more IPs exist."

[OK] Correct: The code only checks a fixed small list of ranges, so the time does not depend on how many IPs there are in total.

Interview Connect

Understanding how fixed-size checks work helps you explain network address handling clearly and confidently in interviews.

Self-Check

What if the list of private IP ranges grew to hundreds? How would that affect the time complexity?