Timezone-Naive Datetime
Problem
A session service stamps each session with an issue time and later decides whether it has expired:
from datetime import datetime, timedelta
SESSION_TTL = timedelta(hours=8)
def issue_session(user_id: str) -> dict:
return {"user": user_id, "issued_at": datetime.utcnow().isoformat()}
def is_expired(session: dict) -> bool:
issued = datetime.fromisoformat(session["issued_at"])
return datetime.now() - issued > SESSION_TTLSymptom: CI is green and the US-West deployment behaves, except that sessions there last about 15 hours instead of 8. The Mumbai deployment is the real problem: users are logged out about two and a half hours after signing in. Both regions run the same container image. Explain both numbers and fix the code.
Examples
- In CI (containers run in UTC): a session issued at
09:00is checked at16:59→ not expired; at17:01→ expired. Correct, but only becauseutcnow()andnow()agree when the local zone is UTC. - Mumbai host (UTC+05:30):
issued_atis09:00(UTC). At the same instantdatetime.now()returns14:30local. The session already looks 5.5 hours old, so it "expires" after 8 − 5.5 = 2.5 hours of real time. - US-West host (UTC−07:00): at issue time
now()reads02:00, 7 hours behind the stored UTC value. The session appears to have negative age and lasts 8 + 7 = 15 hours.
Hints
utcnow()andnow()return the same type. Do they represent the same instant on a machine whose clock is not set to UTC?- What information is missing from
'2024-05-01T09:00:00'that would let a reader know which instant it refers to? - Make the timestamps unambiguous first, then make comparisons refuse anything ambiguous.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.