Bird
0
0

Identify the error in this caching code:

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

Identify the error in this caching code:

cache = {}
def compute(x):
    if x in cache:
        return cache[x]
    result = x + 5
    return result
AThe function does not return any value
BThe cache dictionary is not initialized
CThe result is not stored in the cache after computation
DThe cache is cleared before each call
Step-by-Step Solution
Solution:
  1. Step 1: Check cache usage after computing result

    Result is computed but never saved to cache.
  2. Step 2: Identify missing cache update

    Must add cache[x] = result before returning.
  3. Final Answer:

    The result is not stored in the cache after computation -> Option C
  4. Quick Check:

    Cache update missing = error [OK]
Quick Trick: Always save computed result to cache [OK]
Common Mistakes:
  • Forgetting to store result in cache
  • Assuming cache auto-updates
  • Ignoring cache initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes