Parse an IP:PORT/PATH string into a validated IP address, port and path

Quick Overview

Split a string of the form IP:PORT/PATH into its IPv4 address, port and path, and return three Invalid markers when any part breaks the rules. It tests careful string splitting and range checks on octets and ports, including leading zeros, port zero and an empty path.

Parse an IP:PORT/PATH string into a validated IP address, port and path

Company: Okta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a URL-like string `s` in the form `IP:PORT/PATH`. Parse it to extract the IP address, the port and the path, and validate each part. The IP address is everything before the first `:` and consists of four octets separated by dots. The port is everything between that `:` and the first `/` after it. The path is everything from that `/` to the end of the string, including the `/` itself. Return a list of three strings `[ip, port, path]`. If any validation fails, return `["Invalid", "Invalid", "Invalid"]`. ### Function Signature ```python def parse_url(s: str) -> list[str]: ``` ### Rules - The string must contain a `:` (separating IP and port) and, after that first `:`, a `/` (separating port and path). If either is missing, the result is invalid. - The IP part must split on `.` into exactly four octets. Each octet must be a non-empty string of digits with a value from 0 to 255 and no leading zeros. `"0"` is valid; `"01"` and `"00"` are not. - The port must be a non-empty string of digits with a value from 1 to 65535 and no leading zeros. `"080"` is invalid, and so is `"0"` because it is below 1. - The path is not validated. It is returned exactly as it appears, starting with the `/`, and it may contain further dots, colons and slashes. When nothing follows the `/`, the path is `"/"`. - The IP and port are returned exactly as they appear in the input. - One failed check makes the whole result `["Invalid", "Invalid", "Invalid"]`. Never return a partly valid list. ### Constraints - `1 <= len(s) <= 300` - `s` contains only digits, dots, colons, slashes and lowercase English letters. ### Examples ```text Input: s = "192.168.1.1:8080/api/v1/resource" Output: ["192.168.1.1", "8080", "/api/v1/resource"] ``` All four octets are in range with no leading zeros, and 8080 is inside 1 to 65535. The path starts at the first `/` after the port and runs to the end of the string. ```text Input: s = "256.100.50.25:80/home" Output: ["Invalid", "Invalid", "Invalid"] ``` The first octet, 256, exceeds 255, so the entire result is invalid even though the port and path are fine. ```text Input: s = "192.168.1.1:80/" Output: ["192.168.1.1", "80", "/"] ``` The path portion is empty, which is allowed. The returned path is the `/` on its own.

Overview: Split a string of the form IP:PORT/PATH into its IPv4 address, port and path, and return three Invalid markers when any part breaks the rules. It tests careful string splitting and range checks on octets and ports, including leading zeros, port zero and an empty path.

Okta logo
Okta
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a URL-like string s in the form IP:PORT/PATH. Parse it to extract the IP address, the port and the path, and validate each part.

The IP address is everything before the first : and consists of four octets separated by dots. The port is everything between that : and the first / after it. The path is everything from that / to the end of the string, including the / itself.

Return a list of three strings [ip, port, path]. If any validation fails, return ["Invalid", "Invalid", "Invalid"].

Function Signature

def parse_url(s: str) -> list[str]:

Rules

  • The string must contain a : (separating IP and port) and, after that first : , a / (separating port and path). If either is missing, the result is invalid.
  • The IP part must split on . into exactly four octets. Each octet must be a non-empty string of digits with a value from 0 to 255 and no leading zeros. "0" is valid; "01" and "00" are not.
  • The port must be a non-empty string of digits with a value from 1 to 65535 and no leading zeros. "080" is invalid, and so is "0" because it is below 1.
  • The path is not validated. It is returned exactly as it appears, starting with the / , and it may contain further dots, colons and slashes. When nothing follows the / , the path is "/" .
  • The IP and port are returned exactly as they appear in the input.
  • One failed check makes the whole result ["Invalid", "Invalid", "Invalid"] . Never return a partly valid list.

Constraints

  • 1 <= len(s) <= 300
  • s contains only digits, dots, colons, slashes and lowercase English letters.

Examples

Input: s = "192.168.1.1:8080/api/v1/resource"
Output: ["192.168.1.1", "8080", "/api/v1/resource"]

All four octets are in range with no leading zeros, and 8080 is inside 1 to 65535. The path starts at the first / after the port and runs to the end of the string.

Input: s = "256.100.50.25:80/home"
Output: ["Invalid", "Invalid", "Invalid"]

The first octet, 256, exceeds 255, so the entire result is invalid even though the port and path are fine.

Input: s = "192.168.1.1:80/"
Output: ["192.168.1.1", "80", "/"]

The path portion is empty, which is allowed. The returned path is the / on its own.