Format: 60 min, live coding in CoderPad.
Problem: You're given a base URL for a "maze" served over HTTP. A GET on the base URL returns JSON with a list of next step IDs:
{"next_steps": ["ABC123..."]}
You then GET base_url/{step_id} for each step, which returns more next steps. You need to explore until you find the exit. Things to handle:
- Dead ends:
{"next_steps": []} - Flaky server: some requests return Service Temporarily Unavailable (503), so you need to retry
- The exit: the response contains a message field instead of more steps
- Possible cycles, so track visited step IDs
It's really a graph traversal problem dressed up as an API exercise. Most of the time goes to the HTTP client and JSON parsing, not the algorithm.
My approach: Recursive DFS with a visited set. The base cases are: already visited, found the exit, and dead end. Otherwise recurse on each next step and bubble the result up as soon as one is found. The fetch helper loops and retries on 503. I should have done BFS.
No pass.
Discussion
Loading comments…