Bird
0
0

Find the error in this caching code and select the fix:

medium📝 Debug Q14 of 15
Agentic AI - Production Agent Architecture

Find the error in this caching code and select the fix:

cache = {}
def get_result(x):
    if x in cache:
        return cache[x]
    result = compute(x)
    return result
ARemove the cache dictionary entirely.
BChange 'if x in cache' to 'if x not in cache'.
CAdd 'cache[x] = result' before returning result.
DReturn 'cache[x]' without checking if key exists.
Step-by-Step Solution
Solution:
  1. Step 1: Identify missing cache update

    The function returns computed result but never saves it to cache, so caching fails.
  2. Step 2: Fix by saving result in cache

    Insert 'cache[x] = result' before returning to store computed value.
  3. Final Answer:

    Add 'cache[x] = result' before returning result. -> Option C
  4. Quick Check:

    Cache must store new results [OK]
Quick Trick: Always save new results to cache before returning [OK]
Common Mistakes:
  • Forgetting to update cache after compute
  • Reversing cache check condition
  • Ignoring cache and recomputing every time

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes