During debugging in One Drop services, a useful first step is finding where an observed execution trace stops matching the expected control flow. Given two execution logs as arrays of strings, return the earliest event index where they diverge after normalizing nested function scopes.
Each log entry is one of:
"CALL:name" — enter a function scope"RETURN:name" — exit a function scope"SET:key=value" — record a state update in the current scopeTwo logs are considered equivalent up to an index if, after processing entries in order, they produce the same stack of active function calls and the same latest values for keys visible in the current scope chain. Return the smallest index i where the states differ immediately after processing expected[i] and observed[i]. If the logs have different lengths but match through the shorter one, return the shorter length. If they never diverge, return -1.
Implement find_first_divergence(expected, observed) where:
expected: list of stringsobserved: list of stringsExample 1
expected = ["CALL:A", "SET:x=1", "RETURN:A"], observed = ["CALL:A", "SET:x=2", "RETURN:A"]1x has different values.Example 2
expected = ["CALL:A", "SET:x=1"], observed = ["CALL:A", "SET:x=1", "RETURN:A"]20 <= len(expected), len(observed) <= 10^4100expected = ["CALL:A", "SET:x=1", "RETURN:A"], observed = ["CALL:A", "SET:x=2", "RETURN:A"]Output1WhyBoth logs enter `A`, but after index 1 the visible value of `x` differs.expected = ["CALL:A", "SET:x=1"], observed = ["CALL:A", "SET:x=1", "RETURN:A"]Output2WhyThe logs match through index 1. The first difference is that the observed log has an extra event.expected = ["CALL:A", "CALL:B", "RETURN:B", "RETURN:A"], observed = ["CALL:A", "CALL:B", "RETURN:A", "RETURN:B"]Output2WhyAt index 2, the return order changes the active call stack, so the traces diverge there.0 <= len(expected), len(observed) <= 10^4Each log entry has length between 1 and 100Function names and keys are non-empty alphanumeric stringsEach individual log is well-formed, except mismatched returns may still appear and should be treated as divergencedef find_first_divergence(expected, observed):