Cover an IPv4 address range with the fewest CIDR blocks

Quick Overview

Given a starting IPv4 address and a count of consecutive addresses, return the shortest list of CIDR blocks that covers exactly that range. It tests converting addresses to 32-bit integers, reasoning about block alignment with the lowest set bit, and proving a greedy choice is minimal.

Cover an IPv4 address range with the fewest CIDR blocks

Company: Cloudflare

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

An IP address is a formatted 32-bit unsigned integer where each group of 8 bits is printed as a decimal number and the groups are separated by dots. For example, the binary number `00001111 10001000 11111111 01101011` can be formatted as `"15.136.255.107"`. A CIDR block is a format used to denote a specific set of IP addresses. It is a string consisting of a base IP address, followed by a slash, and then a prefix length `k`. The addresses it covers are all the IPs whose first `k` bits are the same as the base IP address. For example, `"123.45.67.89/20"` is a CIDR block with a prefix length of `20`. Any IP address whose binary representation matches `01111011 00101101 0100xxxx xxxxxxxx` is in the set, where `x` can be either `0` or `1`. Given a start IP address `ip` and an integer `n`, return the minimum list of CIDR blocks that covers all the IP addresses in the range `[ip, ip + n - 1]` (inclusive). Every address in the range must be covered, and no address outside the range may be covered. ### Function Signature ```python def ip_to_cidr(ip: str, n: int) -> list[str]: ``` ### Rules - Treat each address as a 32-bit unsigned integer, so `ip + i` is the address `i` positions after `ip`. - Write each block as `"a.b.c.d/k"`, where `a.b.c.d` is the first address the block covers (every bit after the first `k` is zero) and `k` is the prefix length. A block with prefix length `k` covers ${2^{32-k}}$ addresses. - Return the blocks sorted by their first address, in ascending order. With these two formatting rules the minimum list is unique. ### Constraints - `ip` is a valid IPv4 address. - `1 <= n <= 1000` - Every implied address `ip + i` (for `i` in `[0, n - 1]`) is a valid IPv4 address. ### Examples ```text Input: ip = "255.0.0.7", n = 10 Output: ["255.0.0.7/32", "255.0.0.8/29", "255.0.0.16/32"] ``` The 10 addresses from 255.0.0.7 to 255.0.0.16 are covered by 255.0.0.7/32 (1 address), 255.0.0.8/29 (8 addresses) and 255.0.0.16/32 (1 address), for a total of 1 + 8 + 1 = 10. A larger block cannot start at 255.0.0.7 because it would have to begin at an address before it. ```text Input: ip = "117.145.102.62", n = 8 Output: ["117.145.102.62/31", "117.145.102.64/30", "117.145.102.68/31"] ``` The last octet 62 is a multiple of 2 but not of 4, so the first block can hold only 2 addresses. Then 117.145.102.64/30 covers 4 and 117.145.102.68/31 covers the last 2, for a total of 2 + 4 + 2 = 8. ```text Input: ip = "10.0.0.0", n = 6 Output: ["10.0.0.0/30", "10.0.0.4/31"] ``` Alignment would allow a very large block at 10.0.0.0, but only 6 addresses are needed. A block of 4 covers 10.0.0.0 to 10.0.0.3, and a block of 2 covers 10.0.0.4 and 10.0.0.5.

Overview: Given a starting IPv4 address and a count of consecutive addresses, return the shortest list of CIDR blocks that covers exactly that range. It tests converting addresses to 32-bit integers, reasoning about block alignment with the lowest set bit, and proving a greedy choice is minimal.

Cloudflare logo
Cloudflare
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

An IP address is a formatted 32-bit unsigned integer where each group of 8 bits is printed as a decimal number and the groups are separated by dots. For example, the binary number 00001111 10001000 11111111 01101011 can be formatted as "15.136.255.107".

A CIDR block is a format used to denote a specific set of IP addresses. It is a string consisting of a base IP address, followed by a slash, and then a prefix length k. The addresses it covers are all the IPs whose first k bits are the same as the base IP address. For example, "123.45.67.89/20" is a CIDR block with a prefix length of 20. Any IP address whose binary representation matches 01111011 00101101 0100xxxx xxxxxxxx is in the set, where x can be either 0 or 1.

Given a start IP address ip and an integer n, return the minimum list of CIDR blocks that covers all the IP addresses in the range [ip, ip + n - 1] (inclusive). Every address in the range must be covered, and no address outside the range may be covered.

Function Signature

def ip_to_cidr(ip: str, n: int) -> list[str]:

Rules

  • Treat each address as a 32-bit unsigned integer, so ip + i is the address i positions after ip .
  • Write each block as "a.b.c.d/k" , where a.b.c.d is the first address the block covers (every bit after the first k is zero) and k is the prefix length. A block with prefix length k covers 232−k{2^{32-k}} addresses.
  • Return the blocks sorted by their first address, in ascending order. With these two formatting rules the minimum list is unique.

Constraints

  • ip is a valid IPv4 address.
  • 1 <= n <= 1000
  • Every implied address ip + i (for i in [0, n - 1] ) is a valid IPv4 address.

Examples

Input: ip = "255.0.0.7", n = 10
Output: ["255.0.0.7/32", "255.0.0.8/29", "255.0.0.16/32"]

The 10 addresses from 255.0.0.7 to 255.0.0.16 are covered by 255.0.0.7/32 (1 address), 255.0.0.8/29 (8 addresses) and 255.0.0.16/32 (1 address), for a total of 1 + 8 + 1 = 10. A larger block cannot start at 255.0.0.7 because it would have to begin at an address before it.

Input: ip = "117.145.102.62", n = 8
Output: ["117.145.102.62/31", "117.145.102.64/30", "117.145.102.68/31"]

The last octet 62 is a multiple of 2 but not of 4, so the first block can hold only 2 addresses. Then 117.145.102.64/30 covers 4 and 117.145.102.68/31 covers the last 2, for a total of 2 + 4 + 2 = 8.

Input: ip = "10.0.0.0", n = 6
Output: ["10.0.0.0/30", "10.0.0.4/31"]

Alignment would allow a very large block at 10.0.0.0, but only 6 addresses are needed. A block of 4 covers 10.0.0.0 to 10.0.0.3, and a block of 2 covers 10.0.0.4 and 10.0.0.5.