trekhleb / trekhleb/javascript-algorithms

Cartesian product with an empty set should be an empty set

Open
#339 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
197k
Forks
31k
PR merge metrics
No merged PRs in 30d

Description

https://github.com/trekhleb/javascript-algorithms/blob/master/src/algorithms/sets/cartesian-product/cartesianProduct.js#L8

I think it makes more sense to return an empty array

// invalid input return null or throw an error
if (!setA || !setB) {
    return null;
}
if (!setA.length || !setB.length) {
    return []; 
}
// or do nothing since the for loop will not trigger anyway

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/algorithms/sets/cartesian-product/cartesianProduct.js at the linked line and inspect how empty inputs are handled. Confirm the expected Cartesian-product behavior for an empty set, then add or update coverage for empty inputs so the function returns an empty array.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.