Bird
Raised Fist0
Agentic AIml~20 mins

Caching and result reuse in Agentic AI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Caching Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is caching important in agentic AI workflows?

Imagine you have an AI agent that performs multiple steps to answer a question. Why would caching intermediate results help?

AIt makes the agent forget previous steps faster.
BIt increases the randomness of the agent's answers.
CIt reduces repeated calculations, saving time and resources.
DIt forces the agent to always start from scratch.
Attempts:
2 left
💡 Hint

Think about how computers avoid doing the same work twice.

Predict Output
intermediate
2:00remaining
What is the output of this caching example?

Consider this Python code simulating caching in an AI agent:

cache = {}
def expensive_step(x):
    if x in cache:
        return cache[x]
    result = x * x  # Simulate expensive calculation
    cache[x] = result
    return result

print(expensive_step(3))
print(expensive_step(3))
print(expensive_step(4))

What will be printed?

A
9
9
9
B
9
None
16
C
3
3
4
D
9
9
16
Attempts:
2 left
💡 Hint

Check if the function uses cached results for repeated inputs.

Model Choice
advanced
2:30remaining
Which caching strategy best suits agentic AI with changing inputs?

You have an AI agent that processes user queries with slight variations. Which caching method helps reuse results without returning outdated answers?

ACache results with approximate matching and expiration time.
BDo not use caching at all.
CCache all results indefinitely without checking input.
DCache results with exact input keys only.
Attempts:
2 left
💡 Hint

Think about balancing reuse and freshness of results.

Metrics
advanced
1:30remaining
How does caching affect AI agent performance metrics?

An AI agent uses caching to store intermediate results. Which metric is most directly improved by caching?

AModel accuracy on test data
BInference latency (time to get a result)
CTraining loss during model training
DNumber of model parameters
Attempts:
2 left
💡 Hint

Consider what caching saves in repeated computations.

🔧 Debug
expert
3:00remaining
Why does this caching code cause incorrect reuse?

Review this Python snippet for caching AI agent results:

cache = {}
def process(input_data):
    if 'result' in cache:
        return cache['result']
    result = input_data + 1
    cache['result'] = result
    return result

print(process(5))
print(process(10))

What is the problem with this caching approach?

AIt caches only one result regardless of input, causing wrong reuse.
BIt does not cache any results, so no reuse happens.
CIt raises a KeyError because 'result' key is missing.
DIt caches results correctly for each input.
Attempts:
2 left
💡 Hint

Check how the cache key is used for different inputs.

Practice

(1/5)
1.

What is the main benefit of caching in AI tasks?

easy
A. It saves time by reusing previous results.
B. It increases the size of the dataset.
C. It makes the model more complex.
D. It reduces the accuracy of predictions.

Solution

  1. Step 1: Understand caching purpose

    Caching stores results from previous computations to avoid repeating the same work.
  2. Step 2: Identify the benefit

    Reusing cached results saves time and speeds up AI tasks.
  3. Final Answer:

    It saves time by reusing previous results. -> Option A
  4. Quick Check:

    Caching benefit = Saves time [OK]
Hint: Caching means saving results to avoid repeat work [OK]
Common Mistakes:
  • Thinking caching increases dataset size
  • Believing caching reduces accuracy
  • Confusing caching with model complexity
2.

Which Python code snippet correctly checks if a result is cached before computing?

cache = {}
key = 'input1'
# What to do next?
easy
A. if cache.has_key(key): result = cache[key] else: result = compute() cache[key] = result
B. if key in cache: result = cache[key] else: result = compute() cache[key] = result
C. if key not in cache: result = cache[key] else: result = compute() cache[key] = result
D. if cache[key]: result = cache[key] else: result = compute() cache[key] = result

Solution

  1. Step 1: Check Python dictionary membership

    Use 'if key in cache' to check if key exists in dictionary.
  2. Step 2: Use correct syntax to assign or compute

    If key exists, get cached result; else compute and save it.
  3. Final Answer:

    if key in cache: result = cache[key] else: result = compute() cache[key] = result -> Option B
  4. Quick Check:

    Python dict membership uses 'in' keyword [OK]
Hint: Use 'if key in dict' to check cache presence [OK]
Common Mistakes:
  • Using deprecated has_key() method
  • Checking 'if cache[key]' without key check
  • Reversing condition logic
3.

What will be the output of this code?

cache = {}
def compute(x):
    print(f"Computing {x}")
    return x * 2

inputs = [1, 2, 1]
results = []
for i in inputs:
    if i in cache:
        results.append(cache[i])
    else:
        val = compute(i)
        cache[i] = val
        results.append(val)
print(results)
medium
A. [1, 2, 1]
B. [2, 4, 4]
C. [2, 2, 2]
D. [2, 4, 2]

Solution

  1. Step 1: Trace the loop and caching behavior

    For input 1: not cached, compute(1)=2, cache[1]=2, results=[2]. For input 2: not cached, compute(2)=4, cache[2]=4, results=[2, 4]. For input 1 again: cached, results append cache[1]=2, results=[2, 4, 2].
  2. Step 2: Confirm final results list

    The final printed list is [2, 4, 2].
  3. Final Answer:

    [2, 4, 2] -> Option D
  4. Quick Check:

    Cache reuse returns previous result [OK]
Hint: Cached inputs skip compute, reuse stored value [OK]
Common Mistakes:
  • Assuming compute runs for repeated input
  • Mixing up cached values
  • Ignoring print output side effect
4.

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
medium
A. Remove the cache dictionary entirely.
B. Change 'if x in cache' to 'if x not in cache'.
C. Add 'cache[x] = result' before returning result.
D. Return 'cache[x]' without checking if key exists.

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]
Hint: 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
5.

You want to speed up an AI agent that processes user queries by caching results. Which strategy best balances memory use and speed?

  • A. Cache all results forever.
  • B. Cache only recent results with a size limit.
  • C. Never cache, always compute fresh results.
  • D. Cache results but never check before computing.
hard
A. Cache only recent results with a size limit.
B. Cache all results forever.
C. Never cache, always compute fresh results.
D. Cache results but never check before computing.

Solution

  1. Step 1: Understand caching trade-offs

    Caching all results forever uses unlimited memory, which is impractical.
  2. Step 2: Choose a balanced caching strategy

    Limiting cache size to recent results saves memory and keeps speed benefits.
  3. Final Answer:

    Cache only recent results with a size limit. -> Option A
  4. Quick Check:

    Limited cache balances memory and speed [OK]
Hint: Limit cache size to keep memory use reasonable [OK]
Common Mistakes:
  • Caching everything without limit
  • Not checking cache before computing
  • Skipping caching entirely