Make Surge Quote Expiry Testable With an Injectable Clock

Last updated: November 29, 2026

Quick Overview

A surge quote must be honored for two minutes after it is shown, and the existing tests for it sleep and still flake. Refactor the code so time is injected, and write deterministic unit tests including the exact expiry boundary.

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

0

0

0 solved


A surge quote must be honored for two minutes after it is shown, and the existing tests for it sleep and still flake. Refactor the code so time is injected, and write deterministic unit tests including the exact expiry boundary.

Uber pricing code is full of time windows (quote validity, cancellation grace, wait-time fees), and tests that call the real clock are a classic source of flaky CI. The interviewer checks dependency injection for time, boundary thinking, and clean test structure.

What the Interviewer Expects
  • Explain why sleeping and wall-clock calls make tests slow and flaky
  • Inject a clock dependency with a production default
  • Write a fake clock that tests can advance
  • Define and test the boundary explicitly (valid strictly before expiry)
  • Keep the public API unchanged for callers
Key Topics to Cover
Dependency injection
Test doubles
Flaky tests
Boundary conditions
Time handling
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
  • Should expiry use wall-clock or monotonic time, and why?
  • How would you test this across a daylight saving change?
  • Where else in a pricing service would you apply the same pattern?
  • How do you test the code that uses the real system clock?
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

SurgeQuote(multiplier, ttl) reports is_valid() and picks the quoted or live multiplier at booking. Tests must run in milliseconds, never sleep, and cover just-before, exactly-at, and well-after ex...

High-Level Design

The root cause of the flakiness is a hidden dependency on time.time(): a test that sleeps 119 s is slow, and one that sleeps 1 s with a 1 s TTL races the scheduler. Make time a constructor argument:...


Submit Your Answer
Markdown supported