Product of the Last K Numbers
Medium · Arrays & Strings · about 20 minutes
Asked at Google, Amazon, ByteDance, Microsoft.
Design a structure that receives integers one at a time and can report the product of the most recent ones.
ProductOfNumbers()starts with nothing stored.add(num)records a new value (0 ≤ num ≤ 100).getProduct(k)returns the product of thekvalues added most recently. At leastkvalues have been
added, and the product fits in a 32-bit integer.