The IP to CIDR interview question is a networking and bit manipulation challenge. You are given a starting IPv4 address and a number of required IPs n. Your goal is to represent this range of n addresses using the minimum possible number of CIDR (Classless Inter-Domain Routing) blocks. For example, a range of 4 IPs might be represented by one /30 block.
Companies like Airbnb and Databricks ask the IP to CIDR coding problem to test a candidate's mastery of Bit Manipulation and numeric bases. It evaluations your understanding of binary masks and the ability to work with large integers (). It’s a test of how you convert a domain-specific problem (networking) into a generic algorithmic optimization task.
This problem follows the Greedy Bitmasking pattern.
32 - log2(blockSize).n -= blockSize.currentIP += blockSize.IP: 255.0.0.7, .
...00111. Largest power of 2 that divides it is .
255.0.0.7/32. , IP = .8....01000. Largest power of 2 is .
255.0.0.8/29. , IP = .16....10000. Largest power 2 is 16, but .
255.0.0.16/32. .
Result: three CIDR blocks./30, not /2).Practice finding the "Lowest Set Bit" using the x & -x trick. In this problem, lowbit = currentIP & -currentIP tells you the maximum possible block size for the current address. This is a vital Bit Manipulation interview pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Apply Bitwise Operations to Make Strings Equal | Medium | Solve | |
| Encode Number | Medium | Solve | |
| Find The K-th Lucky Number | Medium | Solve | |
| Generalized Abbreviation | Medium | Solve | |
| Generate Binary Strings Without Adjacent Zeros | Medium | Solve |