Prepin
Log in

Verify IP Address

Approach / Explanation

This question evaluates your capability to create a function or algorithm that can determine if a given string represents a valid Internet Protocol (IP) address. There are two varieties of IP addresses: IPv4 and IPv6. An IP address is considered valid if it adheres to the established formatting criteria for either IPv4 or IPv6. For IPv4: It consists of four numerical segments separated by periods. Each segment must be between 0 and 255. For IPv6: It comprises eight groups of four hexadecimal characters, separated by colons. Each group can range from 0000 to FFFF. Your function should accept a string and return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, and "Neither" if it does not qualify as valid in either format.

Suggested Answer

A Python implementation would appear as follows: def validate_IP ( ip ) : def isIPv4 ( s ) : try : return str ( int ( s ) ) == s and 0 <= int ( s ) <= 255 except : return False def isIPv6 ( s ) : try : return int ( s , 16 ) >= 0 and s [ 0 ] != '-' except : return False if ip . count ( "." ) == 3 and all ( isIPv4 ( i ) for i in ip . split ( "." ) ) : return "IPv4" if ip . count ( ":" ) == 7 and all ( isIPv6 ( i ) for i in ip . split ( ":" ) ) : return "IPv6" return "Neither" This approach divides the task into smaller functions, isIPv4 and isIPv6 , which check if a string is a valid segment of an IPv4 or IPv6 address. The validate_IP function is then created to utilize these smaller validators, parsing the input string and returning the corresponding type if all segments are valid.

Alternative Answer

A Java implementation would appear as follows: public String validIPAddress ( String IP ) { if ( isIPv4 ( IP ) ) return "IPv4" ; if ( isIPv6 ( IP ) ) return "IPv6" ; return "Neither" ; } private boolean isIPv4 ( String IP ) { if ( IP . charAt ( 0 ) == '.' || IP . charAt ( IP . length ( ) - 1 ) == '.' ) return false ; String [ ] nums = IP . split ( "\\." ) ; if ( nums . length != 4 ) return false ; for ( String num : nums ) { if ( ! num . matches ( "\\d{1,3}" ) ) return false ; int val = Integer . parseInt ( num ) ; if ( val < 0 || val > 255 || String . valueOf ( val ) . length ( ) != num . length ( ) ) return false ; } return true ; } private boolean isIPv6 ( String IP ) { if ( IP . charAt ( 0 ) == ':' || IP . charAt ( IP . length ( ) - 1 ) == ':' ) return false ; String [ ] nums = IP . split ( ":" ) ; if ( nums . length != 8 ) return false ; for ( String num : nums ) { if ( ! num . matches ( "[\\da-fA-F]{1,4}" ) ) return false ; } return true ; } This Java implementation divides the problem into two smaller functions, isIPv4 and isIPv6 . Then, it constructs the validIPAddress function, which utilizes these helper methods, splitting the input string with regular expressions and returning the corresponding type if the input is valid.

Question Details

Difficulty & Category

Medium
algorithms

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.