Product of Array Except Self
Problem Statement
You are given an integer array nums.
Build an array answer where answer[i] is the product of every number in nums except nums[i].
Rules: you may not use division, and your solution must run in O(N) time. It is guaranteed that every prefix product and suffix product fits in a 32-bit integer.
Follow-up: can you do it with O(1) extra space? The output array does not count as extra space.
An integer array nums of length n.
An array of n integers: answer[i] = product of all nums[j] with j ≠ i.
answer[0] = 2·3·4 = 24, answer[1] = 1·3·4 = 12, answer[2] = 1·2·4 = 8, answer[3] = 1·2·3 = 6.
Every product that includes the 0 is 0. Only index 2 skips it: (−1)·1·(−3)·3 = 9.
- • 2 ≤ n ≤ 10^5
- • −30 ≤ nums[i] ≤ 30
- • Every prefix and suffix product fits in a 32-bit integer.
- • No division allowed.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each index, loop over the array and multiply all other numbers
Store the product before each index and after each index, then multiply
Fill answer with prefix products, then sweep from the right with one suffix variable
For each i, start with 1 and multiply every nums[j] with j ≠ i. Store the result.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.