Identify Hidden Conflict with Minimal Oracle Queries
Heaps, Sorting & SearchModel answerLast updated
Find one conflicting pair among n items using a black-box oracle that tests subsets, minimizing the number of expensive queries.
Problem
You have n items numbered 0 through n-1. Exactly one pair of items (i, j) with i < j conflicts with each other. You don't know which pair it is.
You can call a black-box function run(subset) that accepts a list of item numbers and returns:
Trueif both conflicting items are present in the subsetFalseotherwise
Your task is to identify the conflicting pair (i, j) while minimizing calls to run(), which is very expensive.
Function Signature
def find_conflicting_pair(n: int) -> Tuple[int, int]:
Constraints
2 <= n <= 10^5- The oracle is deterministic
- Target query complexity:
O(log² n)or better
Examples
Example 1:
n = 4, hidden conflict: (1, 2)
run([0, 1, 2])→True(both 1 and 2 present)run([0, 1])→False(missing 2)run([1, 3])→False(missing 2)
Example 2:
n = 2, only possible pair: (0, 1)
Notes
- Consider how the oracle's response changes as you vary the subset boundaries
- Think about isolating one index before searching for the other
Hints
Hint 1
The oracle's response has a monotonic property when you query ranges with a moving boundary
Hint 2
Try finding the larger index j first by testing increasingly larger prefixes [0, 1, ..., k]
Hint 3
Once you know j, you can find i by testing ranges that always include j but vary the starting point
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is recognizing monotonicity: when we query ordered ranges with one moving boundary, the oracle's response flips exactly once. This lets us use binary search twice—once for each element of the pair.
Step 1: Find j (the larger index)
Query prefixes of increasing length: [0, 1, 2, ..., k].
Since i < j, whenever we include j in our prefix, we automatically include i as well. Therefore:
run([0..k])returnsFalsewhenk < j(prefix too short, missing j)run([0..k])returnsTruewhenk >= j(both i and j included)
The response transitions from False to True exactly at k = j. Binary search over k ∈ [1, n-1] finds this transition point in O(log n) queries.
Step 2: Find i (the smaller index)
Now that we know j, query suffixes ending at j: [m, m+1, ..., j].
Since j is always included, the range contains both items exactly when m <= i:
run([m..j])returnsTruewhenm <= i(we captured i)run([m..j])returnsFalsewhenm > i(we skipped past i)
The response transitions from True to False as m increases past i. Binary search over m ∈ [0, j-1] finds i in another O(log n) queries.
Why This Is Optimal
There are C(n,2) = n(n-1)/2 possible pairs. Each boolean query provides at most 1 bit of information. The information-theoretic lower bound is:
log₂(n(n-1)/2) ≈ 2 log₂ n - 1 queries
Our algorithm uses ≈ 2 log₂ n queries, matching this bound up to a small constant. No algorithm can be asymptotically better.
Implementation Details
Binary search 1 (finding j):
- Initialize
lo = 1, hi = n - 1 - Use standard lower-bound search
- When
run([0..mid])isTrue, sethi = mid - Otherwise set
lo = mid + 1
Binary search 2 (finding i):
- Initialize
lo = 0, hi = j - 1 - Use upper-bound search (finding the last
True) - When
run([mid..j])isTrue, setlo = mid - Otherwise set
hi = mid - 1 - Critical: Use upper midpoint
(lo + hi + 1) // 2to avoid infinite loops when updatinglo = mid
Complexity
- Queries:
⌈log₂(n-1)⌉ + ⌈log₂ j⌉ ≤ 2⌈log₂ n⌉(about 34 queries for n = 10⁵) - Time per query:
O(n)to construct the subset list - Space:
O(n)for subset construction
Edge Cases
n = 2: Both searches start withlo == hi, returning(0, 1)with zero queries- Pair
(0, 1): First search findsj = 1; second search has empty range[0, 0] - Pair
(n-2, n-1): Both searches run through their full ranges
Code
from typing import Tuple, List
def find_conflicting_pair(n: int) -> Tuple[int, int]:
# Phase 1: Binary search for j using prefixes [0..k]
lo, hi = 1, n - 1
while lo < hi:
mid = (lo + hi) // 2
if run(list(range(mid + 1))): # [0, 1, ..., mid]
hi = mid
else:
lo = mid + 1
j = lo
# Phase 2: Binary search for i using suffixes [m..j]
lo, hi = 0, j - 1
while lo < hi:
mid = (lo + hi + 1) // 2 # Upper midpoint
if run(list(range(mid, j + 1))): # [mid, mid+1, ..., j]
lo = mid
else:
hi = mid - 1
i = lo
return (i, j)
What Interviewers Expect
-
Recognizing monotonicity: Explicitly state why prefix queries flip at exactly
jand why suffix queries flip at exactlyi -
Achieving O(log n): Strong candidates recognize that
O(log² n)is not tight and improve toO(log n)total queries -
Binary search correctness: Handle boundary conditions properly, especially choosing the correct midpoint formula for upper-bound search
-
Information-theoretic reasoning: Explain the lower bound and why this solution is essentially optimal
Common Mistakes
- Using
O(n)linear scan instead of recognizing the search structure - Querying random subsets hoping to narrow down candidates (exponential complexity)
- Incorrect midpoint in the second search causing infinite loops
- Not recognizing that finding one element first simplifies finding the second