Inject a Fake Clock to Test Story Expiry Deterministically

Last updated: November 29, 2026

Quick Overview

Instagram Stories disappear 24 hours after posting. Refactor a story store so its expiry logic can be unit tested without sleeping, and write tests that pin down the exact boundary behavior.

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

0

0

0 solved


Instagram Stories disappear 24 hours after posting. Refactor a story store so its expiry logic can be unit tested without sleeping, and write tests that pin down the exact boundary behavior.

Time-dependent logic is a common source of flaky tests in large codebases, and Meta engineers are expected to write code that is easy to test. This phone-screen style question checks whether you know dependency injection for time and whether your tests cover boundaries instead of just the happy path.

What the Interviewer Expects
  • Injects a clock dependency instead of calling time.time() inside business logic
  • Provides a fake clock with an explicit advance method
  • Tests the boundary on both sides (one second before and exactly at 24 hours)
  • Keeps each test independent with fresh setup
  • States the inclusive or exclusive expiry rule explicitly
Key Topics to Cover
dependency injection
unit testing
test doubles
boundary conditions
flaky tests
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 test the background job that deletes expired stories from storage?
  • What changes when viewers are in different time zones, and does it matter here?
  • How would you test behavior across a daylight saving change or a clock jump on the server?
  • When is mocking time.time globally acceptable, and why is injection usually better?
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
  • A story posted at time t is visible while now - t < 24h and hidden from exactly 24h on.
  • active_for(author) returns the author's visible stories.
  • Tests must run instantly and give the same re...
High-Level Design

The original code called time.time() inline, so the only way to test expiry was to sleep for a day or patch a global. Instead the store takes a clock object with a now() method. Production passe...


Submit Your Answer
Markdown supported