PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Compute product of array except self

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of array manipulation, modular arithmetic, and algorithmic optimization by requiring computation of per-element products excluding the current element without division under a large modulus.

  • hard
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Compute product of array except self

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

## Problem Given an integer array `nums` of length `n`, return an array `ans` of length `n` where: - `ans[i] = (nums[0] * nums[1] * ... * nums[i-1] * nums[i+1] * ... * nums[n-1]) mod M` - Use `M = 1_000_000_007`. ### Requirements - Do **not** use division. - Your solution must run in **O(n)** time per test case. ### Input - An integer `n` (`1 <= n <= 2e5`) - An array `nums` of `n` integers (`0 <= nums[i] <= 1e9`) ### Output - An array `ans` of `n` integers. ### Notes - Handle zeros correctly (e.g., if there are multiple zeros, all outputs are zero). ### Example `nums = [1, 2, 3, 4]` → `ans = [24, 12, 8, 6]` (then each taken mod `M`).

Quick Answer: This question evaluates understanding of array manipulation, modular arithmetic, and algorithmic optimization by requiring computation of per-element products excluding the current element without division under a large modulus.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Feb 11, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
2
0
Loading...

Problem

Given an integer array nums of length n, return an array ans of length n where:

  • ans[i] = (nums[0] * nums[1] * ... * nums[i-1] * nums[i+1] * ... * nums[n-1]) mod M
  • Use M = 1_000_000_007 .

Requirements

  • Do not use division.
  • Your solution must run in O(n) time per test case.

Input

  • An integer n ( 1 <= n <= 2e5 )
  • An array nums of n integers ( 0 <= nums[i] <= 1e9 )

Output

  • An array ans of n integers.

Notes

  • Handle zeros correctly (e.g., if there are multiple zeros, all outputs are zero).

Example

nums = [1, 2, 3, 4] → ans = [24, 12, 8, 6] (then each taken mod M).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
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.