Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Validate IPv4 and IPv6 Address
00:00
5 left

Validate IPv4 and IPv6 Address

MediumPython

Problem

At Meta, systems such as network-facing services and internal traffic filters need strict IP parsing. Implement a function that takes a string and returns whether it is a valid IPv4 address, valid IPv6 address, or Neither.

Formal Specification

Write a function validate_ip(address) that accepts a string address and returns one of:

  • "IPv4" if the string is a valid IPv4 address
  • "IPv6" if the string is a valid IPv6 address
  • "Neither" otherwise

A valid IPv4 address has exactly 4 decimal parts separated by .. Each part must contain only digits, be in the range 0 to 255, and must not contain leading zeros unless the part is exactly "0".

A valid IPv6 address has exactly 8 parts separated by :. Each part must be 1 to 4 characters long and contain only hexadecimal characters: 0-9, a-f, or A-F.

Constraints

  • 1 <= len(address) <= 100
  • IPv4 validation requires exactly 4 segments separated by '.'
  • Each IPv4 segment must be a decimal number in [0, 255] with no leading zeros unless the segment is exactly '0'
  • IPv6 validation requires exactly 8 groups separated by ':'
  • Each IPv6 group must contain 1 to 4 hexadecimal characters
  • Do not use built-in IP parsing libraries

Function Signature

def validate_ip(address):
Interviewer

Your question is Validate IPv4 and IPv6 Address. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.