Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite recursion in combinationSum #308

Open
dubzzz opened this issue Feb 15, 2019 · 0 comments · May be fixed by #458
Open

Infinite recursion in combinationSum #308

dubzzz opened this issue Feb 15, 2019 · 0 comments · May be fixed by #458

Comments

@dubzzz
Copy link
Contributor

dubzzz commented Feb 15, 2019

Details:

combinationSum fails with Maximum call stack size exceeded.
It seems that there is an infinite recursion in it.

Step to reproduce: combinationSum([0], 1)

Bug 1: presence of 0 produces an infinite loop
Bug 2: presence of small entries compared to target produces a stack size exceeded - combinationSum([1], 100000)

Fix

I can issue a PR with the fix.

How did I find it?

Thanks to property based testing framework fast-check.
The property was the following:

import fc from 'fast-check';

fc.assert(
    fc.property(
        fc.set(fc.nat()),
        fc.nat(),
        (elements, target) => {
            const combinations = combinationSum(elements, target);
            const combinationsStr = combinations.map(arr => arr.join(','));
            // no duplicates
            expect([...new Set(combinationsStr)]).toEqual(combinationsStr);
            // right sum
            for (const comb of combinations)
                expect(comb.reduce((a,b) => a+b, 0)).toBe(target);
        }
    )
)

Or:

for any elements - array of unique positive integers, target - positive integer
the output of combinationSum(elements, target) does not contain any duplicates
and all its entries sum to target

cascandaliato added a commit to cascandaliato/javascript-algorithms that referenced this issue Jan 30, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant