Make Delivery Slot Cutoff Logic Testable With an Injected Clock

Last updated: November 29, 2026

Quick Overview

A function that lists bookable delivery slots calls datetime.now() internally and its tests fail intermittently in CI. Refactor it so time is injected, then write deterministic tests covering the cutoff boundary, full slots, timezones and midnight.

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

0

0

0 solved


A function that lists bookable delivery slots calls datetime.now() internally and its tests fail intermittently in CI. Refactor it so time is injected, then write deterministic tests covering the cutoff boundary, full slots, timezones and midnight.

Flipkart grocery and large-appliance orders let customers pick a delivery slot, and a cutoff (for example two hours before slot start) decides what is still bookable. In a screening round the interviewer uses this to check whether you spot hidden time dependencies, think about IST versus UTC servers, and write tests that pin exact boundaries instead of sleeping or mocking globally.

What the Interviewer Expects
  • Explain why calling datetime.now() inside the function makes tests flaky
  • Pass now as a parameter or inject a clock
  • Use timezone-aware datetimes and reject naive ones
  • Write boundary tests at exactly the cutoff and one minute after
  • Cover the midnight and UTC versus IST cases
Key Topics to Cover
dependency injection
deterministic tests
boundary testing
timezones
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 same logic if it lived inside a Spring service that reads the system clock?
  • What other hidden inputs commonly make tests flaky?
  • Would you use property-based testing here, and what property would you check?
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 slot is bookable when its start is at least the cutoff after now and it has capacity left. Results are returned in start order. Servers run in UTC while slots are defined in IST, so comparisons must...

High-Level Design

The original code computed datetime.now() inside the loop. Tests written at 7:59 passed and at 8:01 failed, and CI in UTC saw a different date near midnight. The fix is to make time an input: the pr...


Submit Your Answer
Markdown supported