Ugly Number III
A medium-tier problem at 30% community acceptance, tagged with Math, Binary Search, Combinatorics. Reported in interviews at American Express and 0 others.
Ugly Number III is a medium-difficulty problem that American Express has asked. With a 30% acceptance rate, this is a real filter in their assessment. Most candidates fail because they think they need to iterate and count, which times out on large inputs. The trick is that you're not finding ugly numbers at all, you're using math and binary search to answer a counting problem. If this lands on your OA and you blank on the combinatorial approach, StealthCoder solves it invisibly while you stay calm.
Companies that ask "Ugly Number III"
Ugly Number III 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 Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code.
Get StealthCoderThe naive approach of building a list of ugly numbers (multiples of three, five, or seven) fails on large n because iteration is too slow. The real solution combines Number Theory and Binary Search: you binary search on the answer, then use the inclusion-exclusion principle to count how many ugly numbers exist up to a candidate value. For each mid-point in the search, you calculate the count using LCM and GCD to avoid double-counting multiples. Most engineers miss this entirely on first sight. When you hit the wall during the assessment, StealthCoder reads the problem, recognizes the combinatorics pattern, and surfaces the binary search template with the correct counting formula in seconds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Ugly Number III 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. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Ugly Number III interview FAQ
Why does counting ugly numbers matter more than generating them?+
The input n can be huge (up to 10^9), so generating all ugly numbers is infeasible. Instead, you binary search on the answer and count how many ugly numbers exist below each candidate, then find the nth one. This runs in log^2 time instead of linear, which is the only way to pass.
Is this still asked at American Express?+
Yes, American Express is the sole company in the data set for this problem. It's a medium-difficulty filter designed to separate strong problem solvers from those who can only code straightforward loops. Expect it in their online assessment round.
What makes inclusion-exclusion the hard part?+
You must avoid double-counting multiples. For example, 15 is a multiple of both 3 and 5, so you add multiples of 3, add multiples of 5, subtract multiples of 15 (their LCM), then add back multiples of LCM(3,5,7), etc. Get the formula wrong and your count is off by thousands.
How does this relate to Number Theory topics?+
The problem is fundamentally about GCD, LCM, and the Chinese Remainder Theorem in disguise. You're computing the count of integers divisible by at least one of three numbers, which is a classic number-theoretic inclusion-exclusion problem, not a search or iteration problem.
Can I solve this without binary search?+
Theoretically, you could use math to solve directly for n, but binary search is the safe, intuitive approach most engineers land on after they realize iteration fails. It's also more forgiving if your counting logic has minor bugs. Stick with binary search plus counting for the OA.
Want the actual problem statement? View "Ugly Number III" on LeetCode →