AlgoMaster Logo
AlgoMasterEvaluate Shared HTTP Cache Policymedium

Evaluate Shared HTTP Cache Policy

medium

Shared caches such as CDNs and reverse proxies must decide whether a response may be stored and reused without exposing private data or serving it beyond its freshness lifetime.

Design a stateless SharedHttpCachePolicy class:

  • SharedHttpCachePolicy() creates an evaluator.
  • String evaluate(String method, int status, String cacheControl, int ageSeconds, boolean hasAuthorization) returns one of "BYPASS", "FRESH", "REVALIDATE", or "STALE".

Use this deliberately scoped policy model:

  1. Only GET and HEAD are eligible, compared case-insensitively.
  2. Parse comma-separated Cache-Control directives case-insensitively and ignore unknown directives.
  3. no-store or private returns "BYPASS".
  4. If hasAuthorization is true, the response requires public or s-maxage to proceed.
  5. These statuses are cacheable by default: 200, 203, 204, 206, 300, 301, 404, 405, 410, 414, and 501. Any other status requires public.
  6. An explicit lifetime is required. Use s-maxage=N when present; otherwise use max-age=N. If neither exists, return "BYPASS".
  7. no-cache returns "REVALIDATE" after the response passes the preceding checks.
  8. Otherwise return "FRESH" when ageSeconds < lifetime, or "STALE" when ageSeconds >= lifetime.

This exercise does not implement heuristic freshness, validators, stale-while-revalidate, or request directives.

Example 1:

Input:

Output:

Explanation: Freshness uses a strict comparison. At age 60, the 60-second lifetime has ended.

Example 2:

Input:

Output:

Explanation: Shared-cache privacy overrides freshness. no-cache differs from no-store: it allows storage but requires validation. The shared cache uses s-maxage=30, not max-age=120.

Constraints

  • method contains ASCII letters.
  • 100 <= status <= 599
  • cacheControl is a comma-separated list of supported or unknown token directives.
  • max-age and s-maxage values, when present, are integers in [0, 10^9] without quotes.
  • Each supported directive appears at most once.
  • 0 <= ageSeconds <= 10^9
  • At most 10^4 calls are made to evaluate.
Hints

Loading...
CallReturns
new SharedHttpCachePolicy()null
evaluate("GET", 200, "public, max-age=60", 30, false)"FRESH"
evaluate("GET", 200, "public, max-age=60", 60, false)"STALE"

The response can be served while age is strictly below 60 seconds. At the exact freshness boundary it is stale.

Run checks these cases. Submit also runs a larger hidden set.