Classify a Login Source Address as IPv4, IPv6, or Invalid
Last updated: November 29, 2026
Quick Overview
Given a string from a login audit log, return 'IPv4' if it is a valid dotted IPv4 address, 'IPv6' if it is a valid full-form IPv6 address with eight hex groups, and 'Neither' otherwise.
Meta
November 29, 20260
0
0 solved
Given a string from a login audit log, return 'IPv4' if it is a valid dotted IPv4 address, 'IPv6' if it is a valid full-form IPv6 address with eight hex groups, and 'Neither' otherwise.
Account security systems at Meta parse login source addresses before risk scoring, and malformed input must never crash the pipeline. Interviewers use this to test exhaustive rule-following: leading zeros, empty groups, group counts, and character sets.
What the Interviewer Expects
- Split on '.' or ':' and check exact group counts
- Reject leading zeros and out-of-range values in IPv4
- Validate 1 to 4 hex digits per IPv6 group
- Reject empty groups and stray separators
- Avoid int() accepting signs or whitespace
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support the compressed IPv6 '::' form?
- How would you validate a CIDR suffix like /24?
- Would you use a regex here, and what are the risks?
- How would you normalize IPv4-mapped IPv6 addresses?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- IPv4: exactly four decimal groups 0 to 255, no leading zeros (
0is fine,01is not), digits only. - IPv6 (full form only): exactly eight groups of 1 to 4 hex digits, case-insensitive; leading ze...
High-Level Design
Decide the family by separator. If the string contains ., validate as IPv4; if it contains :, validate as IPv6. In both, split keeps empty groups ('1..2'.split('.') has an empty string), so an...