Prime Factor Visitation
Reported by candidates from Maven Securities's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Maven Securities reportedly put this one in front of candidates in August 2026, and the brute-force instinct will burn you. You've got up to 10^5 bulbs and up to 10^5 numbers, and each number can trigger several prime flips. Simulating every flip directly is way too slow. The real task is spotting that flips on the same prime cancel in pairs, so only parity matters. It's a math problem wearing a simulation costume. If you blank on the parity trick, StealthCoder can run invisibly during the live OA and hand you the approach.
The problem
Alex has a row of light bulbs that are initially either on or off. The bulb at 1-based position i has state states[i - 1], where 0 means off and 1 means on.
Process the values in numbers from left to right. For each value:
Find its distinct prime factors.
For every such factor p, flip each bulb whose 1-based position is a multiple of p.
A factor is used only once for one value, even when it appears repeatedly in that value's prime factorization. The same factor may be processed again for another value in numbers, causing its bulbs to flip again.
Return the final state of every bulb after all values have been processed.
Function
lightBulbs(states: int[], numbers: int[]) → int[]
Examples
Example 1
states = [1,1,0,0,1,1,0,1,1,1]
numbers = [3,4,15]
return = [1,0,0,1,0,0,0,0,1,1]
The distinct prime factors are {3} for 3, {2} for 4, and {3, 5} for 15.
Factor 3 flips positions 3, 6, and 9.
Factor 2 flips positions 2, 4, 6, 8, and 10.
The final value 15 flips multiples of 3, then multiples of 5.
The final states are [1,0,0,1,0,0,0,0,1,1].
Constraints
1 <= states.length <= 10^5
1 <= numbers.length <= 10^5
Each states[i] is either 0 or 1.
1 <= numbers[i] <= 10^5Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: flipping all multiples of p twice is a no-op. So count how many times each prime p gets used across all numbers, and only keep primes with an odd count. Step one is a smallest-prime-factor sieve up to 10^5, so each number factors in O(log n). For each number, take its distinct primes and toggle a parity bit for each. Step two: for every prime with odd parity, walk its multiples up to states.length and flip them. That costs about n log log n total, since the sum of n/p over primes is tiny. The pitfall is flipping once per prime power, so 4 = 2*2 must flip factor 2 only once. Another trap is simulating each number in order. Order doesn't matter, because flips commute. Watch the 1-based positions against the 0-based array. If the sieve or the parity idea slips under pressure, StealthCoder is your hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Prime Factor Visitation cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Maven Securities's OA.
Maven Securities reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Prime Factor Visitation FAQ
What's the trick in Prime Factor Visitation?+
Flips commute and cancel in pairs. Count how often each distinct prime shows up across all numbers, keep only the primes with odd counts, then flip multiples of those primes once. That turns a huge simulation into a sieve plus a harmonic-style loop.
Why does brute force fail here?+
With 10^5 numbers and 10^5 bulbs, flipping every multiple for every prime of every number can hit billions of operations. Repeated primes redo identical work. Parity collapses all those repeats into at most one pass per prime.
How do I get distinct prime factors fast?+
Build a smallest-prime-factor sieve up to 10^5 once. Then for each number, repeatedly divide by its smallest prime factor, and record each prime only when it changes. That gives distinct primes in O(log n) per number.
What mistakes should I watch for?+
Counting a prime once per exponent, so 8 flips factor 2 three times, is the big one. Others are mixing up 1-based positions with the 0-based array, and flipping per number instead of aggregating parity. Test with example 1 by hand.
How do I prepare for this in 48 hours?+
Write a sieve with smallest prime factors from memory, then a parity-count array over primes, then the multiples loop. Run the sample from the problem. If you can do those three pieces cleanly, this question is easy points.