← Openai Interview Insights

Openai·Backend Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Backend coding round at OpenAI that was basically a deep dive into IP address manipulation. One problem, but it had enough layers to keep you busy for the whole session.

Questions Asked (1)

Q1

Given a CIDR block like '192.168.0.0/24', write code to iterate over every IP address in the range. Then flip it: given a starting IP and a count, return the minimal set of CIDR blocks that exactly covers that range. Walk through your bit manipulation logic and handle edge cases at block boundaries.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The first part felt manageable, converting the CIDR prefix length to a mask and stepping through the 32-bit integer space.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the input/output format and constraints (e.g., IPv4 vs IPv6, inclusivity of endpoints). Then implement the forward iteration using integer conversion and bitwise operations, and the reverse using a greedy algorithm that finds the largest aligned block at each step. Walk through the bit manipulation logic and edge cases explicitly.

Pro tip: Mention that the greedy algorithm for minimal CIDR cover is optimal because it always takes the largest possible aligned block, and that this is equivalent to the binary representation of the range. Also, proactively discuss how to handle the edge case where the range spans multiple /8 boundaries or includes 0.0.0.0 or 255.255.255.255.

1. Clarify requirements and constraints

Ask about IP version (IPv4/IPv6), whether the range is inclusive of both start and end, and the expected output format (e.g., list of CIDR strings). Confirm that the CIDR block notation uses prefix length.

2. Implement forward iteration

Convert the CIDR block to a starting IP integer and compute the number of addresses (2^(32-prefix)). Iterate from start to start+count-1, converting each integer back to dotted-decimal. Use bitwise operations to avoid string manipulation.

3. Implement reverse CIDR aggregation

Given start IP and count, use a greedy algorithm: while count > 0, find the largest block size (power of two) that is aligned to the start IP (i.e., start % block_size == 0) and does not exceed count. Emit that CIDR block, then advance start by block_size and decrement count.

4. Walk through bit manipulation logic

Explain how to compute the largest aligned block: the maximum block size is the largest power of two dividing the start IP (i.e., start & -start), capped by the remaining count. The prefix length is 32 - log2(block_size). Show how to compute the network address by masking.

5. Handle edge cases and test

Discuss edge cases: start IP at 0.0.0.0, end IP at 255.255.255.255, count that is a power of two but not aligned, and ranges that cross /8 boundaries. Test with small examples and verify minimality.

Key Points to Mention

  • Conversion between IP string and 32-bit integer using shifts and masks.
  • The greedy algorithm for minimal CIDR cover is optimal because it always takes the largest possible aligned block.
  • Alignment condition: a block of size 2^k must start at an address divisible by 2^k.
  • Computing the largest power of two dividing an integer using x & -x.
  • Prefix length calculation: 32 - log2(block_size).
  • Edge cases: start at 0, end at 2^32-1, and ranges that are not aligned to block boundaries.

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