Binary Search Strategy for Grader-Based Interactive Number Guessing

Interactive programming challenges require a different approach than standard I/O problems. Instead of reading from standard input and writing to standard output, the solution communicates directly with a judging system through predefined function calls. This architecture is commonly referred to as a grader-based interaction model.

In a typical number guessing scenario, a hidden integer $k$ exists within a known range $[1, n]$. The objective is to identify $k$ by submitting guesses to a query function. Each query returns a comparision indicator: a positive value if the guess exceeds the target, a negative value if the guess is smaller, and zero for an exact match. The solver must operate within a strict query limit $c$, which typically aligns with logarithmic complexity constraints.

To interface correctly with the judging grader, the query function must be declared with C linkage. This prevents C++ name mangling and ensures the linker can resolve the symbol provided by the external grading library.

extern "C" int query_target(int guess);

The core solving routine accepts the upper bound of the search space and the maximum allowed queries. A standard binary search efficiently narrows the candidate range. By maintaining lower and upper boundaries, the algorithm repeatedly probes the midpoint and adjusts the interval based on the grader's response.

extern "C" int find_hidden_value(int upper_bound, int query_limit) {
    int low = 1;
    int high = upper_bound;

    while (low <= high) {
        int pivot = low + (high - low) / 2;
        int feedback = query_target(pivot);

        if (feedback == 0) {
            return pivot;
        } else if (feedback > 0) {
            // The guess is larger than the hidden number
            high = pivot - 1;
        } else {
            // The guess is smaller than the hidden number
            low = pivot + 1;
        }
    }
    return low;
}

The extern "C" specifier on the solver function itself is often required by competitive programming platforms to expose the symbol to the grader's test harness. The binary search implementation uses low + (high - low) / 2 to prevent potential integer overflow during midpoint calcultaion, though it is less critical when $n \le 10^6$. The loop condition low <= high guarantees that every element in the range is evaluated if necessary, while the early return on feedback == 0 optimizes query usage.

Query limits in these problems are deliberately set to enforce $O(\log n)$ complexity. For a maximum range of $10^6$, approximately 20 queries are sufficient, as $\lceil \log_2(10^6) \rceil = 20$. The grader tracks the number of invocations to query_target. Exceeding the allocated budget results in a failed test case, regardless of whether the final returned value is correct.

When structuring interactive solutions, standard I/O operations should be avoided unless the problem specification explicitly allows them. The judging environment maintains the hidden state internal, and the solver implementation must strictly adhere to the provided interface. Proper boundary management and immediate termination upon receiving a zero feedback code ensure compliance with both correctness and query budget constraints.

Tags: Interactive Problems Binary Search C++ Competitive Programming Grader Interface

Posted on Wed, 13 May 2026 22:09:09 +0000 by indigo2k