Validate CIDR Strings and Compute Subnet Boundaries

Last updated: November 29, 2026

Quick Overview

Write a function that parses an IPv4 CIDR string, rejects malformed input, and returns the canonical network, whether host bits were set, the first and last address, and the block size.

Arista Networks
Coding & Algorithms
Software Engineer
Arista Networks
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Write a function that parses an IPv4 CIDR string, rejects malformed input, and returns the canonical network, whether host bits were set, the first and last address, and the block size.

Configuration validation is everywhere in EOS and CloudVision, and an interface address like 10.1.1.77/24 must be distinguished from the route 10.1.1.0/24. The interviewer is checking bit masking fluency and careful input validation.

What the Interviewer Expects
  • Reject wrong octet count, non-digits, octets above 255 and leading zeros
  • Reject prefix lengths outside 0 to 32
  • Build the mask with a shift and keep it in 32 bits
  • Derive network with AND and broadcast with OR of the inverted mask
  • Handle /0 and /32 correctly
Key Topics to Cover
bit masking
IPv4 addressing
CIDR
input validation
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 check whether two CIDR blocks overlap?
  • Why might you treat /31 links specially (RFC 3021)?
  • Extend the function to IPv6.
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

Return None for invalid strings. For valid ones, return a dict with network (address with host bits cleared plus length), is_canonical, first, last and size. Leading zeros (10.01.0.0) ar...

High-Level Design

Split on / and ., validate each piece, and pack the octets into a 32-bit integer. The mask is (0xFFFFFFFF << (32 - plen)) & 0xFFFFFFFF: the AND is needed because Python integers do not overflow....


Submit Your Answer
Markdown supported

Related Questions