IP Range to CIDR Blocks
Updated
Problem statement
An IPv4 address is written as four whole numbers from 0 to 255 joined by dots, such as "10.0.0.6". Read the four numbers as the four bytes of one 32-bit number, with the first number as the highest byte. So the address right after "10.0.0.255" is "10.0.1.0".
A CIDR block is written as "a.b.c.d/k", where a.b.c.d is an address and k is a whole number from 0 to 32. It covers every address whose first k bits are the same as the first k bits of a.b.c.d. That is 232 - k addresses in a row. In this problem, the last 32 - k bits of a.b.c.d must all be 0, so a.b.c.d is the first address in the block. For example, "10.0.0.8/30" covers the 4 addresses from "10.0.0.8" to "10.0.0.11".
You are given a start address ip and a count n. Return the shortest list of CIDR blocks that together cover the n addresses ip, ip + 1, ..., ip + n - 1 and no other address. List the blocks in increasing order of their first address.
Function
ipToCIDR(ip: String, n: int) → List<String>Examples
Example 1
ip = "192.168.1.0"n = 256return = ["192.168.1.0/24"]The 256 addresses run from "192.168.1.0" to "192.168.1.255". These are exactly the addresses whose first 24 bits match 192.168.1.0, so one block covers them all.
Example 2
ip = "10.0.0.6"n = 7return = ["10.0.0.6/31","10.0.0.8/30","10.0.0.12/32"]The 7 addresses run from "10.0.0.6" to "10.0.0.12". A block of 4 addresses cannot start at "10.0.0.6", because 6 is not a multiple of 4, so the first block is "10.0.0.6/31", which covers "10.0.0.6" and "10.0.0.7". Next, "10.0.0.8/30" covers "10.0.0.8" to "10.0.0.11"; a block of 8 addresses from "10.0.0.8" would go past "10.0.0.12". Only "10.0.0.12" is left, so it gets its own block, "10.0.0.12/32".
Example 3
ip = "172.16.0.250"n = 10return = ["172.16.0.250/31","172.16.0.252/30","172.16.1.0/30"]The 10 addresses run from "172.16.0.250" to "172.16.1.3", because the address after "172.16.0.255" is "172.16.1.0". The blocks are "172.16.0.250/31" (2 addresses), "172.16.0.252/30" ("172.16.0.252" to "172.16.0.255") and "172.16.1.0/30" ("172.16.1.0" to "172.16.1.3").
Constraints
7 <= ip.length <= 15ipis a valid IPv4 address: four whole numbers from0to255joined by dots, with no extra leading zeros.1 <= n <= 109- The last address,
ip + n - 1, is at most"255.255.255.255".
More Airbnb Phone Screen questions
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Text JustificationPHONE SCREEN · Seen Nov 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
- Smallest Common RegionPHONE SCREEN · Seen Aug 2026
- Longest Chain BookingPHONE SCREEN · Seen Aug 2026
- Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Pacific Atlantic Water FlowPHONE SCREEN · Seen May 2023
Similar questions at other companies
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
- Smallest and Largest Numbers with a Digit ProductJane Street · PHONE SCREENJane Street interview questions
- Maximize Binary XOR with a Set-Bit LimitJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Return Exact Cash Register ChangeBank of America · OA
- XOR MultiplicationMicrosoft · OAMicrosoft interview questions
- Smallest Number With a Given Digit SumAmazon · ONSITE INTERVIEW
- Minimum Operations To Reduce To ZeroUber · OAUber interview questions
Interview reports that mention this question
Airbnb interview experiences
- Airbnb SWE OA: Four-Level Banking System: 1 Experience
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern Phone Interviews: 21 Experiences (Part 7 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)