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.

You are given a URL-like string `s` of the form `IP:PORT/PATH`. Split it into an IP address, a port and a path, then validate the IP address and the port. How to split: - The IP address is everything before the first `:` in `s`. - The port is everything between that first `:` and the first `/` that comes after it. - The path is everything from that `/` to the end of `s`, including the `/` itself. If `s` contains no `:`, or contains no `/` after its first `:`, the input is invalid. How to validate: - The IP address 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 may contain more dots, colons and slashes. When nothing follows the `/`, the path is `"/"`. Return a list of three strings `[ip, port, path]`, each copied exactly as it appears in `s`. If any check fails, return `["Invalid", "Invalid", "Invalid"]`. A partly valid list is never returned. Because `s` can be up to 300 characters long, a run of digits in an octet or in the port can stand for a value far larger than 2^31-1, or even 2^64. Such an octet or port is out of range, so the result is invalid. Example 1: Input: s = "192.168.1.1:8080/api/v1/resource" Output: ["192.168.1.1", "8080", "/api/v1/resource"] All four octets are from 0 to 255 with no leading zeros, and 8080 is from 1 to 65535. The path starts at the first `/` after the port and runs to the end of the string. Example 2: Input: s = "256.100.50.25:80/home" Output: ["Invalid", "Invalid", "Invalid"] The first octet, 256, is above 255, so the whole result is invalid even though the port and path are fine. Constraints: - 1 <= len(s) <= 300 - s contains only digits, dots, colons, slashes and lowercase English letters.

Constraints

  • 1 <= len(s) <= 300
  • s contains only digits, dots, colons, slashes and lowercase English letters.
  • Any string within that alphabet and length is a valid input; a malformed one returns ["Invalid", "Invalid", "Invalid"].

Examples

Input: ('192.168.1.1:8080/api/v1/resource',)

Expected Output: ['192.168.1.1', '8080', '/api/v1/resource']

Explanation: Source example 1 (input length 32): every octet and port 8080 are in range, so the three parts come back as written.

Input: ('256.100.50.25:80/home',)

Expected Output: ['Invalid', 'Invalid', 'Invalid']

Explanation: Source example 2 (input length 21): octet 256 is one above 255, and the valid port and path do not change that.

Hints

  1. Find the two split positions first: the first ':' in the string, and the first '/' that comes after it. A '/' that appears before that colon does not count.
  2. Before looking at a number's value, check its form: it must be non-empty, all digits, and free of a leading zero unless it is exactly "0".
  3. A digit run can be hundreds of characters long. Decide how to reject an oversized octet or port without parsing it into a fixed-width integer.

Split and Validate an IP:PORT/PATH String

easyOktaSoftware Engineer

You are given a URL-like string s of the form IP:PORT/PATH. Split it into an IP address, a port and a path, then validate the IP address and the port.

How to split:

  • The IP address is everything before the first : in s.
  • The port is everything between that first : and the first / that comes after it.
  • The path is everything from that / to the end of s, including the / itself.

If s contains no :, or contains no / after its first :, the input is invalid.

How to validate:

  • The IP address 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 may contain more dots, colons and slashes. When nothing follows the /, the path is "/".

Return a list of three strings [ip, port, path], each copied exactly as it appears in s. If any check fails, return ["Invalid", "Invalid", "Invalid"]. A partly valid list is never returned.

Because s can be up to 300 characters long, a run of digits in an octet or in the port can stand for a value far larger than 2^31-1, or even 2^64. Such an octet or port is out of range, so the result is invalid.

Example 1: Input: s = "192.168.1.1:8080/api/v1/resource" Output: ["192.168.1.1", "8080", "/api/v1/resource"] All four octets are from 0 to 255 with no leading zeros, and 8080 is from 1 to 65535. The path starts at the first / after the port and runs to the end of the string.

Example 2: Input: s = "256.100.50.25:80/home" Output: ["Invalid", "Invalid", "Invalid"] The first octet, 256, is above 255, so the whole result is invalid even though the port and path are fine.

Constraints:

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

Examples

Example 1
Input
('192.168.1.1:8080/api/v1/resource',)
Output
['192.168.1.1', '8080', '/api/v1/resource']
Notes
Source example 1 (input length 32): every octet and port 8080 are in range, so the three parts come back as written.
Example 2
Input
('256.100.50.25:80/home',)
Output
['Invalid', 'Invalid', 'Invalid']
Notes
Source example 2 (input length 21): octet 256 is one above 255, and the valid port and path do not change that.

Constraints

  • •1 <= len(s) <= 300
  • •s contains only digits, dots, colons, slashes and lowercase English letters.
  • •Any string within that alphabet and length is a valid input; a malformed one returns ["Invalid", "Invalid", "Invalid"].

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def parse_url(s) -> list[str]:
Sample call
parse_url('192.168.1.1:8080/api/v1/resource')
Language
Case 1
Input
('192.168.1.1:8080/api/v1/resource',)
Expected
['192.168.1.1', '8080', '/api/v1/resource']
Case 2
Input
('256.100.50.25:80/home',)
Expected
['Invalid', 'Invalid', 'Invalid']

Browse More Questions

Show the approach

Approach

Find the first ':' with a left-to-right search. If there is none, return the invalid list. Then search for the first '/' starting one position after that colon; if there is none, return the invalid list. These two positions fix the three parts exactly as the statement defines them: ip = s[0:colon], port = s[colon+1:slash] and path = s[slash:].

Split ip on every '.', keeping empty pieces, and require exactly four pieces. Each piece, and then the port, goes through one shared check: it must be non-empty, all digits, free of a leading zero when longer than one character, and its value must lie in its range (0 to 255 for an octet, 1 to 65535 for the port).

The check rejects a piece longer than 3 characters (octet) or 5 characters (port) before converting it. This is safe because a digit string with no leading zero and 4 or more digits is at least 1000, above 255, and one with 6 or more digits is at least 100000, above 65535. So no conversion ever sees more than five digits, and a digit run of any length, even one past 2^64, is rejected without overflow.

The answer list is built only after every check has passed, so a single failure always yields the full invalid list and a partly valid list can never be returned.

Edge cases: an empty IP splits into one empty piece and fails the count; a leading, trailing or doubled dot produces an empty piece (Java's String.split drops trailing empty pieces by default, so the Java and C++ references split by hand); extra colons, dots and slashes after the split points stay in the path untouched; a second ':' before the first '/' falls inside the port and fails the digit test; a path that is the '/' alone is returned as "/".

Time complexity:
O(n)
Space complexity:
O(n)