0
0
Rest APIprogramming~20 mins

Retry and failure handling in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Retry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this retry logic snippet?

Consider this pseudocode for retrying a REST API call up to 3 times on failure. What will be the final output if the API fails twice and succeeds on the third try?

Rest API
attempts = 0
max_attempts = 3
success = False
while attempts < max_attempts and not success:
    attempts += 1
    if attempts < 3:
        print(f"Attempt {attempts}: Failed")
    else:
        print(f"Attempt {attempts}: Success")
        success = True
if success:
    print("Result: Success")
else:
    print("Result: Failure")
AAttempt 1: Failed\nAttempt 2: Failed\nAttempt 3: Success\nResult: Success
BAttempt 1: Failed\nAttempt 2: Failed\nResult: Failure
CAttempt 1: Failed\nAttempt 2: Success\nResult: Success
DAttempt 1: Success\nResult: Success
Attempts:
2 left
💡 Hint

Look at how the loop increments attempts and when it sets success to True.

🧠 Conceptual
intermediate
1:30remaining
Which HTTP status code should trigger a retry in REST API calls?

When implementing retry logic for REST API calls, which HTTP status code is generally appropriate to trigger a retry?

A200 OK
B500 Internal Server Error
C404 Not Found
D401 Unauthorized
Attempts:
2 left
💡 Hint

Think about server errors vs client errors.

🔧 Debug
advanced
2:30remaining
Why does this retry code cause an infinite loop?

Examine the following retry code snippet. Why does it cause an infinite loop?

Rest API
max_retries = 3
retry_count = 0
success = False
while not success:
    try:
        # simulate API call
        if retry_count < max_retries:
            raise Exception("API failure")
        else:
            success = True
    except Exception as e:
        print(f"Retry {retry_count}: {e}")
Amax_retries is too high causing long loops
BException is not caught properly
Csuccess is never set to True inside the try block
Dretry_count is never incremented, so the loop never ends
Attempts:
2 left
💡 Hint

Check if the retry_count changes inside the loop.

📝 Syntax
advanced
2:30remaining
What error does this retry decorator code raise?

Consider this Python decorator for retrying a function. What error will it raise when used?

Rest API
def retry(func):
    def wrapper(*args, **kwargs):
        for i in range(3):
            try:
                return func(*args, **kwargs)
            except Exception:
                pass
        return wrapper

@retry
def fail_once():
    fail_once.counter += 1
    if fail_once.counter < 2:
        raise Exception("Fail")
    return "Success"

fail_once.counter = 0
print(fail_once())
AAttributeError: 'function' object has no attribute 'counter'
BTypeError: 'function' object is not iterable
CRecursionError: maximum recursion depth exceeded
DNone (prints 'Success')
Attempts:
2 left
💡 Hint

Look at what the decorator returns after retries fail.

🚀 Application
expert
3:00remaining
How many times will the API be called in this exponential backoff retry?

Given this pseudocode implementing exponential backoff retry for a REST API call, how many times will the API be called before the function returns?

Rest API
def call_api_with_retry():
    max_attempts = 4
    delay = 1
    for attempt in range(1, max_attempts + 1):
        response = api_call()
        if response == 'success':
            return attempt
        else:
            sleep(delay)
            delay *= 2
    return max_attempts

# Assume api_call() returns 'fail' for first 3 calls, then 'success' on 4th call.
A4
B3
C5
D1
Attempts:
2 left
💡 Hint

Count how many times the loop runs until success.