Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count URLs Matching Domain

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Count URLs Matching Domain. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Anthropic's Claude documentation tooling represents a crawlable web as an in-memory graph. Given a domain, a starting URL, and a mapping of pages to their outgoing links, count the number of unique reachable URLs that belong to the domain.

Use breadth-first or depth-first traversal. A URL belongs to domain when its hostname is exactly domain or is a subdomain of it. For example, docs.anthropic.com belongs to anthropic.com, but notanthropic.com does not. Ignore URL fragments when determining whether two URLs are the same, so https://docs.anthropic.com/a#intro and https://docs.anthropic.com/a#usage represent one URL. The input contains absolute HTTP or HTTPS URLs.

Formal Specification

Implement count_domain_urls(pages, domain, main_url).

  • pages is a dictionary mapping a URL string to a list of absolute URL strings. A URL absent from pages has no outgoing links.
  • domain, main_url, and all URL keys and links are strings.
  • Return an integer containing the number of unique URLs reachable from main_url, including main_url when it matches the domain.
  • Traversal may follow links outside the target domain because those pages can lead back to target-domain URLs.

Constraints

  • 1 <= len(pages) <= 10^5
  • The reachable graph contains at most 2 * 10^5 distinct normalized URLs
  • Each URL has length at most 2 * 10^3 characters
  • domain is a bare hostname without a scheme or path
  • All supplied URLs are absolute HTTP or HTTPS URLs

Function Signature

def count_domain_urls(pages, domain, main_url):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output