EASYasked at 2 companies

Ugly Number

A easy-tier problem at 42% community acceptance, tagged with Math. Reported in interviews at J.P. Morgan and 1 others.

Founder's read

Ugly Number is a deceptively simple problem that trips up candidates who overthink it. You're given an integer and asked whether it's 'ugly' - meaning its prime factors are limited to 2, 3, and 5. J.P. Morgan and TCS ask this regularly, and at first glance it looks like a divisibility check you could solve in 30 seconds. The 42% acceptance rate reveals the catch: candidates mishandle edge cases, get stuck on the division logic, or miss the pattern entirely. If this lands in your assessment and you blank on the algorithm, StealthCoder surfaces a working solution in seconds while the proctor sees nothing.

Companies asking
2
Difficulty
EASY
Acceptance
42%

Companies that ask "Ugly Number"

If this hits your live OA

Ugly Number is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The trick is straightforward once you see it, but the obvious approach fails silently. You can't just check if the number is divisible by 2, 3, and 5 - you need to repeatedly divide by these primes until you can't anymore, then verify you're left with 1. If anything else remains, it has other prime factors. Most candidates either try to generate all ugly numbers up to n (overkill for this problem) or forget to handle the number 1 correctly - it's ugly by definition since it has no prime factors. Negative numbers and zero are never ugly. The edge cases and the division-loop pattern are where most mistakes live. This is pure Math topic work with no data structures involved.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Ugly Number recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Ugly Number interview FAQ

How hard is Ugly Number really compared to other Easy problems?+

The 42% acceptance rate puts it in the harder half of Easy problems. The algorithm itself is trivial - divide by 2, 3, 5 in a loop. What kills candidates is edge cases (1, negatives, zero) and the subtle mistake of not fully dividing out each prime. If you nail the cases and the loop, it's genuine Easy. Otherwise it tanks you.

Does J.P. Morgan still ask this in their OAs?+

Yes, J.P. Morgan and TCS both report this problem. It's not a trendy problem, so it's a safe bet for prep. The problem hasn't disappeared from their rotation and appears frequently enough that you should understand the pattern before your assessment.

What's the trick I'm missing if I get wrong answer on the test cases?+

Most wrong answers come from three mistakes: forgetting 1 is ugly, not fully dividing out each prime before moving to the next, or returning true when the number has other prime factors. Walk through manually: 6 divides by 2 to get 3, then 3. Result is 1, so true. If your loop exits early, you're not dividing completely.

Is this problem related to the Math topic, or does it need data structures?+

Pure Math. No arrays, heaps, or memoization needed for this version of the problem. It's a single-pass divisibility check with a loop. Some variants ask you to find the nth ugly number, which requires data structures - but the base problem is just arithmetic and control flow.

Should I memorize this or understand the pattern?+

Understand the pattern. The logic is simple: repeatedly divide by 2, 3, and 5. If what remains is 1, it's ugly. If anything else remains, it has other prime factors and isn't ugly. You'll nail it in the live assessment if you internalize that rule once before you sit down.

Want the actual problem statement? View "Ugly Number" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.