Validate IPv4 and IPv6 Addresses in VPC Security Group Rules

Last updated: November 29, 2026

Quick Overview

Given a string from a security group rule, return `IPv4`, `IPv6`, or `Neither`. IPv4 needs four decimal octets 0 to 255 with no leading zeros; IPv6 needs eight groups of 1 to 4 hex digits separated by colons (no `::` shorthand).

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a string from a security group rule, return `IPv4`, `IPv6`, or `Neither`. IPv4 needs four decimal octets 0 to 255 with no leading zeros; IPv6 needs eight groups of 1 to 4 hex digits separated by colons (no `::` shorthand).

A screen question in the style of AWS networking teams, where user-submitted CIDR rules must be validated before they reach the control plane. The interviewer cares about exhaustive edge-case thinking more than cleverness, and whether you avoid regex shortcuts that silently accept bad input.

What the Interviewer Expects
  • Splits on the right delimiter and checks part counts
  • Rejects leading zeros in IPv4 octets but allows 0
  • Rejects empty parts, signs, and whitespace
  • Checks IPv6 group length and hex alphabet
  • Explains why int() alone is not a sufficient check
Key Topics to Cover
string parsing
validation
networking
edge cases
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 add support for the :: compressed IPv6 form?
  • How would you validate a CIDR suffix like /24 or /64?
  • How would you check whether two CIDR blocks overlap?
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 one of three labels. IPv4: exactly 4 parts, each 1 to 3 ASCII digits, value at most 255, no leading zero unless the part is 0. IPv6: exactly 8 parts, each 1 to 4 hex digits, case-insensitive....

High-Level Design

Dispatch on the delimiter count, then validate each part with explicit rules. Avoid relying on int(p) as a validator: Python accepts ' 4', '+4', and '٤' (Arabic-Indic digit). That is exactly t...


Submit Your Answer
Markdown supported