PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to implement array transformations under strict time and space constraints, including handling edge cases such as zeros, negative values, overflow risks, and designing appropriate unit tests.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Compute product of array except self

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Take-home Project

Given an integer array nums, return an array out where out[i] equals the product of all elements in nums except nums[i]. Do it in O(n) time using constant extra space beyond the output array and without using division. Discuss how to handle zero(s), negative values, and potential overflow, and provide unit tests.

Quick Answer: This question evaluates a candidate's ability to implement array transformations under strict time and space constraints, including handling edge cases such as zeros, negative values, overflow risks, and designing appropriate unit tests.

Given an integer array `nums`, return an array `out` where `out[i]` equals the product of all elements of `nums` except `nums[i]`. Solve it in **O(n)** time using **constant extra space** beyond the output array, and **without using the division operator**. The classic trick is two passes: a left-to-right pass that fills `out[i]` with the product of everything to the left of `i`, then a right-to-left pass that multiplies in the product of everything to the right of `i`. This naturally handles zeros (a single zero makes every other entry zero except its own slot; two or more zeros make the whole result zero) and negative values (sign just follows from the multiplications). Division is avoided entirely, which sidesteps the divide-by-zero problem that a `total_product / nums[i]` approach would hit. **Example 1** Input: `nums = [1, 2, 3, 4]` Output: `[24, 12, 8, 6]` **Example 2** Input: `nums = [-1, 1, 0, -3, 3]` Output: `[0, 0, 9, 0, 0]`

Constraints

  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer
  • Must run in O(n) time
  • Must use only constant extra space beyond the output array
  • The division operator may not be used

Examples

Input: ([1, 2, 3, 4],)

Expected Output: [24, 12, 8, 6]

Explanation: out[0]=2*3*4=24, out[1]=1*3*4=12, out[2]=1*2*4=8, out[3]=1*2*3=6.

Input: ([-1, 1, 0, -3, 3],)

Expected Output: [0, 0, 9, 0, 0]

Explanation: A single zero at index 2: every other slot includes that zero in its product and becomes 0, while index 2 itself is the product of the non-zero elements (-1*1*-3*3 = 9).

Input: ([2, 3],)

Expected Output: [3, 2]

Explanation: Two-element minimum: out[0] is the other element 3, out[1] is the other element 2.

Input: ([5],)

Expected Output: [1]

Explanation: Single element: there are no other elements, so the empty product is 1.

Input: ([0, 0, 4],)

Expected Output: [0, 0, 0]

Explanation: Two or more zeros: every slot's product includes at least one zero, so the whole result is 0.

Input: ([-2, -3, -4],)

Expected Output: [12, 8, 6]

Explanation: All negatives: out[0]=(-3)*(-4)=12, out[1]=(-2)*(-4)=8, out[2]=(-2)*(-3)=6; signs follow from the multiplications.

Hints

  1. Avoid division: even if you could compute the total product, a single zero in the array makes total / nums[i] undefined for the zero's slot.
  2. out[i] = (product of everything left of i) * (product of everything right of i). Compute these with two passes.
  3. First pass left-to-right: store the running prefix product in out[i] before multiplying nums[i] in. Then a second pass right-to-left multiplies each out[i] by the running suffix product. Keep the prefix/suffix in single scalar variables so you use O(1) extra space.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)