Product of the Last K Numbers
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Integers are appended to a stream one by one. At any point you may be asked for the product of the most recent k integers.
Implement:
- ProductOfNumbers() starts with an empty stream.
- add(num) appends num to the stream.
- getProduct(k) returns the product of the last k numbers in the stream.
getProduct(k) is only called when the stream holds at least k numbers, and its answer is guaranteed to fit in a signed 32-bit integer. Note that num can be 0. Both operations should be O(1) (amortized).
Examples
Constraints
- 0 <= num <= 100
- 1 <= k <= 4 * 10^4
- At most 4 * 10^4 calls in total.
- The stream holds at least `k` numbers when `getProduct(k)` is called.
- Every answer fits in a 32-bit signed integer.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.