Subnet facts from CIDR

A free medium 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
Medium
Type
Function
Topic
Networking
Languages
JavaScript, TypeScript or Python

Problem

cidr is an IPv4 block such as "192.168.10.77/26" (address and prefix length 0–32; the address may have host bits set). Return [network, broadcast, usableHosts]:
- network — the address with all host bits cleared, dotted-decimal
- broadcast — the address with all host bits set, dotted-decimal
- usableHosts — 2^(32−prefix) − 2, except a /31 has 2 (point-to-point link, RFC 3021) and a /32 has 1.

The input is always valid.

Function signature

// JavaScript
function cidrInfo(cidr)

// TypeScript
function cidrInfo(cidr: any): any

// Python
def cidr_info(cidr):

Examples

  1. cidrInfo("192.168.10.77/26") → ["192.168.10.64","192.168.10.127",62]
  2. cidrInfo("10.0.0.0/8") → ["10.0.0.0","10.255.255.255",16777214]
  3. cidrInfo("172.16.5.4/31") → ["172.16.5.4","172.16.5.5",2]

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