Reported June 2024
Zscalermath

Sum of Divisors of the Array GCD

Reported by candidates from Zscaler's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Zscaler OA. Under 2s to a working solution.
Founder's read

The Zscaler OA reported in June 2024 looks like an array problem, but it's really two small math steps glued together. Fold the array into one GCD, then add up the divisors of that single number. If you've got an invite and 48 hours, this one is a quick win once you see it. The array can hold 200000 values, but after the fold you're only dealing with one number up to 10^9. If your brain freezes on the divisor part, StealthCoder is the safety net running invisibly during the live OA.

The problem

Given a nonempty array of positive integers values, compute the greatest common divisor of all values.
Return the sum of every positive divisor of that GCD.

Function
sumDivisorsOfArrayGcd(values: int[]) → long

Examples
Example 1
values = [6,12,18]
return = 12
The array GCD is 6. Its positive divisors are 1, 2, 3, 6, whose sum is 12.
Example 2
values = [7,14]
return = 8
The GCD is 7, so the divisor sum is 1 + 7 = 8.
Example 3
values = [8,16,32]
return = 15
The GCD is 8. Its divisors 1, 2, 4, 8 sum to 15.

Constraints
1 <= values.length <= 200000
1 <= values[i] <= 10^9
The answer fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that the array disappears after one pass. Run gcd across all values, which is O(n log max). Then you have a single g, at most 10^9. Don't loop from 1 to g, that's up to a billion iterations and it'll time out. Loop i from 1 while i*i <= g. When i divides g, add i, and add g/i only if it differs from i so perfect squares don't double count. That's about 31623 steps. Use a 64-bit type for the sum and for i*i, since i*i can overflow a 32-bit int in some languages. Early exit is possible if the running gcd hits 1, since the answer is then 1. Common pitfall: forgetting the square-root pairing or the perfect square case. If you blank mid-assessment, StealthCoder can hand you the solution from the screen without the proctor seeing it.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Sum of Divisors of the Array GCD 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Zscaler's OA.

Zscaler reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sum of Divisors of the Array GCD FAQ

What's the trick in Sum of Divisors of the Array GCD?+

Reduce the whole array to one GCD first. After that it's a single number up to 10^9. Then sum its divisors by looping i up to the square root and adding both i and g/i when i divides g. Skip the second add when they're equal.

How hard is this Zscaler OA question really?+

Easy to medium. Each step is standard: fold gcd across the array, then enumerate divisors in square-root time. The only real risk is a brute-force divisor loop to g, which is too slow for values near 10^9.

Why can't I just loop from 1 to the GCD?+

The GCD can be as large as 10^9, so that loop could run a billion times. Divisors come in pairs (i, g/i), so checking i up to sqrt(g) finds them all in roughly 31623 iterations.

What edge cases should I test?+

Test a single-element array, where the GCD is the value itself. Test a GCD of 1, which gives a sum of 1. Test a perfect square GCD like 36 so you don't double count 6. Also test large values near 10^9 for overflow.

How do I prepare for this in 48 hours?+

Write a gcd function by hand, fold it over an array, and write the square-root divisor sum. Run it on the three examples: 12, 8 and 15. Then check the long-typed sum and the perfect square case. That's about an hour of work.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Zscaler.

OA at Zscaler?
Invisible during screen share
Get it