Fix a Flaky Test for Upload Retry With Exponential Backoff
Last updated: November 29, 2026
Quick Overview
A helper retries a failing chunk upload with exponential backoff, and its unit test passes locally but fails randomly in CI. Explain the likely causes of flakiness, then rewrite the helper and the test so the test is fast and deterministic.
TikTok
November 29, 20260
0
0 solved
A helper retries a failing chunk upload with exponential backoff, and its unit test passes locally but fails randomly in CI. Explain the likely causes of flakiness, then rewrite the helper and the test so the test is fast and deterministic.
TikTok's mobile and backend teams retry uploads constantly on unreliable networks, and flaky tests around timing are a common real-world pain. This screen checks whether you can spot hidden nondeterminism (real sleeps, wall clock, random jitter) and fix it with dependency injection.
What the Interviewer Expects
- Names the usual sources: real time.sleep, wall-clock assertions, unseeded random jitter, shared global state
- Injects sleep and random functions so the test controls them
- Asserts on the exact sequence of delays instead of elapsed time
- Caps the backoff and the number of attempts
- Keeps the production default behavior unchanged
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
- How would you test the same logic in an async (asyncio) uploader?
- How do you find which tests are flaky in a large CI suite?
- When is it right to retry a failed test automatically in CI, and when is it hiding a bug?
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
upload_with_retry(send, max_attempts, base, cap)callssend()until it succeeds or attempts run out.- Delay before attempt k (k from 1) is
min(cap, base * 2^(k-1))plus jitter. - The test mus...
High-Level Design
The original test probably did something like: make send fail twice, call the helper, and assert elapsed < 1.5 seconds. That depends on CI machine load, real sleeps, and random jitter, so it flake...