15% off all plans & roadmaps Use code
See plansLeetCode 1352 · Solution & editorial
Design an algorithm that accepts a stream of integers and retrieves the product of the last k integers of the stream.
Product of the Last K Numbers is a Design Patterns problem, in the Design (General/Specific) subpattern. The pattern covers tries, LRU/LFU cache, and data structure design
Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
The reference solution runs in O(1) time and O(N) space.
Product of the Last K Numbers has been reported in interviews at ByteDance, Google, Uber.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Product of the Last K Numbers is a Design Patterns problem, in the Design (General/Specific) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.