As reported: Intuit SWE-2 (backend), 90-minute technical phone interview (2025), second DSA question: Validate IP Address.
Given a string queryIP, return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, and "Neither" otherwise.
- IPv4: four decimal numbers
x1.x2.x3.x4separated by dots, each0 ≤ xi ≤ 255, with no leading zeros ("0"is fine,"01"is not). - IPv6: eight groups
x1:x2:…:x8separated by colons, each 1 to 4 hexadecimal digits (0-9,a-f,A-F); leading zeros are allowed.
This is LeetCode 468 — Validate IP Address.
Examples
"172.16.254.1" -> "IPv4"
"2001:0db8:85a3:0:0:8A2E:0370:7334" -> "IPv6"
"256.256.256.256" -> "Neither"
"2001:0db8:85a3:0:0:8A2E:0370:7334:" -> "Neither"
"1e1.4.5.6" -> "Neither"
Constraints: queryIP consists only of English letters, digits, . and :.
Code runner format: one JSON value per line (LeetCode's test-case format): queryIP. Implement validIPAddress(queryIP); the harness prints the string you return.
Clarifications
- No compressed IPv6 forms (
::) — exactly eight groups. - Empty groups (
"1..2.3", a trailing separator) are invalid.