Find the first error in a log, then trace failures through dependencies
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Snowflake's two coding screens round, last asked Sep 2025. It tests binary-search, bfs and graphs.
A service writes a chronological log, and every line starts with [Info], [Warn] or [Error]. Once the service fails, it never recovers: from the first [Error] on, every line is [Error], and the line just before the first [Error] is always a [Warn]. Then the question moves from one service to many services that call each other.
Part 1: first error
-
first_error(logs: list[str]) -> intreturns the 0-based index of the first[Error]line, or-1if there is none, usingO(log n)line checks.
first_error(["[Info] boot", "[Info] ok", "[Warn] slow", "[Error] down", "[Error] down"]) -> 3
first_error(["[Info] a", "[Warn] b"]) -> -1
Part 2: who else fails
You also get the call graph as edges (u, v), meaning service u calls (depends on) service v, and the service s that failed first. When a service fails, every service that calls it fails too, and so on upstream.
-
failing_services(edges: list[tuple[str, str]], s: str) -> list[str]returns every service that ends up failing, includings, sorted by name.
Part 3: longest failure chain
Assume the call graph is a DAG.
-
longest_chain(edges, s) -> list[str]returns one longest chain of failures starting atsand walking upstream (each next service calls the previous one). Among equally long chains, return the lexicographically smallest list.
edges = [("web","auth"), ("web","search"), ("search","db"), ("auth","db"),
("admin","auth"), ("billing","ledger")]
failing_services(edges, "db") -> ["admin", "auth", "db", "search", "web"]
longest_chain(edges, "db") -> ["db", "auth", "admin"] # ties with db-auth-web and db-search-web
Constraints
- Up to 200,000 log lines; at least the first line is not an error when an error exists.
- Up to 100,000 services and 200,000 edges; names are unique strings.
Clarifying questions
- Can the log have no error at all, or be all errors?
- Does a failure propagate to callers (upstream) or to callees?
- Can the call graph have cycles in Part 2?
- Do you also need the time at which each service fails (its BFS depth)?
What a strong answer covers
- Part 1: binary search on the monotone predicate "line starts with
[Error]", the exact loop invariant, and why the[Warn]guarantee is not needed for correctness but bounds the answer at index 1 or later. - Part 2: build the reverse graph, BFS or DFS from
swith a visited set so cycles terminate;O(V + E). - Part 3: longest path from
sin the reversed DAG with memoized DFS, a deterministic tie rule, and why longest simple path is hard (NP-hard) once cycles are allowed. - Tests: no error, error at index 1, isolated
s, cycles in Part 2, ties in Part 3.
Follow-ups
- Report, for each failing service, how many hops after
sit fails. - Several services fail first at the same time.
- A service only fails when all (or a quorum) of its dependencies fail.
Where it's asked
- Snowflake · Two coding screens · Last asked Sep 202560 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.
What it tests
binary-search · bfs · graphs · 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.
