Find the conflicting pair of tests with a black-box runner
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked Jan 2026. It tests binary-search, divide and conquer and debugging.
You have n tests, numbered 1..n. Each test passes when run alone, but exactly one pair of tests conflicts: any run that includes both of them fails. You can only learn about the tests through a black box, run(tests), which runs a set of tests together and reports whether the run passed. Each call is expensive. Find the conflicting pair with as few calls as possible.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
run(tests: set[int]) -> bool— provided;Truewhen the set passes,Falsewhen it contains both tests of the conflicting pair. -
find_conflict(n: int, run) -> tuple[int, int]— returns the pair(i, j)withi < j.
Example
n = 8, the hidden pair is (3, 6)
binary search on prefixes {1..m}: the smallest failing prefix is {1..6}, so j = 6
binary search on {m..5} plus {6}: the largest m that still fails is 3, so i = 3
find_conflict(8, run) -> (3, 6) # 5 calls in total, versus up to 28 pair tests
Constraints
-
2 <= n <= 10^5. - Aim for O(log n) calls; the time spent outside
runis not the bottleneck.
Clarifying questions
- Exactly one conflicting pair, or possibly several?
- Does the order of tests within a run matter?
- Is
rundeterministic, or can tests be flaky? - Is there a limit on how many tests one run may contain?
What a strong answer covers
- Brute force over pairs is O(n^2) calls.
- The prefix idea: a prefix
{1..m}fails exactly when it contains both tests, so the smallest failing prefix ends at the larger testj. Binary search: aboutlog2 ncalls. - The second search: with
jfixed, the set{m..j-1} ∪ {j}fails exactly whenm <= i; binary search again fori. Total about2 log2 ncalls. - An information-theory lower bound: there are
n(n-1)/2possible pairs, so at least about2 log2 n - 1calls are needed. - Tests with the pair at the ends (
(1, 2),(n-1, n)).
Follow-ups
- There may be several conflicting pairs: find all of them, and count calls in terms of
nand the number of pairs. - A conflict can involve three tests.
-
runis flaky and fails at random 1% of the time.
Where it's asked
- Google · Onsite coding · Last asked Jan 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
binary-search · divide and conquer · debugging
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.
