Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Mirror Products

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Build every product without division: The Mirror Products.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Prefix and suffix products matches the clues
  2. 2Keep this true after every move: before each backward step, answer[i] holds the left product and suffix holds the right product
  3. 3Reach the result within O(n) time, O(1) extra space beyond output

Rules and pressure

  • Target cost: O(n) time, O(1) extra space beyond output
  • State rule: before each backward step, answer[i] holds the left product and suffix holds the right product
Lesson 1 of 3

Live algorithm trace

Prefix and suffix products

Complete execution
1 of 7

Initialize neutral output values and a prefix product of 1.

1answer = [1] * n
2prefix = 1
3store prefix, then multiply by nums[i]
4suffix = 1
5multiply answer[i] by suffix
6multiply suffix by nums[i]
7return answer
answer = [1, 1, 1, 1]prefix = 1
Truth to preserve / Cost target
Truth to preserve

before each backward step, answer[i] holds the left product and suffix holds the right product

Cost target

O(n) time, O(1) extra space beyond output

Each answer needs everything to its left and everything to its right. A forward pass stores left products, then a backward pass multiplies in a rolling right product without dividing.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead