KatInterview

Identify Hidden Conflict with Minimal Oracle Queries

Heaps, Sorting & SearchModel answer
HardModel answerHeaps, Sorting & Searchbinary-searchoptimizationoracle

Last 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:

  • True if both conflicting items are present in the subset
  • False otherwise

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