simple O(1) prefix sum solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
var ProductOfNumbers = function () {
    this.numbers = [];
    this.prefixSum = [];
    this.zeroInd = null;
};

ProductOfNumbers.prototype.add = function (num) {
    this.numbers.push(num);
    const len = this.numbers.length;
    if (len > 1) {
        if (this.prefixSum[len - 2] === 0) {
            this.zeroInd = len - 2;
            this.prefixSum[len - 1] = num;
        } else {
            this.prefixSum[len - 1] = num * this.prefixSum[len - 2];
        }
    } else {
        this.prefixSum[len - 1] = num;
    }
};

/**
 * @param {number}
 * @return {number}
 */
ProductOfNumbers.prototype.getProduct = function (k) {
    if (k === 1) return this.numbers[this.numbers.length - 1];

    const len = this.numbers.length;
    if (this.zeroInd !== null && len - k <= this.zeroInd) {
        return 0;
    }
    const divider = len - (k + 1);
    return this.zeroInd === divider || k === len
        ? this.prefixSum[len - 1]
        : this.prefixSum[len - 1] / this.prefixSum[len - (k + 1)];
};

/** 
 * Your ProductOfNumbers object will be instantiated and called as such:
 * var obj = new ProductOfNumbers()
 * obj.add(num)
 * var param_2 = obj.getProduct(k)
 */
```
['JavaScript']