Make a Flaky Delivery Date Test Deterministic by Injecting a Clock
Last updated: November 29, 2026
Quick Overview
A unit test for the delivery promise date passes most of the day but fails when CI runs near midnight UTC. Explain why and refactor the service and tests so time is injected and the tests are deterministic.
Amazon
November 29, 20260
0
0 solved
A unit test for the delivery promise date passes most of the day but fails when CI runs near midnight UTC. Explain why and refactor the service and tests so time is injected and the tests are deterministic.
Amazon's delivery promise logic depends on order cutoffs and time zones, and flaky tests block pipelines for every team sharing a deployment pipeline. This checks whether you can diagnose hidden nondeterminism and fix it by design rather than by adding retries to the test.
What the Interviewer Expects
- Identifies the direct call to the system clock as hidden input
- Introduces a clock abstraction passed into the service
- Writes tests that pin exact boundary times such as one minute before cutoff
- Uses timezone-aware datetimes
- Rejects test retries or sleeps as the fix
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- What other sources of flakiness are common in service tests?
- How would you test daylight saving transitions for a local cutoff?
- How would you find flaky tests across a large codebase automatically?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Orders before 22:00 UTC ship today; later orders ship tomorrow.
- Promise date is ship date plus transit days.
- Tests must give the same result whenever they run....
High-Level Design
The flaky version calls datetime.now() inside promise_date, and the test computes its expected value from another now() call made a moment earlier, so around the cutoff (or across midnight) the ...