Bird
Raised Fist0
Rest APIprogramming~20 mins

Expiration-based caching in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Expiration Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this cache expiration check?

Consider a REST API cache that stores data with an expiration timestamp. Given the following code snippet, what will be the output?

Rest API
import time

cache = {'data': 'value', 'expires_at': time.time() + 2}

# Wait 3 seconds to simulate delay
time.sleep(3)

if time.time() > cache['expires_at']:
    print('Cache expired')
else:
    print('Cache valid')
ATypeError
BCache expired
CKeyError
DCache valid
Attempts:
2 left
💡 Hint

Think about how the expiration time compares to the current time after the delay.

🧠 Conceptual
intermediate
1:00remaining
Which HTTP header is commonly used to control cache expiration?

In REST APIs, which HTTP header tells clients how long to keep a cached response before requesting it again?

ACache-Control
BAuthorization
CContent-Type
DUser-Agent
Attempts:
2 left
💡 Hint

It controls caching policies like max-age and no-cache.

🔧 Debug
advanced
2:00remaining
Why does this cache never expire?

Look at this cache expiration code snippet. Why does the cache never expire?

Rest API
import time

cache = {'data': 'value', 'expires_at': time.time() + 5}

# Check expiration
if time.time() < cache['expires_at']:
    print('Cache expired')
else:
    print('Cache valid')
ABecause the comparison operator is reversed, so it prints 'Cache expired' when cache is still valid
BBecause the expiration time is set in the past
CBecause time.time() returns a string, causing a TypeError
DBecause the cache dictionary is missing the 'data' key
Attempts:
2 left
💡 Hint

Check the logic of the if condition comparing current time and expiration time.

📝 Syntax
advanced
1:30remaining
Which option correctly sets a cache expiration time 10 seconds from now?

Choose the correct Python code to set a cache expiration timestamp 10 seconds in the future.

Acache['expires_at'] = time.now() + 10
Bcache['expires_at'] = time.time() - 10
Ccache['expires_at'] = time.time() + 10
Dcache['expires_at'] = time.time + 10
Attempts:
2 left
💡 Hint

Remember the correct function to get current time in seconds is time.time()

🚀 Application
expert
2:30remaining
How many items remain in cache after expiration cleanup?

Given this cache dictionary with expiration timestamps, how many items remain after removing expired entries?

Rest API
import time

cache = {
    'a': {'value': 1, 'expires_at': time.time() + 5},
    'b': {'value': 2, 'expires_at': time.time() - 1},
    'c': {'value': 3, 'expires_at': time.time() + 10},
    'd': {'value': 4, 'expires_at': time.time() - 10}
}

# Remove expired
now = time.time()
cache = {k: v for k, v in cache.items() if v['expires_at'] > now}

print(len(cache))
A1
B3
C4
D2
Attempts:
2 left
💡 Hint

Check which items have expiration times greater than current time.

Practice

(1/5)
1. What does the Cache-Control: max-age header do in expiration-based caching?
easy
A. It deletes the cache immediately after the response.
B. It disables caching for the resource.
C. It sets how long the cached data is considered fresh.
D. It forces the client to always fetch new data.

Solution

  1. Step 1: Understand the role of Cache-Control header

    The Cache-Control header controls caching behavior for HTTP responses.
  2. Step 2: Identify the meaning of max-age directive

    The max-age directive specifies the time in seconds that the response is considered fresh and can be served from cache.
  3. Final Answer:

    It sets how long the cached data is considered fresh. -> Option C
  4. Quick Check:

    max-age = cache freshness duration [OK]
Hint: Remember max-age sets cache freshness time in seconds [OK]
Common Mistakes:
  • Confusing max-age with disabling cache
  • Thinking max-age deletes cache immediately
  • Assuming max-age forces fresh fetch every time
2. Which of the following is the correct syntax to set a cache expiration of 300 seconds in an HTTP response header?
easy
A. Cache-Control: maxage: 300
B. Cache-Control: max-age=300
C. Cache-Control: expire=300
D. Cache-Control: timeout=300

Solution

  1. Step 1: Recall correct Cache-Control syntax

    The Cache-Control header uses directives separated by commas, with max-age specified as max-age=seconds.
  2. Step 2: Compare options to correct syntax

    Only Cache-Control: max-age=300 uses the correct format: max-age=300.
  3. Final Answer:

    Cache-Control: max-age=300 -> Option B
  4. Quick Check:

    Correct header syntax = max-age=seconds [OK]
Hint: Use equal sign (=) with max-age, no spaces [OK]
Common Mistakes:
  • Using colon instead of equal sign
  • Using incorrect directive names like expire or timeout
  • Adding spaces inside the directive
3. Given this HTTP response header:
Cache-Control: max-age=10
What will happen if a client requests the same resource 5 seconds later?
medium
A. The client will use the cached response without contacting the server.
B. The client will ignore the cache and fetch new data from the server.
C. The client will receive a 404 error.
D. The client will refresh the cache but still use old data.

Solution

  1. Step 1: Understand max-age meaning

    max-age=10 means the cached data is fresh for 10 seconds after the first fetch.
  2. Step 2: Check time elapsed since caching

    5 seconds is less than 10 seconds, so cache is still fresh and valid.
  3. Final Answer:

    The client will use the cached response without contacting the server. -> Option A
  4. Quick Check:

    Elapsed time < max-age = use cache [OK]
Hint: If request time < max-age, cache is used [OK]
Common Mistakes:
  • Assuming cache expires immediately
  • Thinking client always fetches new data
  • Confusing max-age with cache invalidation
4. You set Cache-Control: max-age=60 but clients still fetch new data every time. What is the likely mistake?
medium
A. The client does not support caching headers.
B. The max-age value should be in milliseconds, not seconds.
C. The resource URL is missing a file extension.
D. The server is sending no-cache header overriding max-age.

Solution

  1. Step 1: Check for conflicting headers

    If the server sends a no-cache directive, it forces clients to revalidate or fetch fresh data despite max-age.
  2. Step 2: Understand max-age units and client support

    max-age is in seconds, and most clients support caching headers, so these are unlikely causes.
  3. Final Answer:

    The server is sending no-cache header overriding max-age. -> Option D
  4. Quick Check:

    no-cache overrides max-age [OK]
Hint: Check for no-cache header overriding max-age [OK]
Common Mistakes:
  • Confusing seconds with milliseconds for max-age
  • Blaming client support without checking headers
  • Assuming URL format affects caching
5. You want to cache API responses for 5 minutes but ensure clients always get fresh data after expiration. Which header setup is best?
hard
A. Cache-Control: max-age=300, must-revalidate
B. Cache-Control: max-age=300, no-store
C. Cache-Control: no-cache, max-age=300
D. Cache-Control: max-age=0, public

Solution

  1. Step 1: Understand caching for 5 minutes

    max-age=300 sets cache freshness for 300 seconds (5 minutes).
  2. Step 2: Ensure fresh data after expiration

    must-revalidate forces clients to check with server after max-age expires before using cached data.
  3. Step 3: Evaluate other options

    no-store disables caching, no-cache forces revalidation every time, max-age=0 means no caching.
  4. Final Answer:

    Cache-Control: max-age=300, must-revalidate -> Option A
  5. Quick Check:

    max-age + must-revalidate = cache then fresh after expiry [OK]
Hint: Use must-revalidate with max-age for fresh data after expiry [OK]
Common Mistakes:
  • Using no-store disables caching completely
  • Using no-cache forces revalidation every request
  • Setting max-age=0 disables caching
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard