Validate IP Address
Updated
Problem statement
Given a string queryIP, return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, or "Neither" otherwise.
IPv4 rules
- The address contains exactly four decimal segments separated by
.. - Each segment is an integer from
0through255. - A segment has no leading zero unless it is exactly
0. - Every character in a segment is a decimal digit.
IPv6 rules
- The address contains exactly eight groups separated by
:. - Each group contains one to four hexadecimal characters.
- Hexadecimal letters may be lowercase or uppercase.
- Leading zeroes are allowed, but an empty group is not.
Function
validIPAddress(queryIP: String) → StringExamples
Example 1
queryIP = "172.16.254.1"return = "IPv4"The address has four decimal segments, each is in range, and none has an invalid leading zero.
Example 2
queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"return = "IPv6"The address has eight non-empty hexadecimal groups, and every group contains at most four characters.
Example 3
queryIP = "256.256.256.256"return = "Neither"Every segment is greater than 255, so this is not a valid IPv4 address.
Constraints
queryIPis a non-empty string.queryIPcontains only English letters, decimal digits,., and:.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026
Similar questions at other companies
- Polynomial CoefficientsShopify · OAShopify interview questions
- Parse Router LogsIntuit · OAIntuit interview questions
- Simplify a Quadratic ExpressionOpenAI · OAOpenAI interview questions
- DNA Sequence MatchSpaceX · OASpaceX interview questions
- Correct Apartment Bedroom CountsRamp · OARamp interview questions
- Remove CSS Comments and Find Hex ColorsAdobe · OAAdobe interview questions
- Create New VersionCisco · OACisco interview questions
- Order LayoutAmazon · OA
- Validate and Parse HTTP RequestsIBM · OAIBM interview questions
- Convert Backticked Identifiers to Camel CaseZipRecruiter · OAZipRecruiter interview questions