Problem · String

CIDR IPv4 Range Iterator

Learn this problem
MediumOpenAIFULLTIMEPHONE SCREEN

Problem statement

You are given an IPv4 address in CIDR notation, such as 192.168.1.0/24. The prefix length fixes the leading bits of the 32-bit address; the remaining bits identify addresses inside the network.

Use the prefix mask to compute the inclusive network start and end addresses, then visit every IPv4 address in that range in increasing order.

Interview context

The third phone-screen part supplied a CIDR string and asked candidates to calculate its start and end addresses with bitwise operations, then iterate over the full network range.

Function

iterateCIDR(cidr: String) → String[]

Examples

Example 1

cidr = "192.168.1.5/30"return = ["192.168.1.4", "192.168.1.5", "192.168.1.6", "192.168.1.7"]

A /30 prefix leaves two host bits. Masking 192.168.1.5 gives network start 192.168.1.4 and network end 192.168.1.7.

More OpenAI problems

drafts saved locally
public String[] iterateCIDR(String cidr) {
  // write your code here
}
cidr"192.168.1.5/30"
expected["192.168.1.4", "192.168.1.5", "192.168.1.6", "192.168.1.7"]
checking account