IPv4 address structure in Computer Networks - Time & Space Complexity
We want to understand how the time to process an IPv4 address changes as the address size or number of addresses grows.
Specifically, how does the work scale when handling IPv4 addresses?
Analyze the time complexity of the following code snippet.
function parseIPv4Address(address) {
const parts = address.split('.');
let result = [];
for (let i = 0; i < parts.length; i++) {
result.push(parseInt(parts[i], 10));
}
return result;
}
This code splits an IPv4 address string into its four parts and converts each part to a number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the four parts of the IPv4 address.
- How many times: Exactly 4 times, once for each part.
Since an IPv4 address always has 4 parts, the number of operations stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 4 (standard IPv4) | 4 operations |
| 10 (hypothetical larger address) | 10 operations |
| 100 (hypothetical larger address) | 100 operations |
Pattern observation: The work grows linearly with the number of parts in the address, but for IPv4 it is fixed at 4 parts.
Time Complexity: O(1)
This means the time to process an IPv4 address does not grow with input size because the address length is fixed.
[X] Wrong: "Parsing an IPv4 address takes longer as more addresses are processed at once."
[OK] Correct: Each IPv4 address is processed independently with a fixed number of steps, so the time per address stays constant.
Understanding fixed-size data like IPv4 addresses helps you explain why some operations are very fast and predictable, a useful skill in networking and programming.
"What if we changed to IPv6 addresses with 8 parts instead of 4? How would the time complexity change?"
Practice
IPv4 address?Solution
Step 1: Understand IPv4 format
An IPv4 address consists of four parts called octets, separated by dots.Step 2: Check the range of each octet
Each octet ranges from 0 to 255, which fits in 8 bits.Final Answer:
Four numbers separated by dots, each from 0 to 255 -> Option CQuick Check:
IPv4 = 4 octets (0-255) [OK]
- Confusing IPv4 with IPv6 format
- Using commas instead of dots
- Incorrect number of parts
IPv4 address?Solution
Step 1: Check each octet's value
Valid IPv4 octets must be between 0 and 255 inclusive.Step 2: Validate each option
A has all octets 0-255 and 4 parts (valid). B: 192.168.1.256 has 256 (invalid), C has 300 (invalid), D has only 3 parts (invalid). Only A is valid.Final Answer:
10.0.0.1 -> Option AQuick Check:
All octets 0-255 and 4 parts = valid IPv4 [OK]
- Allowing numbers above 255
- Ignoring missing octets
- Confusing IPv4 with IPv6
11000000.10101000.00000001.00000001?Solution
Step 1: Convert first octet from binary to decimal
Binary 11000000 equals 128+64=192.Step 2: Confirm decimal value
The first octet decimal value is 192.Final Answer:
192 -> Option DQuick Check:
11000000 binary = 192 decimal [OK]
- Misreading binary digits
- Adding wrong powers of two
- Confusing octet positions
256.100.50.25Solution
Step 1: Check each octet's value range
IPv4 octets must be between 0 and 255 inclusive.Step 2: Identify invalid octet
The first octet is 256, which is outside the valid range.Final Answer:
The first octet is out of range -> Option BQuick Check:
Octet > 255 means invalid IPv4 [OK]
- Thinking 256 is valid
- Confusing separators
- Ignoring octet count
Solution
Step 1: Calculate number of addresses in each range
Range A: 256 addresses (0-255), B: 128 addresses, C: 512 addresses (two full /24 blocks), D: 101 addresses.Step 2: Compare with required 300 devices
Only range C has enough addresses (512) to cover 300 devices.Final Answer:
172.16.0.0 to 172.16.1.255 -> Option AQuick Check:
More than 300 addresses needed, only C fits [OK]
- Assuming 256 addresses cover 300 devices
- Ignoring address range size
- Confusing IP blocks
Ready to prove it?
Pass the test and this pattern moves unknown
