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^4100