Largest Series Product

In the C track’s largest series product, the last unit test is:

static void test_rejects_negative_span(void)
{
        //TEST_IGNORE();
        TEST_ASSERT_EQUAL(-1, largest_series_product("12345", -1));
}

Because span is unsigned:

int64_t largest_series_product(char *digits, size_t span);

This test is actually rejecting “span longer than the digit string” …

1 Like

Looks like it rejects 2^{64}-1.

Edit: So. If subtracting the span from the length of the string yields a more negative result than -2^{63} then the result cannot be stored in a signed 64-bit integer, so you get an incorrectly positive value. Solely to account for this edge-case, an explicit comparison must be included. This is what is actually enforced by testing for “negative” spans.