Build a single-domain web crawler that returns a sorted site map of links and assets

Quick Overview

Crawl a website from a start URL, following only links on the same domain, and return a sorted site map of each reachable page's in-domain links and static assets. It tests graph traversal with a visited set, parsing the host out of a URL, and exact output rules for missing pages and ordering.

Build a single-domain web crawler that returns a sorted site map of links and assets

Company: Monzo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Implement a simple web crawler limited to a single domain. Given a starting URL `start_url` and a description of the site's pages, produce a site map that lists, for every reachable page within the starting URL's domain, the in-domain pages it links to and the static assets it depends on. Each entry in `pages` describes one page: its URL, the list of URLs the page links to, and the list of asset URLs the page depends on (images, scripts and so on). The crawler must start from `start_url`, only follow links whose domain matches the domain of `start_url`, and visit each page exactly once. External links must be ignored: they are not followed and not included in the site map. Pages that are not present in `pages` cannot be crawled and get no entry in the output. Return one entry for every reachable in-domain page that is present in `pages`. Sort the links and assets lexicographically within each entry, and sort the outer list lexicographically by page URL. ### Function Signature ```python def crawl(start_url: str, pages: list[list[list[str]]]) -> list[list[list[str]]]: ``` ### Rules - Each entry of `pages` is `[[url], links, assets]`. The page URL is wrapped in a one-element list so that every entry is a list of string lists. Each output entry has the same shape: `[[page_url], in_domain_links, assets]`. - The domain of a URL is the host between `://` and the next `/` (or the end of the string), compared case-insensitively. The scheme does not matter, and a subdomain such as `api.example.com` is a different domain from `example.com`. - Page identity uses the exact URL string. A link leads to a page only when it equals that page's URL character for character; only the domain comparison ignores case. - `in_domain_links` holds every link of the page whose domain matches the start domain, including a link to the page itself and links to URLs that are missing from `pages`. A missing URL never gets an entry of its own. - `assets` holds every asset of the page, whatever its domain. The domain filter applies only to links. - A URL repeated within one page's `links` or `assets` is listed once. - Sorting uses plain string comparison (character code order), so `:` sorts before `s` and uppercase letters sort before lowercase ones. - Page URLs in `pages` are distinct. If `start_url` is not in `pages`, return an empty list. ### Constraints - `1 <= len(pages) <= 10^4` - Each URL has length between 1 and 200. - The total number of links and assets across all pages is at most `10^5`. - All URLs begin with `http://` or `https://`. - `start_url` is a valid URL but may or may not appear in `pages`. ### Examples ```text Input: start_url = "http://a.com/" pages = [ [["http://a.com/"], ["http://a.com/x", "http://b.com/"], ["http://cdn.net/logo.png"]], [["http://a.com/x"], ["http://a.com/", "http://a.com/missing"], []], [["http://a.com/z"], ["http://a.com/"], []], [["http://b.com/"], ["http://a.com/z"], []] ] Output: [ [["http://a.com/"], ["http://a.com/x"], ["http://cdn.net/logo.png"]], [["http://a.com/x"], ["http://a.com/", "http://a.com/missing"], []] ] ``` The link to `b.com` is external, so it is dropped and `b.com` is never crawled, which leaves `a.com/z` unreachable even though it is in `pages`. The link to `a.com/missing` stays in the list for `a.com/x` because it is in-domain, but that URL gets no entry since it is not in `pages`, and the external logo is kept because assets are not filtered. ```text Input: start_url = "http://solo.io/home" pages = [ [["http://solo.io/home"], ["http://solo.io/home"], ["http://solo.io/style.css", "http://solo.io/app.js"]] ] Output: [ [["http://solo.io/home"], ["http://solo.io/home"], ["http://solo.io/app.js", "http://solo.io/style.css"]] ] ``` A single page links to itself. The self-link is recorded once and the page is visited once, and the assets are sorted. ```text Input: start_url = "https://Docs.io/start" pages = [ [["https://Docs.io/start"], ["https://docs.io/b", "http://DOCS.IO/a", "https://docs.io/b", "https://api.docs.io/c"], []], [["https://docs.io/b"], [], ["https://docs.io/b.png"]], [["http://DOCS.IO/a"], [], []] ] Output: [ [["http://DOCS.IO/a"], [], []], [["https://Docs.io/start"], ["http://DOCS.IO/a", "https://docs.io/b"], []], [["https://docs.io/b"], [], ["https://docs.io/b.png"]] ] ``` Hosts compare without case and the scheme is ignored, so `http://DOCS.IO/a` is in-domain, while `api.docs.io` is a different host and is dropped; the repeated link to `/b` is listed once. In plain character order `:` comes before `s` and `D` comes before `d`, which puts `http://DOCS.IO/a` first and `https://Docs.io/start` before `https://docs.io/b`.

Overview: Crawl a website from a start URL, following only links on the same domain, and return a sorted site map of each reachable page's in-domain links and static assets. It tests graph traversal with a visited set, parsing the host out of a URL, and exact output rules for missing pages and ordering.

Monzo logo
Monzo
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Implement a simple web crawler limited to a single domain. Given a starting URL start_url and a description of the site's pages, produce a site map that lists, for every reachable page within the starting URL's domain, the in-domain pages it links to and the static assets it depends on.

Each entry in pages describes one page: its URL, the list of URLs the page links to, and the list of asset URLs the page depends on (images, scripts and so on).

The crawler must start from start_url, only follow links whose domain matches the domain of start_url, and visit each page exactly once. External links must be ignored: they are not followed and not included in the site map. Pages that are not present in pages cannot be crawled and get no entry in the output.

Return one entry for every reachable in-domain page that is present in pages. Sort the links and assets lexicographically within each entry, and sort the outer list lexicographically by page URL.

Function Signature

def crawl(start_url: str, pages: list[list[list[str]]]) -> list[list[list[str]]]:

Rules

  • Each entry of pages is [[url], links, assets] . The page URL is wrapped in a one-element list so that every entry is a list of string lists. Each output entry has the same shape: [[page_url], in_domain_links, assets] .
  • The domain of a URL is the host between :// and the next / (or the end of the string), compared case-insensitively. The scheme does not matter, and a subdomain such as api.example.com is a different domain from example.com .
  • Page identity uses the exact URL string. A link leads to a page only when it equals that page's URL character for character; only the domain comparison ignores case.
  • in_domain_links holds every link of the page whose domain matches the start domain, including a link to the page itself and links to URLs that are missing from pages . A missing URL never gets an entry of its own.
  • assets holds every asset of the page, whatever its domain. The domain filter applies only to links.
  • A URL repeated within one page's links or assets is listed once.
  • Sorting uses plain string comparison (character code order), so : sorts before s and uppercase letters sort before lowercase ones.
  • Page URLs in pages are distinct. If start_url is not in pages , return an empty list.

Constraints

  • 1 <= len(pages) <= 10^4
  • Each URL has length between 1 and 200.
  • The total number of links and assets across all pages is at most 10^5 .
  • All URLs begin with http:// or https:// .
  • start_url is a valid URL but may or may not appear in pages .

Examples

Input:
start_url = "http://a.com/"
pages = [
  [["http://a.com/"], ["http://a.com/x", "http://b.com/"], ["http://cdn.net/logo.png"]],
  [["http://a.com/x"], ["http://a.com/", "http://a.com/missing"], []],
  [["http://a.com/z"], ["http://a.com/"], []],
  [["http://b.com/"], ["http://a.com/z"], []]
]
Output:
[
  [["http://a.com/"], ["http://a.com/x"], ["http://cdn.net/logo.png"]],
  [["http://a.com/x"], ["http://a.com/", "http://a.com/missing"], []]
]

The link to b.com is external, so it is dropped and b.com is never crawled, which leaves a.com/z unreachable even though it is in pages. The link to a.com/missing stays in the list for a.com/x because it is in-domain, but that URL gets no entry since it is not in pages, and the external logo is kept because assets are not filtered.

Input:
start_url = "http://solo.io/home"
pages = [
  [["http://solo.io/home"], ["http://solo.io/home"], ["http://solo.io/style.css", "http://solo.io/app.js"]]
]
Output:
[
  [["http://solo.io/home"], ["http://solo.io/home"], ["http://solo.io/app.js", "http://solo.io/style.css"]]
]

A single page links to itself. The self-link is recorded once and the page is visited once, and the assets are sorted.

Input:
start_url = "https://Docs.io/start"
pages = [
  [["https://Docs.io/start"], ["https://docs.io/b", "http://DOCS.IO/a", "https://docs.io/b", "https://api.docs.io/c"], []],
  [["https://docs.io/b"], [], ["https://docs.io/b.png"]],
  [["http://DOCS.IO/a"], [], []]
]
Output:
[
  [["http://DOCS.IO/a"], [], []],
  [["https://Docs.io/start"], ["http://DOCS.IO/a", "https://docs.io/b"], []],
  [["https://docs.io/b"], [], ["https://docs.io/b.png"]]
]

Hosts compare without case and the scheme is ignored, so http://DOCS.IO/a is in-domain, while api.docs.io is a different host and is dropped; the repeated link to /b is listed once. In plain character order : comes before s and D comes before d, which puts http://DOCS.IO/a first and https://Docs.io/start before https://docs.io/b.