Retry a rate limit, not a bad request

Retry a rate limit, not a bad request

Databricks LLM Interview Question

Databricks runs model calls in bulk, and an API answering thousands of them will fail some of the time. Each failure comes back as a status code, and the code says whose fault it was. A rate limit or a server error is the API's own, and the same request will usually work on a second try. A request the API could not read is yours, and it will be just as wrong every time you send it.

Write a function requests_sent(statuses, max_attempts) that returns how many requests a correct client sends before it stops. The statuses are what a test server is set up to answer, one for each request it receives, and the attempts are the most the client is allowed to send. The list can run on past the point your client stops. A status of 200 means the request worked and there is nothing left to send. A 429, or anything from 500 up, is worth sending again, and any other status ends it there.

Asked of

  • AI Engineer
  • ML Engineer
  • Data Scientist

Example 1

Input

statuses = [429, 200], max_attempts = 3

Output

2

Example 2

Input

statuses = [400, 200], max_attempts = 3

Output

1

Example 3

Input

statuses = [500, 503, 429], max_attempts = 3

Output

3

Explanation

In the first example the rate limit clears and the second request works, so two were sent. In the second the server could not read the request, so the client stops there and never reaches the success sitting behind it in the list. In the third every failure was one worth repeating, so the budget ran out first.

Submit also runs 3 hidden test cases that check edge cases.

Similar questions