PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates array manipulation skills, arithmetic reasoning with cumulative products, edge-case handling for zeros and negative numbers, and time/space complexity optimization for in-place or constant-extra-space algorithms.

  • medium
  • Asana
  • Coding & Algorithms
  • Software Engineer

Compute Products Excluding Each Index

Company: Asana

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given an integer array `nums` of length `n`. Return an array `answer` such that `answer[i]` equals the product of every element of `nums` except `nums[i]`. Requirements: - Do not use division. - Run in O(n) time. - Use O(1) extra space besides the returned array if possible. - Correctly handle zeros and negative numbers. Example: `nums = [1, 2, 3, 4]` returns `[24, 12, 8, 6]`. Follow-up: explain how your approach behaves when the input contains one zero or multiple zeros.

Quick Answer: This question evaluates array manipulation skills, arithmetic reasoning with cumulative products, edge-case handling for zeros and negative numbers, and time/space complexity optimization for in-place or constant-extra-space algorithms.

You are given an integer array `nums` of length `n`. Return an array `answer` such that `answer[i]` is equal to the product of every element in `nums` except `nums[i]`. Requirements: - Do not use division. - Your algorithm must run in O(n) time. - Use O(1) extra space besides the returned array if possible. - Correctly handle zeros and negative numbers. For example, given `nums = [1, 2, 3, 4]`, return `[24, 12, 8, 6]`. Follow-up: be ready to explain how your approach behaves when the input contains exactly one zero or multiple zeros.

Constraints

  • 0 <= n <= 100000
  • -30 <= nums[i] <= 30
  • Do not use division
  • The final answers fit in a signed 64-bit integer

Examples

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

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

Explanation: For each position, multiply all other values: 2*3*4=24, 1*3*4=12, 1*2*4=8, 1*2*3=6.

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

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

Explanation: Negative numbers must be handled correctly: [2*(-3)*4, (-1)*(-3)*4, (-1)*2*4, (-1)*2*(-3)].

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

Expected Output: [6, 0, 0, 0]

Explanation: With exactly one zero, only the index containing zero gets the product of the non-zero elements. Every other index includes that zero, so its product is 0.

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

Expected Output: [0, 0, 0]

Explanation: With more than one zero, every product excludes only one element but still includes at least one remaining zero.

Input: ([5],)

Expected Output: [1]

Explanation: There are no other elements, so the product of all elements except itself is the empty product, which is 1.

Input: ([],)

Expected Output: []

Explanation: An empty input has no indices, so the result is also an empty list.

Hints

  1. First store, for each index, the product of all numbers to its left.
  2. Then scan from right to left while keeping a running suffix product, and multiply it into the current answer.
Last updated: May 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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

  • Solve a Jigsaw Puzzle - Asana (medium)
  • Implement an ASCII Art Printer - Asana (medium)
  • Implement ASCII canvas and solve two data problems - Asana (medium)
  • Implement core puzzle/array/grid routines - Asana (medium)