PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of IPv4 address encoding, binary and bitwise operations, and optimal interval covering for expressing consecutive addresses as CIDR blocks. It is commonly asked in the Coding & Algorithms domain to assess practical algorithmic skills in data representation and alignment constraints, focusing on practical application rather than purely conceptual theory.

  • hard
  • Databricks
  • Coding & Algorithms
  • Software Engineer

Convert an IP range to minimal CIDRs

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

## Problem Given a starting IPv4 address `ip` and a non-negative integer `n`, output the **smallest possible list of CIDR blocks** that exactly covers **`n` consecutive IPv4 addresses**, starting from `ip`. A CIDR block is written as `a.b.c.d/k`, representing a block of size `2^(32-k)` addresses aligned on that boundary. ### Input - `ip`: string, a valid IPv4 address in dotted-decimal notation. - `n`: integer, number of consecutive addresses to cover. ### Output - A list of CIDR strings that cover exactly the range `[ip, ip + n - 1]` with the **minimum number of blocks**. ### Example - Input: `ip = "255.0.0.7"`, `n = 10` - Output (one valid minimal answer): - `255.0.0.7/32` - `255.0.0.8/29` - `255.0.0.16/32` ### Constraints - `0 <= n <= 2^32` - Assume arithmetic is within IPv4 range (wrap-around not required).

Quick Answer: This question evaluates understanding of IPv4 address encoding, binary and bitwise operations, and optimal interval covering for expressing consecutive addresses as CIDR blocks. It is commonly asked in the Coding & Algorithms domain to assess practical algorithmic skills in data representation and alignment constraints, focusing on practical application rather than purely conceptual theory.

Given a starting IPv4 address `ip` and a non-negative integer `n`, return the smallest possible list of CIDR blocks that exactly covers `n` consecutive IPv4 addresses starting from `ip`. A CIDR block is written as `a.b.c.d/k`, where the block size is `2^(32-k)` addresses and the starting address must be aligned to that block size. Your task is to cover the range `[ip, ip + n - 1]` using the minimum number of CIDR blocks. Return the blocks in ascending address order. If `n` is `0`, return an empty list.

Constraints

  • 0 <= n <= 2^32
  • `ip` is a valid IPv4 address
  • The range from `ip` to `ip + n - 1` stays within the IPv4 address space
  • Use 64-bit-safe arithmetic when needed, since `n` can be as large as 2^32

Examples

Input: ("255.0.0.7", 10)

Expected Output: ["255.0.0.7/32", "255.0.0.8/29", "255.0.0.16/32"]

Explanation: The address 255.0.0.7 is not aligned for a larger block, then 255.0.0.8 can take 8 addresses as /29, and 255.0.0.16 covers the final single address.

Input: ("0.0.0.0", 0)

Expected Output: []

Explanation: No addresses need to be covered, so the minimal answer is an empty list.

Input: ("192.168.1.0", 256)

Expected Output: ["192.168.1.0/24"]

Explanation: The start is aligned to a /24 boundary, and 256 addresses exactly match one /24 block.

Input: ("192.168.1.5", 4)

Expected Output: ["192.168.1.5/32", "192.168.1.6/31", "192.168.1.8/32"]

Explanation: The range covers .5 through .8. Because .5 is unaligned, it must be a /32. Then .6-.7 can be a /31, and .8 is the last /32.

Input: ("0.0.0.1", 3)

Expected Output: ["0.0.0.1/32", "0.0.0.2/31"]

Explanation: The first odd address must be a /32, then the next two aligned addresses can be grouped into a /31.

Input: ("10.0.0.255", 2)

Expected Output: ["10.0.0.255/32", "10.0.1.0/32"]

Explanation: The range crosses an octet boundary. Both addresses are singletons because the start is not aligned for a 2-address block.

Input: ("0.0.0.0", 4294967296)

Expected Output: ["0.0.0.0/0"]

Explanation: Covering the entire IPv4 address space requires exactly one /0 block.

Hints

  1. It is often easier to solve this by converting the IPv4 address into a 32-bit integer and working with integer ranges.
  2. At each step, take the largest power-of-two-sized block that is both aligned at the current start address and does not exceed the remaining number of addresses.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Design In-Memory QPS Counter - Databricks (medium)
  • Implement Random Connectivity and Grid Routing - Databricks
  • Implement a Snapshot Set Iterator - Databricks (hard)
  • Find Fastest Commute Mode - Databricks (hard)
  • Solve Grid Path and Graph Sampling - Databricks (medium)