Private vs public IP addresses in Computer Networks - Performance Comparison
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.
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.
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.
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 |
|---|---|
| 10 | 3 checks each |
| 100 | 3 checks each |
| 1000 | 3 checks each |
Pattern observation: The time to check one IP does not grow with input size; it stays constant.
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.
[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.
Understanding how fixed-size checks work helps you explain network address handling clearly and confidently in interviews.
What if the list of private IP ranges grew to hundreds? How would that affect the time complexity?