Skip to main content
medium

Web Crawler

Web Crawler - Explore a website by following links! You're given a start URL and a dict of URL -> [linked URLs]. Return all pages reachable from the start URL. Example: start_url = "http://a.com" links = {"http://a.com": ["http://a.com/about"]} Result: ["http://a.com", "http://a.com/about"]

Simulate crawling a finite map from URL strings to arrays of outgoing URL strings, visiting each URL at most once and treating missing entries as having no outgoing links. Part 1 takes startUrl and the links map and returns all reachable URLs, including startUrl, in any order. Part 2 follows only links whose hostname exactly matches startUrl, treating subdomains as different hostnames, and returns the reached URLs in any order. Part 3 additionally takes integer priorities and a non-negative limit, treats missing priorities as 0, and returns at most limit URLs in crawl order, choosing a highest-priority discovered unvisited URL next. Part 4 takes an ordered frontier, the links map, a visited set, and a non-negative batchSize, examines the first batchSize frontier URLs, and returns distinct newly discovered URLs absent from both visited and the entire frontier in lexicographically sorted order.

Examples

You write

crawl(start_url: str, links: Dict[str, List[str]])

  1. Example 1

    Input
    Linear chain: a.com -> page1 -> page2
    returns
    ["http://a.com","http://a.com/page1","http://a.com/page2"]
  2. Example 2

    Input
    Cycle: A -> B -> A
    returns
    ["http://a.com","http://a.com/b"]

The pattern behind it

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Graphs

I reach for it when

  • The input is edges, prerequisites or connections.
  • You need cycles, an ordering or connected groups.

Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.

All 32 Graphs problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Follow All Linksmedium
2Part 2
3Part 3
4Part 4

Each locked part opens in the editor once the part before it passes.