Design a Cookie Manager with Domain and Path Matching, Expiry, and Security Flags
Company: Zoom
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a `CookieManager` class that simulates browser cookie management. The manager supports setting cookies with optional expiration times and security flags, retrieving cookie values, and deleting cookies.
Cookies are scoped by domain and path. A cookie is accessible if the request domain matches the cookie's domain exactly or is a subdomain of it (for example, `sub.example.com` matches cookie domain `example.com`), and the request path starts with the cookie's path at a `/` boundary. The boundary rule means the two paths are equal, or the cookie path ends with `/`, or the character in the request path right after the cookie path is `/`. So cookie path `/docs` matches `/docs` and `/docs/intro`, but not `/docsearch`.
Expired cookies are never returned. When multiple cookies with the same name match a request, return the one with the longest (most specific) path. If there is still a tie, return the most recently set cookie. Secure cookies are only accessible over secure connections, and httpOnly cookies are only accessible via HTTP requests.
Implement the following methods:
- `set_cookie(domain: str, path: str, name: str, value: str, expires: int, http_only: bool, secure: bool) -> None`: set a cookie with the given domain, path, name, value, expiration timestamp (-1 for no expiration), httpOnly flag and secure flag. It overwrites any existing cookie with the same domain, path and name.
- `get_cookie(domain: str, path: str, name: str, current_time: int, is_secure_connection: bool, is_http_request: bool) -> str`: return the value of the best-matching cookie for the given domain, path and name at `current_time`, taking the secure and httpOnly flags into account. Return an empty string if no cookie matches.
- `delete_cookie(domain: str, path: str, name: str) -> bool`: delete the cookie with exactly this domain, path and name. Return `True` if the cookie was found and deleted, `False` otherwise.
For example, after these three calls:
```text
set_cookie("example.com", "/", "sid", "root", -1, False, False)
set_cookie("example.com", "/docs", "sid", "docs", 2000, False, True)
set_cookie("sub.example.com", "/docs", "sid", "sub", -1, True, False)
```
the following calls, made in this order, return:
```text
get_cookie("sub.example.com", "/docs/intro", "sid", 1000, True, True) -> "sub"
get_cookie("sub.example.com", "/docs/intro", "sid", 1000, True, False) -> "docs"
get_cookie("example.com", "/docsearch", "sid", 1000, True, True) -> "root"
get_cookie("example.com", "/docs", "sid", 2000, True, True) -> "root"
delete_cookie("example.com", "/", "sid") -> True
get_cookie("example.com", "/docs", "sid", 2000, True, True) -> ""
```
The first `get_cookie` call returns "sub" because two cookies share the longest matching path `/docs` and the `sub.example.com` one was set later. The second call is not an HTTP request, so the httpOnly cookie is skipped. `/docsearch` does not continue `/docs` at a `/` boundary, so only the root cookie applies. At time 2000 the `/docs` cookie on `example.com` has expired, and the `sub.example.com` cookie is not visible from the parent domain. Once the root cookie is deleted, nothing matches.
```hint Look-alike domains
A plain suffix check lets `badexample.com` match a cookie set on `example.com`. Decide what has to sit right before the suffix.
```
```hint Path prefixes
Test your path check on `/docs` against `/docsearch`, and on a cookie path that ends in `/`, such as `/docs/` against `/docs/intro`, before you trust a bare prefix test.
```
```hint Recency without a clock
`set_cookie` receives no current time, so "most recently set" has to be something you record yourself at write time.
```
### Constraints & Assumptions
- `1 <= len(domain) <= 100`, `1 <= len(name) <= 100`, `1 <= len(value) <= 1000`.
- Every path starts with `/`.
- `expires` is a positive Unix timestamp or `-1` (never expires). A cookie with expiry `t` is expired at any `current_time >= t`.
- `http_only` and `secure` are booleans.
- At most 10,000 operations in total.
### Clarifying Questions to Ask
- Does overwriting an existing cookie make it the most recently set one for the tie-break?
- Should domains and names be compared case-insensitively, the way browsers treat host names?
- Should `delete_cookie` return `True` for a cookie that is still stored but has already expired?
- Can a cookie domain arrive with a leading dot, as in `.example.com`, and should it be normalized?
### What a Strong Answer Covers
- The storage key and how it makes overwrite and exact delete cheap.
- Domain matching that accepts exact and subdomain matches and rejects look-alike suffixes.
- Path matching at `/` boundaries, including cookie paths that end in `/`.
- The expiry boundary, the `-1` case, and the secure and httpOnly filters.
- A deterministic tie-break for equal path lengths, and the cost of each method.
### Follow-up Questions
- How would you make `get_cookie` independent of how many cookies share a name?
- How would you remove expired cookies so memory does not grow without bound, and what does that change for `delete_cookie`?
- Browsers send every matching cookie in one `Cookie` header. How would you return all matches in the order browsers use?
- How would you cap the number of cookies per domain and evict the least recently used one?
Overview: Design a CookieManager class that stores browser cookies and returns the best match for a request using domain, path, expiry, secure and httpOnly rules. It tests string matching at domain and path boundaries, deterministic tie-breaking, and a storage layout that keeps set and delete cheap.