Not sure what to study next?

Get a personalised quant interview roadmap built around your upcoming interviews, strengths and weaknesses.

Takes a few minutes. Start with your target role and firms.

← Programming questions
Medium / PYTHON

Product Except Self Modulo M

arrays · prefix_suffix · modular_arithmetic 25 minute study target

Not completed

Challenge

For each position i, return the product of all nums[j] with j ≠ i, reduced modulo modulus. Return a list of residues from 0 to modulus-1. The modulus is not necessarily prime, and numbers can be zero or negative. Use prefix/suffix products rather than division.

Input and output contract

0 ≤ n ≤ 3,500; |nums[i]| ≤ 1,000,000; 2 ≤ modulus ≤ 1,000,000,007. The product over no elements is 1, so a singleton returns [1]; empty input returns [].

All tests follow this contract. You do not need to validate malformed input. Return the requested result from product_except_self; printed output is not your answer. Standard-library tools are allowed. Implement your solution in Python 3.12.

Target complexity: O(n) arithmetic operations; O(1) auxiliary space excluding output

Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.

Public examples

Example 1

Arguments

[[2, 3, 4], 12]

Expected return

[0, 8, 6]
Example 2

Arguments

[[0, -2, 3], 7]

Expected return

[1, 0, 0]
Example 3

Arguments

[[9], 8]

Expected return

[1]
Hint

The product excluding i is the product strictly to its left times the product strictly to its right.

The worked solution unlocks after completion, or with Premium access.

product_except_self.pyPython 3.12

Drafts are saved to your account, not to shared browser storage.

Sign in to submit code and save progress.

Results

Your results will appear here.

Hidden tests reveal only their pass/fail status, never their inputs or expected outputs. Runtime is observed wall time—not an inferred complexity class.

Join our Newsletter and Receive the Latest Updates!

Main Links

Trade Courses Assessments Interview Prep Contact Us

Social Media

Facebook Instagram YouTube

Legal

Terms and Conditions Privacy Policy Privacy settings

Talent Acquisition

Looking to hire exceptional quant talent?
See how we can help.

© 2026 EverythingQuant. All Rights Reserved.