Find final URL by crawling until “congrats”
Company: Ramp
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's competency in implementing robust HTTP-based crawling and traversal logic, including response parsing, retry and error handling, and termination detection.
Constraints
- 0 <= len(web) <= 10^4
- 0 <= max_retries <= 10
- The total number of URLs across all `urls` lists is at most 2 * 10^4
- Each URL in a `urls` list should be processed at most once, except for internal retries caused by HTTP 503
Examples
Input: ("http://start", {"http://start": {"urls": ["http://a", "http://b"], "meta": 1}, "http://a": {"message": "dead end"}, "http://b": "congrats"}, 2)
Expected Output: "http://b"
Explanation: Start returns two next URLs. `http://a` is a dead end, and `http://b` returns `congrats`, so the answer is that final URL.
Input: ("s", {"s": {"urls": ["r"]}, "r": [{"status": 503}, {"status": 503}, "congrats"]}, 2)
Expected Output: "r"
Explanation: URL `r` returns HTTP 503 twice, then succeeds on the third attempt. With `max_retries = 2`, this is allowed.
Input: ("u1", {"u1": {"urls": ["u2"]}, "u2": {"urls": ["u1", "u3"]}, "u3": {"info": "nothing here"}}, 1)
Expected Output: None
Explanation: The crawl contains a cycle between `u1` and `u2`, but no reachable response ever equals `congrats`.
Input: ("done", {"done": "congrats"}, 3)
Expected Output: "done"
Explanation: The starting URL already returns `congrats`, so it is immediately the answer.
Input: ("root", {"root": {"urls": ["bad", "slow"]}, "bad": [[1, 2, 3]], "slow": [{"status": 503}, {"status": 503}, {"status": 503}]}, 1)
Expected Output: None
Explanation: `bad` returns a malformed body (a list body, treated as a dead end). `slow` keeps returning 503 more times than allowed by `max_retries`, so no success is found.
Hints
- Model the crawl as a graph traversal: each URL is a node, and each `urls` list gives outgoing edges.
- Keep retry logic separate from your visited set: a 503 means retry the same URL, while cycle detection prevents revisiting URLs discovered through links.