🔴💎 Solution!
IntuitionWhen first approaching this problem, the key insight is that calculating products repeatedly for each getProduct call would be inefficient. Instead, we
# Intuition
When first approaching this problem, the key insight is that calculating products repeatedly for each getProduct call would be inefficient. Instead, we can maintain running products (prefix products) as we add numbers, which would allow us to calculate any range product quickly using division.

# Approach
1. **Prefix Products Array**:
   - Maintain an array that stores running products
   - Start with [1] to handle edge cases elegantly
   - Each element represents product of all numbers up to that position

2. **Handling Zeros**:
   - When a zero is encountered, reset the prefix products array to [1]
   - This effectively "forgets" all previous products
   - Makes zero handling simple and efficient

3. **Getting Products**:
   - For a range of k numbers, divide the latest prefix product by the prefix product k positions before
   - If k is larger than available numbers (after last zero), return 0
   - This gives us the product of last k numbers in O(1) time

# Complexity
- Time complexity: 
  - add(): $$O(1)$$ - just one multiplication and array append
  - getProduct(): $$O(1)$$ - just one division operation
  
- Space complexity: $$O(n)$$ 
  - where n is the number of elements added after the last zero
  - In worst case (no zeros), stores all numbers added

# Code
```ruby
class ProductOfNumbers
    def initialize()
        # Initialize an array to store prefix products
        # Start with 1 as the first element to handle edge cases
        @prefix_products = [1]
    end

    def add(num)
        if num == 0
            # If number is 0, reset prefix products
            # This handles the case of zeros in the stream
            @prefix_products = [1]
        else
            # Multiply the last prefix product with current number
            @prefix_products << @prefix_products.last * num
        end
    end

    def get_product(k)
        # If k is larger than our current prefix products (minus the initial 1)
        # it means we encountered a zero in the last k numbers
        if k > @prefix_products.size - 1
            return 0
        end
        
        # Get product of last k numbers by dividing current prefix product
        # by the prefix product k positions before
        @prefix_products.last / @prefix_products[-k-1]
    end
end
```
['Ruby']