0
0
Rest APIprogramming~20 mins

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

Choose your learning style9 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.