Interactive Problems

Example Driver (of An Obvious Interactive Problem)


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>

int main() {
    FILE *pfin, *pfout;
    pfin = fdopen(3, "r");
    pfout = fdopen(4, "w");

    int n;
    scanf("%d", &n);

    bool ok = false;

    int l = 25;
    while(l--) {
        int x;
        int e = fscanf(pfin, "%d", &x);
        if(e != 1) {

        if(x < n) {
            fprintf(pfout, "Bigger\n");
        } else if(x > n) {
            fprintf(pfout, "Smaller\n");
        } else {
            fprintf(pfout, "Bingo!\n");
            ok = true;

    if(ok) {
    } else {

    return 0;

The first thing you need to do in a driver is open two special files: pfin and pfout. Pfin is connected to the output of the submitted program. And, pfout is connected to the input of the submitted program. So anything that the submitted program outputs (e.g. using printf) can be read from pfin. And, anything you write to pfout can be read by the submitted program (e.g. using scanf).

At the end, you need to print a single integer from the driver. If the integer is 0, it implies that the submitted program behaved in an expected way (i.e. the test case passed successfully). Otherwise, you can print a non-zero positive number to indicate that the submitted program failed to produce the correct behavior.

The driver program can read the test input through scanf. For this problem, each test input has a single integer.

# Contents of Test Input
1 54372
2 878223

The output part of a test is irrelevant in case of interactive problems. It is also not expected that interactive problems will have sample test cases. Rather, an example interaction should be included as a part of the output specification in the statement.