Anthropic logoAnthropic
Coding·55 minFree preview

Coding Q1 — Concurrent Web Crawler

Implement a same-domain web crawler given a pre-supplied `htmlParser.getUrls(url)` helper. Starts as a single-threaded BFS; the bulk of the round is the follow-up to parallelize it (threads, processes, or async) and reason about scaling across machines.

SWE
MLE
Infra Eng
concurrency
threading
async
bfs
python
medium
Frequency
High
Last asked
2026-10-28
Stage
phone-screen · onsite-coding

Problem

Implement crawl(startUrl: str, htmlParser: HtmlParser) -> list[str]:

  • Visit every URL reachable from startUrl whose hostname matches urlparse(startUrl).hostname exactly.
  • Deduplicate visited URLs — the prompt states explicitly that no URL may be fetched twice. The link graph can contain cycles.
  • The interview environment provides htmlParser.getUrls(url) -> list[str] (LeetCode 1242-style). The supplied HtmlParser interface is List<String> getUrls(String url) (Java-style; the Python harness mirrors it).
  • Code must compile and run inside CodeSignal; tests pass when the returned set size is below a target threshold (often < 100).
  • Assume "all links use http, no port numbers" unless told otherwise.
  • The usual order is single-threaded first, a short discussion, then a concurrent rewrite.

Variants

Fragment-normalizing variant

URL fragments (everything after #) must be stripped before deduplication and comparison. A September 2026 phone screen tested different hostnames, fragment-bearing URLs, and cycles after the single-threaded implementation was converted to concurrent execution. Clarify the normalization contract before coding.

Pitfalls

  • Replit was used through mid-2025 and has since been replaced by CodeSignal. The IDE shows no linter errors before run; expect a few minutes of fixing imports on the first run.
  • Interviewers do not deduct for using Google or library docs, but they watch how you decide between ThreadPoolExecutor and asyncio — pick one and be able to defend it.
  • Common stumble: writing a custom async HTML parser instead of staying with the supplied getUrls API. Codec edge cases (0x89 PNG headers etc.) are not part of the spec.
  • The Q1 prompt blurb is shared with the image-processing variant; the recruiter usually does not tell you which one you'll get.
  • Two clarifying questions worth asking before coding: (1) how to treat URL fragments (http://x.com/p#section vs #section2 — same page or different?) and (2) whether URL normalization is in scope.
  • Concurrent visited race: if the membership check and the insert are not under the same lock, two workers can both pass the check and fetch the same URL twice. Dedupe before enqueueing, with check-and-insert atomic.
  • Termination is the easiest part to break: an empty queue does not mean the crawl is done, because another worker may still be parsing a page and about to enqueue new links. Stop only when the queue is empty and the in-flight worker count is zero — otherwise the crawler exits early or deadlocks.
  • The single-threaded version is not hard once the rules (fragment stripping, exact hostname, cycles) are straight; most time goes into the concurrent version.

Follow-ups

Preparation

  • Write the single-threaded BFS from scratch, then add a ThreadPoolExecutor wrapper with a Lock-guarded visited set in under 15 minutes.
  • Hand-write the locked concurrent version once and run it on a cyclic graph to confirm it neither deadlocks nor exits early (track queue size + in-flight count).
  • Practice an asyncio rewrite: asyncio.Semaphore, asyncio.gather, awaiting the parser as if it were async.
  • Be able to whiteboard the multi-machine sharding answer: central queue (Redis / SQS), consistent hashing on hostname, dedupe in a shared Bloom filter or Redis set, per-host rate limiting via token buckets.
  • Cross-train the image-processing sibling prompt in parallel — same recruiter blurb, different problem.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...