Implement IPv4 and CIDR iterators
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of IPv4 addressing and CIDR notation, along with competency in binary/bitwise arithmetic, iterator construction, and edge-case handling for address boundaries.
Part 1: Next IPv4 Address
Constraints
- The input is guaranteed to be a valid IPv4 address.
- Each octet is an integer in the range 0 to 255.
- Your algorithm should use O(1) extra space.
Examples
Input: "1.2.3.4"
Expected Output: "1.2.3.5"
Explanation: Simple increment without carry.
Input: "1.2.3.255"
Expected Output: "1.2.4.0"
Explanation: The last octet overflows and carries into the third octet.
Input: "10.0.255.255"
Expected Output: "10.1.0.0"
Explanation: Carry may ripple across multiple octets.
Input: "0.0.0.0"
Expected Output: "0.0.0.1"
Explanation: Lowest normal case.
Input: "255.255.255.255"
Expected Output: None
Explanation: There is no next IPv4 address after the maximum value.
Hints
- Treat the address like a 4-digit number in base 256.
- Work from right to left and stop once you increment an octet that is not 255.
Part 2: Previous IPv4 Address
Constraints
- The input is guaranteed to be a valid IPv4 address.
- Each octet is an integer in the range 0 to 255.
- Your algorithm should use O(1) extra space.
Examples
Input: "1.2.3.5"
Expected Output: "1.2.3.4"
Explanation: Simple decrement without borrow.
Input: "1.0.0.0"
Expected Output: "0.255.255.255"
Explanation: Borrow ripples across three octets.
Input: "0.0.1.0"
Expected Output: "0.0.0.255"
Explanation: Borrow from the third octet into the last octet.
Input: "10.0.0.0"
Expected Output: "9.255.255.255"
Explanation: Borrow can affect multiple octets and reduce a higher octet.
Input: "0.0.0.0"
Expected Output: None
Explanation: There is no previous IPv4 address before the minimum value.
Hints
- This is the reverse of addition with carry: think in terms of borrow in base 256.
- Process octets from right to left until you find one that is greater than 0.
Part 3: Iterate All IPv4 Addresses in a CIDR Block
Constraints
- The input is guaranteed to be a valid CIDR string.
- The block size will not exceed 65536 addresses.
- Include both network and broadcast addresses in the output.
- You may assume the result fits in memory.
Examples
Input: "192.168.1.0/30"
Expected Output: ["192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3"]
Explanation: A /30 block contains 4 total addresses.
Input: "10.0.0.5/30"
Expected Output: ["10.0.0.4", "10.0.0.5", "10.0.0.6", "10.0.0.7"]
Explanation: The IP is normalized to the network address 10.0.0.4 before iteration.
Input: "0.0.0.0/32"
Expected Output: ["0.0.0.0"]
Explanation: A /32 block contains exactly one address.
Input: "255.255.255.254/31"
Expected Output: ["255.255.255.254", "255.255.255.255"]
Explanation: A /31 block contains exactly two addresses.
Hints
- Convert the IPv4 address to a 32-bit integer so masking and iteration are easier.
- The block size of a `/p` network is `2^(32-p)`.