LeetCode 1352 | Product of the Last K Numbers | Java | Medium | Stream Design | CodeByTushu
Description: ๐ Welcome Back to CodeByTushu!
In today's video, we solve LeetCode 1352 โ Product of the Last K Numbers using Prefix Products and Stream Design in Java.
We need to design a data structure supporting two continuous operations on an integer stream: appending numbers via add(num) and retrieving the product of the trailing k elements via getProduct(k). While a naive loop recomputes the product in linear time proportional to k, cumulative prefix multiplication enables constant time queries. The primary obstacle is handling zeros: multiplication by zero destroys invertible division, and subsequent products would remain zero indefinitely.
To overcome this, we reset our prefix product array whenever a zero is added, placing a neutral base value of 1. If a query requests k elements and k is greater than or equal to the size of the active prefix list, it indicates that a zero was encountered within those trailing k numbers, allowing us to immediately return 0. Otherwise, the product is evaluated in strict constant time O(1) by dividing the latest cumulative prefix product by the prefix product preceding the k-length window.
๐ฅ Problem Highlights
โ Cumulative Prefix Product: Enables O(1) constant-time range multiplication
โ Zero Reset Strategy: Resets prefix array upon encountering 0 to avoid zero-division errors
โ Range Invalidation: Returns 0 instantly when k is greater than or equal to current prefix size
โ Dynamic Stream Handling: Appends elements on the fly with amortized O(1) cost
โ Memory Optimized: Clears outdated prefix state upon zero insertions, conserving space
โ Optimal Complexity: O(1) time per add and getProduct operation, O(N) auxiliary space
โฑ๏ธ TIMESTAMPS
00:00 Intro
01:37 Problem Understanding
03:29 Observation
06:10 Approach
08:12 Code Explanation
10:35 Code Run
13:30 Time Complexity
14:37 Space Complexity
15:31 Outro
๐ Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers
โ Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/Solution.java
๐ Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/solution.py
โก C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/solution.cpp
๐ JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/solution.js
๐ LeetCode Problem:
https://codebytushu.com/leetcode/problem/product-of-the-last-k-numbers
๐ All LeetCode Solutions
๐ https://codebytushu.com/Leetcode/
๐ฑ Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
โค๏ธ Donate
๐ https://codebytushu.com/Leetcode/donate.php
๐ฅ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Prefix Product Technique
Data Stream Design
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
๐ Follow CodeByTushu
โถ๏ธ YouTube: https://www.youtube.com/@codebytushu
๐ธ Instagram: https://instagram.com/codebytushu/
๐ผ LinkedIn: https://www.linkedin.com/company/codebytushu/
๐ Website: https://codebytushu.com
๐ GitHub: https://github.com/CodeByTushu
โค๏ธ Don't Forget To
๐ Like | ๐ฌ Comment | ๐ Subscribe
๐ฌ Question of the Day
Why does adding a 0 allow us to safely reset the entire prefix product array?
1๏ธโฃ Because any subsequent getProduct(k) including that 0 will automatically evaluate to 0
2๏ธโฃ Because 0 acts as a neutral identity element in multiplication
3๏ธโฃ Because negative numbers cannot follow a zero in a stream
4๏ธโฃ Because clearing the array is required to prevent 64-bit integer overflow
#LeetCode1352 #LeetCode #ProductOfTheLastKNumbers #Java #JavaDSA #PrefixSum #Design #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
๐ Comment the answer below! ๐ฅย Close description
In today's video, we solve LeetCode 1352 โ Product of the Last K Numbers using Prefix Products and Stream Design in Java.
We need to design a data structure supporting two continuous operations on an integer stream: appending numbers via add(num) and retrieving the product of the trailing k elements via getProduct(k). While a naive loop recomputes the product in linear time proportional to k, cumulative prefix multiplication enables constant time queries. The primary obstacle is handling zeros: multiplication by zero destroys invertible division, and subsequent products would remain zero indefinitely.
To overcome this, we reset our prefix product array whenever a zero is added, placing a neutral base value of 1. If a query requests k elements and k is greater than or equal to the size of the active prefix list, it indicates that a zero was encountered within those trailing k numbers, allowing us to immediately return 0. Otherwise, the product is evaluated in strict constant time O(1) by dividing the latest cumulative prefix product by the prefix product preceding the k-length window.
๐ฅ Problem Highlights
โ Cumulative Prefix Product: Enables O(1) constant-time range multiplication
โ Zero Reset Strategy: Resets prefix array upon encountering 0 to avoid zero-division errors
โ Range Invalidation: Returns 0 instantly when k is greater than or equal to current prefix size
โ Dynamic Stream Handling: Appends elements on the fly with amortized O(1) cost
โ Memory Optimized: Clears outdated prefix state upon zero insertions, conserving space
โ Optimal Complexity: O(1) time per add and getProduct operation, O(N) auxiliary space
โฑ๏ธ TIMESTAMPS
00:00 Intro
01:37 Problem Understanding
03:29 Observation
06:10 Approach
08:12 Code Explanation
10:35 Code Run
13:30 Time Complexity
14:37 Space Complexity
15:31 Outro
๐ Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers
โ Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/Solution.java
๐ Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/solution.py
โก C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/solution.cpp
๐ JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/23-November/1352-Product-of-the-Last-K-Numbers/solution.js
๐ LeetCode Problem:
https://codebytushu.com/leetcode/problem/product-of-the-last-k-numbers
๐ All LeetCode Solutions
๐ https://codebytushu.com/Leetcode/
๐ฑ Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
โค๏ธ Donate
๐ https://codebytushu.com/Leetcode/donate.php
๐ฅ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Prefix Product Technique
Data Stream Design
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
๐ Follow CodeByTushu
โถ๏ธ YouTube: https://www.youtube.com/@codebytushu
๐ธ Instagram: https://instagram.com/codebytushu/
๐ผ LinkedIn: https://www.linkedin.com/company/codebytushu/
๐ Website: https://codebytushu.com
๐ GitHub: https://github.com/CodeByTushu
โค๏ธ Don't Forget To
๐ Like | ๐ฌ Comment | ๐ Subscribe
๐ฌ Question of the Day
Why does adding a 0 allow us to safely reset the entire prefix product array?
1๏ธโฃ Because any subsequent getProduct(k) including that 0 will automatically evaluate to 0
2๏ธโฃ Because 0 acts as a neutral identity element in multiplication
3๏ธโฃ Because negative numbers cannot follow a zero in a stream
4๏ธโฃ Because clearing the array is required to prevent 64-bit integer overflow
#LeetCode1352 #LeetCode #ProductOfTheLastKNumbers #Java #JavaDSA #PrefixSum #Design #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
๐ Comment the answer below! ๐ฅย Close description




