CIDR blocks and IP addressing in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with CIDR blocks in AWS, it is important to understand how the number of IP addresses affects operations like subnet creation and IP allocation.
We want to know how the effort or steps grow as the size of the CIDR block changes.
Analyze the time complexity of allocating IP addresses within a CIDR block.
// Example: Allocate IP addresses from a CIDR block
const cidrBlock = "10.0.0.0/24";
const totalIPs = 256; // Number of IPs in /24
for (let i = 0; i < totalIPs; i++) {
allocateIPAddress(cidrBlock, i);
}
This sequence simulates allocating each IP address inside a CIDR block one by one.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: IP address allocation call inside the CIDR block.
- How many times: Once for each IP address in the block.
As the CIDR block size grows, the number of IP addresses increases exponentially, so the number of allocation operations grows proportionally.
| Input Size (CIDR block size) | Approx. IPs / Operations |
|---|---|
| /28 (16 IPs) | 16 |
| /24 (256 IPs) | 256 |
| /16 (65,536 IPs) | 65,536 |
Pattern observation: The number of operations grows linearly with the number of IP addresses in the CIDR block.
Time Complexity: O(n)
This means the time to allocate IPs grows directly in proportion to the number of IP addresses in the CIDR block.
[X] Wrong: "Allocating IPs from a CIDR block takes the same time no matter how big the block is."
[OK] Correct: Larger CIDR blocks have more IP addresses, so allocating each one takes more steps, increasing total time.
Understanding how IP allocation scales with CIDR block size shows you can reason about resource limits and efficiency in cloud networking.
What if we changed from allocating IPs one by one to allocating them in batches? How would the time complexity change?
Practice
192.168.1.0/24 represent in AWS networking?Solution
Step 1: Understand CIDR notation
The number after the slash (/24) shows how many bits are fixed for the network part. Here, 24 bits fixed means the first 3 parts (192.168.1) are fixed.Step 2: Calculate the IP range
With 24 bits fixed, the last 8 bits can vary from 0 to 255, so the range is 192.168.1.0 to 192.168.1.255.Final Answer:
A range of IP addresses from 192.168.1.0 to 192.168.1.255 -> Option DQuick Check:
CIDR /24 means 256 addresses [OK]
- Confusing CIDR with a single IP
- Misreading the subnet mask bits
- Assuming /24 means only 24 addresses
Solution
Step 1: Calculate bits needed for 512 addresses
512 addresses require 9 bits (2^9 = 512) for host part.Step 2: Determine CIDR prefix
IPv4 has 32 bits total, so prefix = 32 - 9 = 23. So CIDR is /23.Final Answer:
/23 -> Option AQuick Check:
512 IPs = 2^(32-23) = 512 [OK]
- Choosing /24 which gives only 256 addresses
- Confusing /22 with 1024 addresses
- Miscounting bits for hosts
10.0.0.0/26, how many usable IP addresses are available for hosts?Solution
Step 1: Calculate total IPs in /26 block
/26 means 32 - 26 = 6 bits for hosts, so total IPs = 2^6 = 64.Step 2: Subtract network and broadcast addresses
Two addresses are reserved (network and broadcast), so usable IPs = 64 - 2 = 62.Final Answer:
62 -> Option BQuick Check:
Usable IPs = total - 2 [OK]
- Counting all IPs as usable
- Forgetting to subtract network and broadcast
- Mixing up prefix length and host bits
172.16.0.0/16. You want to create two subnets without overlapping IPs. Which pair of CIDR blocks is valid?Solution
Step 1: Understand the VPC range
172.16.0.0/16 covers IPs from 172.16.0.0 to 172.16.255.255.Step 2: Check subnet ranges for overlap
/17 splits the /16 into two halves: 172.16.0.0 to 172.16.127.255 and 172.16.128.0 to 172.16.255.255. These do not overlap.Final Answer:
172.16.0.0/17 and 172.16.128.0/17 -> Option AQuick Check:
Non-overlapping halves split /16 into two /17s [OK]
- Choosing overlapping CIDRs
- Using larger CIDR than VPC block
- Ignoring subnet mask sizes
10.0.0.0/24 without overlap?Solution
Step 1: Calculate needed CIDR for each subnet
100 IPs need at least /25 (128 IPs), 50 IPs need /26 (64 IPs), 25 IPs need /27 (32 IPs).Step 2: Assign CIDRs inside 10.0.0.0/24 without overlap
10.0.0.0/25 covers 0-127, 10.0.0.128/26 covers 128-191, 10.0.0.192/27 covers 192-223. These fit perfectly without overlap.Final Answer:
10.0.0.0/25, 10.0.0.128/26, 10.0.0.192/27 -> Option CQuick Check:
Subnet sizes fit and sum within /24 [OK]
- Using CIDRs too small for IP needs
- Overlapping subnet ranges
- Assigning subnets outside VPC CIDR
