Google Onsite Interview Question | Need N Log N solution
Company: Google
Role: unknown
Interview Stage: Onsite
Location: unknown
Year: unknown
Original Post Content:
You have N unit tests that pass if run separately. But there are several pairs of tests that, if run in a single test set, fail the whole set (e.g., because they modify a global state in a non-hermetic db). Your task is to find any such pair.
You are given a function runTests(i1, i2, i3,.....) that takes a subset of tests, executes them in a single set and returns true if the whole set passes.
You can also assume that all minimal sets of tests that fail are made up of a pair of tests (i.e., if runTests(s) fails, with len(s) > 2, then there exists a strict subset t of s such that runTests(t) also fails).
My Python Code:def find_problematic_pair(tests, runTests): if len(tests) == 2: if not runTests(tests): return list(tests) else: return None mid = len(tests) // 2 left = tests[:mid] right = tests[mid:] if not runTests(left): return find_problematic_pair(left, runTests) if not runTests(right): return find_problematic_pair(right, runTests) for l in left: for r in right: if not runTests([l, r]): return [l, r] return None