FastPrepFastPrep
Problem Brief

IP Address to CIDR Blocks

FULLTIMEPHONE SCREEN

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:

  1. String startIp: the first IPv4 address in the range
  2. long count: the number of consecutive IPv4 addresses to cover

Returns

String[]: the minimum CIDR cover for the range.

1Example 1

Input
startIp = "0.0.0.0", count = 1
Output
["0.0.0.0/32"]
Explanation

A single IPv4 address is represented by a /32 CIDR block.

2Example 2

Input
startIp = "0.0.0.0", count = 256
Output
["0.0.0.0/24"]
Explanation

The range from 0.0.0.0 through 0.0.0.255 is exactly one /24 block.

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= count <= 2^32
  • startIp is a valid IPv4 address.
  • The answer must cover exactly the requested range using the minimum number of blocks.
public String[] ipRangeToCidrBlocks(String startIp, long count) {
    // write your code here
}
Input

startIp

"0.0.0.0"

count

1

Output

["0.0.0.0/32"]

Sign in to submit your solution.