Cover an IPv4 Address Range With the Fewest CIDR Blocks

Last updated: November 29, 2026

Quick Overview

Given a first and last IPv4 address (inclusive), return the smallest list of CIDR blocks that covers exactly that range and nothing else. This is the kind of conversion needed when writing allowlist or blocklist rules for an edge firewall.

Airbnb
Coding & Algorithms
Software Engineer
Airbnb
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Hard

0

0

0 solved


Given a first and last IPv4 address (inclusive), return the smallest list of CIDR blocks that covers exactly that range and nothing else. This is the kind of conversion needed when writing allowlist or blocklist rules for an edge firewall.

Airbnb's infrastructure and security teams manage allowlists for partner integrations and block abusive scraping ranges, and firewall rules only accept CIDR notation. The interviewer uses it to test comfort with bit manipulation, alignment, and off-by-one reasoning on inclusive ranges.

What the Interviewer Expects
  • Convert dotted quads to 32-bit integers and back
  • Use the lowest set bit of the start to find the largest aligned block
  • Shrink the block until it fits in the remaining range
  • Handle the edge case where the start is 0
  • Argue why the greedy choice gives the minimum count
Key Topics to Cover
bit manipulation
CIDR
greedy
networking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support IPv6 with 128-bit addresses?
  • Given a list of overlapping ranges, how would you produce a minimal merged CIDR set?
  • How do you test this thoroughly?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: two IPv4 strings with first <= last. Output: CIDR strings like 10.0.0.0/24, covering every address in [first, last] and no others, using as few blocks as possible....

High-Level Design

A /p block holds 2^(32-p) addresses and must start at a multiple of its size. From the current start lo, the largest block that can begin there has size lo & -lo (the lowest set bit). It may overs...


Submit Your Answer
Markdown supported