← Openai Interview Insights

Openai·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Interviewed for a software engineering role at OpenAI and got a bit-manipulation problem around CIDR blocks. Niche enough that I had to actually think, not just pattern-match to something I'd drilled before.

Questions Asked (1)

Q1

Given a CIDR block like 10.0.0.0/24, write code to enumerate all IPv4 addresses it contains in order, using bit manipulation to derive the network address and iterate through all host-bit combinations.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew what a CIDR block was but hadn't thought about it in terms of raw bit ops in a while.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, parse the CIDR string to extract the IP address and prefix length. Then, compute the network address by masking the IP with the subnet mask, and determine the number of host bits. Finally, iterate from 0 to 2^(host bits) - 1, adding each value to the network address to generate all IPs in order.

Pro tip: Clarify whether to include network and broadcast addresses; in most contexts, enumerating all addresses in the block includes them, but some may expect only usable hosts. Also, mention that for large blocks (e.g., /8), enumeration may be impractical, so consider lazy generation or streaming.

1. Parse the CIDR notation

Split the input string on '/' to separate the IP address and prefix length. Convert the IP address into a 32-bit integer.

2. Compute network address and host bits

Calculate the subnet mask from the prefix length. Perform a bitwise AND between the IP and mask to get the network address. Compute the number of host bits as 32 minus the prefix length.

3. Determine the range of host values

The number of addresses is 2^(host bits). Iterate from 0 to that number minus 1, representing all possible host-bit combinations.

4. Generate and output IP addresses

For each host value, add it to the network address (bitwise OR) to get the current IP as an integer. Convert the integer back to dotted-decimal format and output or store it.

5. Handle edge cases and efficiency

Consider edge cases like /32 (single address) and /31 (two addresses). For large blocks, discuss memory and time trade-offs, possibly using a generator to yield addresses lazily.

Key Points to Mention

  • Bitwise operations: AND for network address, OR for adding host bits.
  • Subnet mask calculation: (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF.
  • Number of addresses: 2^(32 - prefix).
  • Conversion between integer and dotted-decimal IP format.
  • Inclusion of network and broadcast addresses (unless specified otherwise).
  • Efficiency considerations for large CIDR blocks (e.g., /8 has 16M addresses).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.