IP Address to CIDR Blocks
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
OpenAI's May OA asks you to convert a range of consecutive IPv4 addresses into the minimum set of CIDR blocks. You're given a starting IP and a count, and you need to return a list of CIDR notation strings that cover exactly that range, no more, no less. This is a bit manipulation and greedy problem disguised as networking. StealthCoder can generate the solution in real time if you blank on the bit-math details during the live assessment.
The problem
Given a starting IPv4 address startIp and a positive integer count, return the minimum set of CIDR blocks that exactly covers the count consecutive IPv4 addresses starting at startIp. Each CIDR block must be written in the form a.b.c.d/prefix. The returned blocks must cover the range exactly and use the minimum possible number of blocks. You may assume the input is a valid IPv4 address and that the range stays within the IPv4 space. Function Description Complete the function ipRangeToCidrBlocks in the editor below. ipRangeToCidrBlocks has the following parameters: Returns String[]: the minimum CIDR cover for the range. A single IPv4 address is represented by a /32 CIDR block. The range from 0.0.0.0 through 0.0.0.255 is exactly one /24 block.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to greedily split the IP range by aligning block boundaries and maximizing block size at each step. Convert the starting IP to a 32-bit integer, then repeatedly find the largest CIDR block (smallest prefix) that fits within the remaining count without crossing a boundary misaligned to that block's size. A /24 covers 256 addresses, /25 covers 128, /26 covers 64, and so on. The key insight: a CIDR block of size 2^(32-prefix) can only start at an address divisible by that size. Use bit operations to check alignment, then greedily assign the largest valid block each iteration. Most candidates miss the boundary-alignment check and over-segment. StealthCoder handles the integer arithmetic and mask logic so you don't risk off-by-one errors under pressure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill IP Address to CIDR Blocks cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
IP Address to CIDR Blocks FAQ
How do I convert an IP string to an integer?+
Split by '.', parse each octet as an int, then combine: (a << 24) | (b << 16) | (c << 8) | d. Reverse it to get back the IP string: octet 3 is (ip >> 24) & 255, octet 2 is (ip >> 16) & 255, etc.
What does 'exactly covers the count' mean?+
Your CIDR blocks must sum to exactly count IPs, with no overlap and no gaps. A /32 is 1 address, /31 is 2, /30 is 4, /24 is 256. If count is 300, you might use one /23 (512) but that overshoots, so you'd need smaller blocks that sum to 300.
How do I check if an address is aligned to a block size?+
For a /prefix block, the size is 2^(32-prefix). Check if (ip % size) == 0 using modulo, or (ip & (size - 1)) == 0 using bitwise AND. Both work. The second is faster.
Is there a standard approach?+
Yes. While count > 0, find the largest prefix (smallest block) that starts at a boundary aligned to that block's size and doesn't exceed remaining count. Add it, subtract its size from count, increment IP, repeat until count hits zero.
Did OpenAI ask this in earlier cycles?+
This exact problem appears in LeetCode 751. It's a known hard problem. OpenAI uses it because it tests bit manipulation, greedy logic, and the ability to reason about IP structure under time pressure. Expect it again.