Bird
0
0

Consider this simplified server logic for handling POST requests with idempotency keys:

medium📝 Predict Output Q13 of 15
Rest API - Advanced Patterns
Consider this simplified server logic for handling POST requests with idempotency keys:
stored_keys = {}

def handle_request(idempotency_key, data):
    if idempotency_key in stored_keys:
        return stored_keys[idempotency_key]
    response = process_data(data)
    stored_keys[idempotency_key] = response
    return response

# Client sends:
print(handle_request('abc123', 'order1'))
print(handle_request('abc123', 'order1'))
print(handle_request('xyz789', 'order2'))
What will be the output?
ANone, None, None
Bprocess_data output for order1, None, process_data output for order2
Cprocess_data output for order1, process_data output for order2, process_data output for order2
Dprocess_data output for order1, process_data output for order1, process_data output for order2
Step-by-Step Solution
Solution:
  1. Step 1: Analyze first call with key 'abc123'

    Key not in stored_keys, so process_data('order1') runs, result saved and returned.
  2. Step 2: Analyze second call with same key 'abc123'

    Key found, stored response returned without reprocessing.
  3. Step 3: Analyze third call with new key 'xyz789'

    Key not found, process_data('order2') runs, result saved and returned.
  4. Final Answer:

    process_data output for order1, process_data output for order1, process_data output for order2 -> Option D
  5. Quick Check:

    Repeated key returns cached response [OK]
Quick Trick: Repeated keys return cached response, new keys process data [OK]
Common Mistakes:
MISTAKES
  • Assuming second call reprocesses data
  • Confusing keys and responses
  • Thinking None is returned on repeats

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes