Retry a flaky operation with a cap

Python data engineering interview problem. Difficulty: intermediate. Pattern: Recursion. About 15 minutes. Part of the Pro drill bank.

A downstream HTTP call flakes. The job retries a few times and then gives up with a defined result, rather than hanging forever.

A flaky service sometimes fails. A job tries again a few times, and it should use the first attempt that works. In this exercise the attempts are already given as a list, and a failed attempt is marked with the text '__fail__'. Go through the attempts in order and return the first one that is not '__fail__'. If every attempt failed (or there are no attempts), return None. Write retry_until_ok(attempts). For ['__fail__', 9] the first attempt failed and the second gave 9, so the answer is 9. For ['__fail__', '__fail__'] everything failed, so the answer is None. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Check the attempts in order.
  • Return the first value that is not '__fail__'.
  • Stop as soon as one works.
  • Return None if all attempts failed.

Constraints

  • attempts is a list of results.
  • '__fail__' is the only failure marker.
  • Falsy results like 0 or '' are real successes.

Examples

Input: retry_until_ok(['__fail__', 9]) Output: 9 This input follows the stated rules and produces this output.

Topics: lakebench, python, error-handling.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Retry a flaky operation with a cap

Interview-style drill: Write a function that retries a failing operation up to a maximum number of attempts before giving up.

Retry a flaky operation with a cap and return the first success (or the fallback the tests define).