BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Data processing and features31 of 54 in topic

756. Crawl a site breadth first, staying on one host

MediumData processing and features Pro

Training data and search indexes both start with a crawler. Here the web is simulated: pages maps a normalized URL to the list of raw link strings found on that page. A URL that is not a key in pages failed to fetch.

Write crawl(start, pages, max_pages) that visits pages breadth first from start and returns the list of URLs it fetched successfully, in visit order.

Rules: (1) Normalize every link before using it: a link that starts with '/' is joined to the start URL's scheme and host; only links that start with 'http://' or 'https://' are kept after that (skip 'mailto:' and the like); drop anything from '#' on; lowercase the host; remove trailing '/' characters. (2) Follow only links whose host equals the start URL's host. (3) Never enqueue the same normalized URL twice. (4) A failed fetch is skipped and does not count toward max_pages. (5) Stop once max_pages pages were fetched. The start URL is given already normalized.

If the start URL itself fails to fetch (it is not a key in pages), nothing is fetched, so return an empty list.

Functiondef crawl(start, pages, max_pages)

Example 1

Input: start = 'https://a.com', pages = {'https://a.com': ['/x', 'https://b.com/y', '/x#top'], 'https://a.com/x': ['https://A.com/']}, max_pages = 10

Output: ['https://a.com', 'https://a.com/x']

Example 2

Input: start = 'http://s.io', pages = P2, max_pages = 10

Output: ['http://s.io', 'http://s.io/a', 'http://s.io/b', 'http://s.io/c', 'http://s.io/d']

Constraints

  • At most 1,000 pages and 10,000 links.
  • Standard library only.
Hint 1

Split the start URL into scheme and host once.

Hint 2

Write a small normalize helper that returns None for links you will skip.

Hint 3

Count only successful fetches toward max_pages.

Common mistakes
  • Marking pages seen only when visited, so duplicates pile up in the queue.
  • Forgetting that '/x' and '/x#top' are the same page.
  • Counting failed fetches toward the page budget.
Understand the idea

A breadth-first crawler visits pages level by level: the start page, then every page it links to, then their links, and so on. A queue gives that order. The hard parts are not the queue but deciding when two links are the same page and which links are in scope. Normalizing URLs (resolve relative links, drop fragments, lowercase the host, trim trailing slashes) turns many spellings into one key. Marking a URL as seen when you enqueue it keeps the queue small. Real crawlers add politeness delays, robots rules, retries and a per-host budget on top of this core loop.

Topics
Data processing and features
Similar questions
  • Split a dataset so every class keeps its shareMedium
  • Bin a feature into quantile buckets learned from training dataMedium
  • Frequency-encode a category with a shared bucket for rare and unseen valuesMedium
  • Generate K-fold train and validation index splitsMedium
All Data processing and features problems
Report
756 / 903
Python 3solution.pyPython idle
1
2
3
4
def crawl(start, pages, max_pages):
# return the list of fetched URLs in breadth-first order
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS