2dbi
Home/Databricks/IP to CIDR / Validate IP Ranges
DDatabricks·DSASDE-2Online Assessment

IP to CIDR / Validate IP Ranges

Problem

Given a start IP and a number of IPs to cover, return the minimal list of CIDR blocks; or determine if IP ranges fit a CIDR. Reported Databricks OA.

Example

ip="255.0.0.7", n=10 -> ["255.0.0.7/32","255.0.0.8/29","255.0.0.16/32"]

Constraints

  • Handle 32-bit IPv4 arithmetic and bit alignment

Approach

Bit manipulation: largest aligned block ≤ remaining, bounded by lowest set bit of the current IP.

added 6 days ago
LeadersAccount