MEDIUMasked at 3 companies

Ugly Number II

A medium-tier problem at 49% community acceptance, tagged with Hash Table, Math, Dynamic Programming. Reported in interviews at EPAM Systems and 2 others.

Founder's read

Ugly Number II is a medium-difficulty problem that shows up in assessments at Google, EPAM Systems, and Accenture. It asks you to generate the nth ugly number where ugly numbers are positive integers whose prime factors are only 2, 3, and 5. The 49.3% acceptance rate tells you most candidates either brute-force it wrong or miss the DP pattern entirely. The trick isn't math, it's the state machine: you're building ugly numbers in order by maintaining three pointers. Miss that and you'll timeout or blow memory. StealthCoder solves it in seconds if you blank on the approach during the OA.

Companies asking
3
Difficulty
MEDIUM
Acceptance
49%

Companies that ask "Ugly Number II"

If this hits your live OA

Ugly Number II 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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.

Get StealthCoder
What this means

The obvious move is to check every integer for ugliness, but that fails fast on larger n values. The real pattern uses dynamic programming with a smart pointer technique: maintain an array of ugly numbers and three indices tracking candidates from multiplying 2, 3, and 5. At each step, pick the smallest next candidate, add it to your array, and advance the pointer that generated it. You'll hear candidates debate heap vs. DP here, and both work, but the pointer approach is cleaner and faster. Common pitfalls include advancing the wrong pointer, not handling duplicates, or not initializing your base case. This problem tests whether you can recognize that generating candidates in sorted order beats checking each number individually. StealthCoder handles the pointer logic so if you hit this live and your mental model cracks, you get a working solution invisibly.

Pattern tags

The honest play

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

Ugly Number II recycles across companies for a reason. It's medium-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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Ugly Number II interview FAQ

Is Ugly Number II still asked at FAANG companies?+

Yes. Google includes it in their OA pool, and it appears in reports from Accenture and EPAM Systems. It's a classic dynamic programming pattern that screeners still use to filter. Not as common as two-pointer or tree problems, but common enough that you'll regret not drilling it.

What's the trick I'm missing if my brute-force times out?+

You're checking primality or factorization for every integer up to some threshold. Stop. Instead, build ugly numbers by only generating candidates that are 2, 3, or 5 times a previous ugly number. That eliminates checking non-ugly numbers entirely. The DP pointer approach does this in O(n) time.

How does this relate to the Dynamic Programming topic?+

It's a textbook DP problem where the recurrence is: next ugly = min(ugly[i]*2, ugly[j]*3, ugly[k]*5). You compute subproblems (smaller ugly numbers) once and reuse them to build larger ones. The state is just an index into your array, making it space and time optimal versus memoized recursion.

Should I use a heap or the pointer technique?+

Both work. Heap is O(n log n) because you extract and insert repeatedly. Pointers are O(n) and cleaner. If you're not sure which during the OA, the pointer technique is faster to code once you see the pattern. StealthCoder will surface whichever approach fits your thinking.

How do I avoid off-by-one errors with the three pointers?+

Track indices i, j, k separately. Generate the next candidate as min(ugly[i]*2, ugly[j]*3, ugly[k]*5). Add that to your array. Then increment all indices where the result matched (to avoid duplicates). Most bugs come from incrementing the wrong index or forgetting to check equality for multiple factors.

Want the actual problem statement? View "Ugly Number II" 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.