Validate IP Address

MediumStringssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a string queryIP, return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, and "Neither" otherwise. A valid **IPv4** address has exactly four parts separated by .. Each part is a decimal number from 0 to 255 made only of the digits 0-9, and has no leading zeros unless the part is exactly "0". For example "192.168.1.1" is valid, while "192.168.01.1", "256.1.1.1" and "1.1.1." are not. A valid **IPv6** address has exactly eight parts separated by :. Each part has 1 to 4 characters, and each character is a digit or a letter from a-f or A-F. Leading zeros are allowed, but an empty part (as in "::") is not. For example "2001:db8:85a3:0:0:8A2E:0370:7334" is valid, while "02001:db8:85a3:0:0:8A2E:0370:7334" is not, because its first part has five characters.

Examples

Input: queryIP = "172.16.254.1"
Output: "IPv4"
Four decimal parts, each between 0 and 255, with no leading zeros.
Input: queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
Eight hex parts, each 1 to 4 characters long.
Input: queryIP = "256.256.256.256"
Output: "Neither"
256 is larger than 255.

Constraints

  • 1 <= queryIP.length <= 100
  • `queryIP` consists only of English letters, digits, '.' and ':'.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems