Validate IP Address
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Motive reportedly put "Validate IP Address" in front of candidates in December 2025, and the detail that trips people is small: "256.256.256.256" comes back as Neither, while "2001:0db8:85a3:0:0:8A2E:0370:7334" is a valid IPv6 with mixed case and short groups. This is a string parsing problem, not an algorithm puzzle. There's no clever data structure. You split, you check rules, you return one of three words. If your OA is in a day or two, the win is being methodical about edge cases. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the logic below is simple enough to own yourself.
The problem
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 0 through 255. 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) → String Examples 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 queryIP is a non-empty string. queryIP contains only English letters, decimal digits,., and :.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to decide the type first, then validate strictly. If the string contains a dot, treat it as an IPv4 candidate. If it contains a colon, treat it as an IPv6 candidate. Otherwise return Neither. Split on the separator with a method that keeps empty pieces, because "1.1.1." and "1::1" must fail on the empty segment. For IPv4, check exactly four parts, every character is a digit, no leading zero unless the part is "0", and the value is at most 255. For IPv6, check exactly eight parts, each 1 to 4 characters, all in 0-9, a-f, A-F. The common pitfall is a lenient split that drops trailing empty strings, or parsing ints that accept signs and whitespace. Check characters before converting. If you freeze on the split edge case during the live OA, StealthCoder can hand you a clean reference solution. Runtime is linear in the string length.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Validate IP Address cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as validate ip address. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Motive's OA.
Motive reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Validate IP Address FAQ
How hard is the Motive Validate IP Address question really?+
Easy on algorithms, annoying on edge cases. There's no pattern to memorize beyond string splitting. Most failures come from trailing separators, leading zeros, and non-digit characters. Write the checks as small helper functions and you'll pass cleanly.
What's the trick to getting it right the first time?+
Pick the type by separator, then validate every segment strictly. Split in a way that preserves empty strings, count the parts, and verify characters before you convert to a number. That order avoids almost every wrong answer.
Which edge cases should I test before submitting?+
Try "1.1.1.", "01.1.1.1", "1.1.1.1.1", "::::::::" style strings, a group with five hex characters, a group with letter g, and a segment like "+1". Also test "0.0.0.0" and uppercase hex, which should be valid.
Do I need regex or can I parse by hand?+
Parsing by hand is safer. Regex works but is easy to get subtly wrong on leading zeros and range checks. A split plus a loop over characters is short, readable, and easy to debug under pressure.
How do I prepare for this in 48 hours?+
Write the solution once from scratch without looking, then run it against a list of tricky inputs. Spend the rest of the time on similar string parsing problems. This type of question rewards careful reading more than speed.