Aggregate CIDR blocks

A free hard coding practice problem from networking. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.

Difficulty
Hard
Type
Function
Topic
Networking
Languages
JavaScript, TypeScript or Python

Problem

cidrs is a list of IPv4 blocks like "10.0.0.0/24" (host bits in the address may be set — ignore them; blocks may overlap or repeat). Return the SHORTEST list of CIDR blocks that covers exactly the same set of addresses as the union of the input, sorted by network address ascending, each written as "a.b.c.d/p" with host bits cleared. (This minimal list is unique: merge overlapping/adjacent ranges, then cover each merged range with the largest aligned blocks from left to right.) An empty input returns []. Up to 2 000 blocks.

Function signature

// JavaScript
function aggregateCidrs(cidrs)

// TypeScript
function aggregateCidrs(cidrs: any): any

// Python
def aggregate_cidrs(cidrs):

Examples

  1. aggregateCidrs(["10.0.0.0/25","10.0.0.128/25"]) → ["10.0.0.0/24"]
  2. aggregateCidrs(["192.168.0.0/24","192.168.1.0/24","192.168.2.0/24"]) → ["192.168.0.0/23","192.168.2.0/24"]
  3. aggregateCidrs(["10.0.0.0/8","10.20.0.0/16","11.0.0.0/8"]) → ["10.0.0.0/7"]

How checking works

Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.

More practice problems

Next steps