KatInterview

Alphabet-Coverage Web Crawler with Rate Limiting

Data Structure DesignModel answer
MediumModel answerData Structure Designcrawlingrate-limitingpriority-queue

Last updated

Build a crawler that prioritizes unvisited first-letters across a link graph while respecting a configurable requests-per-second ceiling.

Alphabet-Coverage Web Crawler with Rate Limiting

You are building a focused crawler that walks a web of encyclopedia-style pages. Each page has a title and contains links to other pages by title. Your crawler must explore pages intelligently, track which starting letters it has already encountered, and stay within a configurable request rate.

What you are given

  • A start title (e.g. "Python") from which crawling begins.
  • A fetch helper you can treat as injected: fetch(title) -> (canonical_title, [linked_titles]). It raises PageNotFound for titles that do not exist, and TransientError for recoverable failures (timeouts, server errors).
  • A rate limit R: the crawler must make at most R requests per second.
  • A maximum page count max_pages: stop after successfully crawling this many pages.

What you must implement

A Crawler class (or equivalent) with at minimum:

crawl(start_title) -> list[str]   # returns titles successfully crawled, in order

Your implementation must satisfy the following requirements:

Deduplication. Never fetch the same title twice. A title should also not sit in the frontier more than once.

Letter tracking. Maintain a set of first-letters (A–Z, case-insensitive) seen among successfully crawled pages. Titles that do not start with a letter (e.g. "1984") are crawlable but never contribute a letter and are never prioritized.

Selection policy.

  • While any unseen letter has at least one candidate in the frontier, pick the next page from that group (uniformly at random among unseen-letter candidates).
  • Once all 26 letters have been seen, or no frontier page starts with an unseen letter, pick the next page uniformly at random from the full frontier.

Rate limiting. Every HTTP attempt — including retries — must consume one token from a token-bucket limiter configured to R tokens per second.

Error handling.

  • Transient errors: retry up to a configurable maximum with exponential backoff; each retry still consumes a rate-limit token.
  • Permanent errors (PageNotFound): skip the page, do not count it toward max_pages, do not record its letter.
  • Redirect (canonical title differs from requested title): mark both as visited.
  • Empty frontier: terminate cleanly.

Constraints and clarifications

  • Titles are normalized: collapse underscores to spaces, strip fragment identifiers (#Section), capitalize the first character, drop namespaced titles (File:, Category:, Help:, etc.).
  • You may assume fetch is the only network call you need; all other logic must be your own.
  • Focus on correctness of the priority logic, deduplication, and rate limiting. You are expected to be able to walk through the code and explain every design decision.

Example trace

start: "Python"          # 'P' added to seen letters
frontier after first fetch might include:
  "Guido van Rossum"     # 'G' — unseen, prioritized
  "Programming language" # 'P' — seen, not prioritized
  "Amsterdam"            # 'A' — unseen, prioritized
  ...
next pick: random choice among {'G...', 'A...', ...} unseen-letter candidates

What a complete answer looks like

  • A working implementation in Python (or a language of your choice).
  • Clear explanation of the data structures chosen and why.
  • Time and space complexity analysis.
  • A walkthrough of at least three edge cases.

Hints

Hint 1

The selection policy has two distinct phases. Think about what data structure lets you maintain per-letter buckets and a global pool simultaneously, so you can serve both phases in O(1) or O(26) without rebuilding anything.

Hint 2

A standard token bucket needs only two fields of state — current token count and last refill timestamp — plus a sleep call. Use a monotonic clock, not wall time, to avoid jumps from clock corrections.

Hint 3

Deduplication needs to happen in two places: when a title is enqueued (skip if already visited or already in the frontier) and when a canonical title comes back from fetch (mark it visited even if it differs from the title you requested).