Lyft VO Interview Question: Mock Client

19 Views
No Comments

Implement a mock client for testing.

Do not rely on its behavior in your implementation.

class MockClient implements Client {
    // These numbers are for testing only and may be changed by the interviewer.
    // Do not use them in your solution.
    private static final int MAX_RESULTS = 103;
    private static final int PAGE_SIZE = 10;

    public Page fetchPage(int page) throws Exception {List<Integer> results = new ArrayList<Integer>();

        if (Math.random() < 0.3) {throw new Exception("Timeout");
        }

        if (page * PAGE_SIZE > MAX_RESULTS) {return new Page(results, -1);
        }

        results = TestHelpers.makeRange(page * PAGE_SIZE, Math.min(MAX_RESULTS, (page + 1) * PAGE_SIZE));
        return new Page(results, page + 1);
    }
}

This problem is a mock pagination client: the main task is to follow the paging contract, return the correct range of results for each page, and handle timeout and end-of-data conditions properly. The key implementation details are tracking the page number, respecting the fixed page size, and returning an empty result with a terminal next-page value when there are no more records. Since the constants in the sample client are only for testing, the solution should rely on the interface behavior rather than hard-coded values.

END
 0