# Extract Domain

> The domain is buried in the string.

Canonical URL: <https://datadriven.io/problems/extract_domain>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a URL string containing '://', return the substring between '://' and the next '/' (or end of string if no trailing slash).

## Worked solution and explanation

### Why this problem exists in real interviews

Extracting a domain from a URL tests **string splitting and indexing**, a pattern that appears constantly in log parsing and ETL pipelines. It checks whether you can find substrings between delimiters efficiently.

---

### Break down the requirements

#### Step 1: Find the position after ://

Split on `'://'` and take the part after it. This isolates everything after the protocol.

#### Step 2: Take everything before the next /

Split the remainder on `'/'` and take the first segment. This gives the domain.

---

### The solution

**Delimiter-based extraction with split**

```python
def extract_domain(url):
    after_protocol = url.split('://')[1]
    domain = after_protocol.split('/')[0]
    return domain
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the URL length. Two split operations scan the string.
> 
> **Space:** O(n) for the intermediate split results.

> **Interviewers Watch For**
>
> Using `split` for clean extraction rather than manual index arithmetic. The split approach is more readable and less error-prone.

> **Common Pitfall**
>
> Not handling URLs with no path after the domain (e.g., `'https://example.com'`). The split on `'/'` still works because `split('/')` returns at least one element.

---

## Common follow-up questions

- What if the URL has a port number like 'https://example.com:8080/path'? _(Tests splitting on ':' after the protocol to separate domain and port.)_
- How would you extract the subdomain separately? _(Tests splitting the domain on '.' and taking all but the last two segments.)_
- What if the URL is malformed and has no ://? _(Tests error handling: return an empty string or raise a ValueError.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/extract_domain)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.