KatInterview

Crawl a Web-Served Labyrinth Through Unreliable Responses

Trees & GraphsModel answer
MediumModel answerTrees & Graphsgraph-traversalbfshttp

Last updated

Write a crawler that explores a maze of HTTP endpoints to find the exit, coping with cycles, transient 503 errors and malformed JSON, while guaranteeing termination.

You are handed the entrance URL of a maze that is served over HTTP. Each location in the maze is its own endpoint. Sending a GET request to a location returns a JSON object with a next_stops key holding a list of paths you can move to, for example (illustrative paths):

{"next_stops": ["/rooms/17", "/rooms/42"]}

To reach a neighbouring location, join each path onto the host of the entrance URL and request the result. The exit is the location whose response body is:

{"message": "congrats"}

Write a program that accepts the entrance URL and finds the exit.

Ground rules

  • No one will explain how the service behaves. You are expected to call it, look at what comes back, and infer the rules yourself. In particular, nobody will tell you what a 503 means or how to treat a body that is not valid JSON.
  • The maze can contain cycles, and many routes may lead to the same URL.
  • Responses can be HTTP errors (such as 503) or bodies that fail JSON parsing. You decide, based on experimentation, how each is handled.
  • You may send as many requests as you like, but the program must always terminate.

What to deliver

Describe and implement your approach, and be ready to explain:

  1. How you avoid revisiting locations and looping forever.
  2. How you build each next URL from the host and a path.
  3. How you classify and respond to each kind of failure, including how many retries you allow.
  4. How you learn the service's behaviour rather than assume it.
  5. What your program does if no exit can be found, and what it costs in requests.

Hints

Hint 1

Model the maze as a directed graph whose nodes are URLs. Use a visited set (or a set of 'seen' URLs added when enqueued) so cycles and repeated routes cost nothing.

Hint 2

Do not treat every failure the same. A 503 that succeeds on a second try is transient and deserves a bounded retry with backoff; a body that is still unparseable after a few tries is likely permanent, so log it and skip that node instead of retrying forever.

Hint 3

Before finalising the logic, write a small probe that logs status code, headers and raw body for the first few hundred calls. Use what you see to decide retry counts and whether paths are always host-relative, and add a global request budget so the program always terminates.