Implement IPv4 and CIDR iterators
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem: IPv4 / CIDR Iterator
You are building utilities to iterate through IPv4 addresses.
### Definitions
- An IPv4 address is in dotted decimal form: `a.b.c.d`, where each octet is `0..255`.
- A CIDR block is `ip/prefixLen`, e.g. `192.168.1.0/24`, where `prefixLen` is `0..32`.
### Tasks
Implement functions/classes to support:
1. **Ascending iteration of a single IPv4 address**
- Given an IPv4 address string, return the **next** IPv4 address (i.e., add 1 to the 32-bit value with carry across octets).
- Define behavior when the input is `255.255.255.255` (e.g., error, wrap, or return null) and be consistent.
2. **Descending iteration of a single IPv4 address**
- Given an IPv4 address string, return the **previous** IPv4 address (subtract 1 from the 32-bit value with borrow across octets).
- Define behavior when the input is `0.0.0.0`.
3. **Iteration over a CIDR block**
- Given a CIDR string like `10.0.0.0/30`, produce an iterator (or list/stream) of all IPv4 addresses in that block.
- Specify ordering (ascending is typical).
- Clarify whether you include the network and broadcast addresses; implement accordingly.
### Examples
- `next("1.2.3.4") -> "1.2.3.5"`
- `next("1.2.3.255") -> "1.2.4.0"`
- `prev("1.0.0.0") -> "0.255.255.255"`
- For `"192.168.1.0/30"`, the block size is 4 addresses; iterate all addresses in that range.
### Constraints / Notes
- Assume valid input formatting unless you choose to add validation.
- Aim for O(1) work per `next()`/`prev()` step and O(1) extra space (besides the output).
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.