Reported April 2021
SambaNova Systemsmath

Ugly Number

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

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

SambaNova Systems reported this one in April 2021, and it's Ugly Number, a warm-up that still trips people. The input spans the full signed 32-bit range, so you can't loop through candidate factors or test every prime up to n. You also can't forget that zero and negatives exist. If you've got an invite and 48 hours, this is a five-minute problem once you see the shape. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net so you're not stuck on a trivial one.

The problem

Return whether n is a positive integer whose prime factors are limited to 2, 3, and 5. The number 1 is ugly because it has no prime factors.

Function
isUgly(n: int) → boolean

Examples
Example 1
n = 6
return = true
6 equals 2 times 3.
Example 2
n = 1
return = true
One has no prime factors.
Example 3
n = 14
return = false
The factor 7 is forbidden.

Constraints
-2^31 <= n <= 2^31 - 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is repeated division. Reject n <= 0 first, since the constraints allow negatives and zero, and those are never ugly. Then, while n % 2 == 0, divide by 2. Do the same for 3 and 5. If what's left is 1, return true. Otherwise some other prime factor survived, so return false. That runs in O(log n) because each division shrinks n fast. No sieve, no factorization, no recursion needed. The common pitfall is skipping the n <= 0 check, which makes n = 0 loop forever since 0 % 2 == 0 and 0 / 2 is still 0. Another slip is returning false for 1. Your loops never fire for 1, and it ends at 1, so it passes naturally. If the SambaNova Systems assessment makes you freeze on the edge cases, StealthCoder is the hedge that hands you the clean loop in real time.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Ugly Number 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as ugly number. If you have time before the OA, drill that.

⏵ The honest play

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

SambaNova Systems reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Ugly Number FAQ

How hard is Ugly Number really?+

Easy. It's a few lines of code. The only way to lose it is edge cases: zero, negatives, and the number 1. Handle n <= 0 up front and the rest is a simple loop of divisions.

What's the trick to solve it fast?+

Strip out every factor of 2, then 3, then 5 using while loops with modulo and division. If the remainder is 1, the number only had those prime factors. If not, another prime was hiding in there.

Why does the constraint range matter?+

The input goes from -2^31 to 2^31 - 1, so trial division by all primes is wasteful and negatives must be handled. Repeated division by 2, 3, and 5 takes only about 30 or so steps at most.

What edge cases should I test before submitting?+

Test n = 1 (true), n = 0 (false), a negative like -6 (false), 14 (false because of 7), and a large power of 2 like 2^30 (true). Zero is the one that causes infinite loops if unguarded.

How do I prepare for this in 48 hours?+

Write the division loop from memory twice, then run it on the edge cases. Spend the rest of your time on harder variants like Ugly Number II, which uses a three-pointer or heap approach to generate the nth one.

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

OA at SambaNova Systems?
Invisible during screen share
Get it