← Questions
Find when failures started in a log
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked Jan 2026. It tests binary-search and logs.
A service writes a time-ordered log of request results. Up to some moment every request succeeded; from then on every request failed. Find the first failure. Solve it twice: when the log can be read only once, as a stream; and when it is stored and you can jump to any entry.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
first_failure_stream(entries: Iterable[tuple[int, bool]]) -> int | None— entries are(timestamp, ok)in order; returns the timestamp of the first failure, orNone. -
first_failure_stored(log) -> int | None—loghaslen(log)andlog.read(i) -> (timestamp, ok); each read is expensive.
Example
log = [(100, True), (105, True), (112, True), (120, False), (121, False), (130, False)]
first_failure_stream(log) -> 120
first_failure_stored(log) -> 120 # binary search reads about 3 entries, not 6
all successes -> None
all failures -> 100
Constraints
- Up to 10^9 entries in the stored log.
- Once failures start they don't stop (the base problem).
Clarifying questions
- Is it guaranteed that failures, once started, never stop?
- What should come back when nothing failed? When everything failed?
- In the stream case, must I return as soon as I see it, or at the end?
- Is the stored log one file, or many files by hour?
What a strong answer covers
- Stream: one pass, returning on the first failure; O(k) for the position of that failure, O(1) memory. Binary search is impossible without random access.
- Stored: binary search for the first
ok == False, O(log n) reads, with careful bounds (lo,hi, the empty log). - Why the monotone property is what makes binary search valid, and what you'd check if it weren't.
- Tests: empty log, failure at the first and last entry, no failure.
Follow-ups
- Failures are noisy: a few successes still happen after the outage starts. Find when the failure rate first stayed above 50% for a minute.
- The log is unbounded and you don't know its length: exponential search.
- Many services, each with its own log: find which one failed first.
Where it's asked
- Amazon · Technical phone screen · Last asked Jan 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
binary-search · logs
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
