KatInterview

Build a Polite Web Crawler with Letter-Coverage Ordering

Data Structure DesignModel answer
MediumModel answerData Structure Designweb-crawlerrate-limitingfrontier-design

Last updated

Design and implement a rate-limited article crawler that visits pages with unseen first letters before switching to random order, and explain how you would use an AI assistant to build it.

You have about 75 minutes, with an AI coding assistant available, to build a crawler for an online encyclopedia (for example, Wikipedia). The crawler starts from one seed article and follows links to other articles.

Ordering rule

The crawler tracks which letters A–Z have been "covered", meaning that a page whose title begins with that letter has been crawled.

  1. While some letters remain uncovered, the crawler prefers pages whose title starts with an uncovered letter. Pages whose initial is already covered are held back for now. For instance, if the seed is "Google", other pages starting with G are deferred.
  2. Once all 26 letters are covered, the crawler picks the remaining known pages at random.
  3. The crawler must obey a rate limit.

Questions to settle first

State your assumptions on each of these before coding:

  • Is a letter covered when a page is discovered, or only after it is fetched successfully?
  • How are titles starting with digits, punctuation or non-Latin characters handled? Is matching case-insensitive?
  • What stops the crawl once all letters are covered (page budget, time budget, never)?
  • How is the rate limit expressed, and does the server signal violations with error responses?
  • Do you parse article HTML, or use an API that returns the links of a page?

Part 1: Crawl order

Implement the core: fetch a page, extract links to other articles, and choose the next page according to the coverage rule. Describe your frontier and visited-set design, how you normalize links and skip non-article pages, and what happens when no known page has an uncovered initial.

Part 2: Rate limiting

Make the crawler respect the limit and behave well when the server pushes back. Explain how your limiter can be tested without real waiting, how you handle rate-limit and transient errors (including retries), and what politeness measures you add beyond the limit itself.

Part 3: Working with the assistant

Explain how you would divide the work with the AI assistant during the 75 minutes, and how you keep the generated code understandable and correct.

Hints

Hint 1

Keep one bucket of pending titles per initial letter, plus a visited set. Picking the next page then becomes: choose from any bucket whose letter is uncovered, otherwise pick from all remaining pages.

Hint 2

Put the clock and the sleep function behind small interfaces so the limiter and the retry logic can be unit tested with a fake clock, and put the fetcher behind an interface so ordering can be tested on a fake link graph.

Hint 3

For the assistant, write the interfaces and a few failing tests yourself first, ask for one small component at a time, and read and run each piece before moving on.