JAVA | both operations in  O(1) | 99.81% | Minimal space
\nclass ProductOfNumbers {\n    private List<Integer> prods;\n    \n    private int indexOfZero;\n    private int size;\n    \n    public ProductOfNumbers() {\n
```\nclass ProductOfNumbers {\n    private List<Integer> prods;\n    \n    private int indexOfZero;\n    private int size;\n    \n    public ProductOfNumbers() {\n        indexOfZero = -1;\n        size = 0;\n        prods = new ArrayList<>();\n    }\n    \n    public void add(int num) {  \n        if(num == 0) {\n            indexOfZero = size;\n            prods = new ArrayList<>();\n        } else {\n            int currentSize = prods.size();\n            int prevProd = currentSize == 0 ? 1 : prods.get(currentSize - 1);\n            prods.add(num * prevProd);\n        }\n        \n        size++;               \n    }\n    \n    public int getProduct(int k) {\n        if(size - k <= indexOfZero) {\n            return 0;\n        }\n        \n        int currentSize = prods.size();\n        \n        int numerator = prods.get(currentSize - 1);\n        int denominator = currentSize - k <= 0 ? 1: prods.get(currentSize - k - 1);\n        \n        return  numerator / denominator;\n    }\n}\n\n```
[]