Fix a Flaky Expiry Test by Injecting a Controllable Clock

Last updated: November 29, 2026

Quick Overview

A TTL cache's unit tests pass locally but fail intermittently in CI because they call sleep and compare wall-clock times. Redesign the cache and tests so expiry is tested deterministically.

Google
Software Engineering Fundamentals
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Software Engineering Fundamentals
Easy

0

0

0 solved


A TTL cache's unit tests pass locally but fail intermittently in CI because they call sleep and compare wall-clock times. Redesign the cache and tests so expiry is tested deterministically.

Flaky tests waste huge amounts of CI time at Google's scale, and test hygiene is a frequent fundamentals topic. The interviewer checks whether you identify time as hidden nondeterminism and fix it with dependency injection rather than longer sleeps.

What the Interviewer Expects
  • Identifies real time as the source of flakiness
  • Injects a clock dependency with a sensible default
  • Uses a monotonic clock in production
  • Writes boundary tests at exactly the TTL
  • Avoids sleep in unit tests entirely
Key Topics to Cover
flaky tests
dependency injection
test doubles
determinism
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you handle code that schedules timers, not just reads time?
  • Where else does nondeterminism creep into tests?
  • How do you find flaky tests across a large repo?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • Cache entries expire ttl seconds after their last put.
  • Tests must be fast and deterministic, covering just before expiry, exactly at expiry, and refresh on overwrite.
  • No behavior change for pr...
High-Level Design

The original test did put, sleep(1.0), then asserted expiry with a 1 s TTL. On a loaded CI machine, scheduling jitter and wall-clock adjustments made the boundary unpredictable. The fix: the cache...


Submit Your Answer
Markdown supported