Started with the CIDR case and immediately fumbled explaining how to extract the host range from the prefix length.
Start by clarifying requirements and edge cases, then design a memory-efficient iterator using integer representation of IP addresses. Explain the conversion between IP string and integer, and how to increment and check bounds without storing all addresses. Discuss handling of CIDR notation and potential pitfalls like overflow and invalid inputs.
Pro tip: Mention that you would use unsigned 32-bit integers to avoid sign issues and that you can precompute the end address for CIDR to simplify the iteration logic. Also, consider thread-safety if the iterator might be used concurrently.
Ask about input formats, expected behavior for invalid inputs, inclusivity of endpoints, and whether the iterator needs to be thread-safe. Confirm that memory efficiency is critical.
Define next() and hasNext() methods. Internally, store the current IP as a 32-bit integer and the end IP as a 32-bit integer. For CIDR, compute the start and end addresses from the prefix.
Write helper functions to convert IP string to integer and vice versa. In next(), return the current IP and increment it; in hasNext(), check if current <= end. Handle overflow when incrementing past 255.255.255.255.
Parse CIDR to get base IP and prefix length, then compute the network address and broadcast address. Ensure the range is correct and handle cases like /0 and /32. Validate inputs and throw exceptions for invalid formats.
Compare integer-based iteration vs. string manipulation. Mention that integer approach is O(1) memory and O(1) per next() call. Consider if lazy evaluation is needed and how to handle very large ranges (e.g., /0) without performance issues.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.