Katkat

Compute Products Excluding Each Index

AsanaSoftware Engineer · Onsite

Last updated

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] 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.