Count Lucky Numbers in a Range
Reported by candidates from Target's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Target's July 2026 OA includes a problem where x is lucky if it's divisible by floor(sqrt(x)), and you count lucky numbers in [l, r] with r up to 10^18. That bound kills any loop. Your example 3 is 10^18 as a single point, and it's a hint that the answer needs math, not iteration. The pattern is grouping numbers by their floored square root and counting multiples in closed form. If you blank on the setup during the live assessment, StealthCoder is the quiet safety net that reads the problem and hands you a working solution.
The problem
An integer x is lucky when it is divisible by floor(sqrt(x)). Given two integers l and r, return the number of lucky integers in the inclusive interval [l, r]. Implement countLuckyNumbers(l, r). Function countLuckyNumbers(l: long, r: long) → long Examples Example 1 l = 1 r = 10 return = 7 The lucky numbers are 1, 2, 3, 4, 6, 8, 9. Example 2 l = 16 r = 24 return = 3 Throughout this interval floor(sqrt(x)) = 4. The divisible values are 16, 20, 24. Example 3 l = 1000000000000000000 r = 1000000000000000000 return = 1 The endpoint is 10^18 = (10^9)^2, so it is divisible by its floored square root. Constraints 1 <= l <= r <= 10^18.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Write a count function f(n) for lucky numbers in [1, n], then answer f(r) - f(l-1). For k = floor(sqrt(x)), x ranges over [k^2, (k+1)^2 - 1], which is 2k+1 values. Multiples of k in that block are exactly k^2, k^2+k, k^2+2k, so every full block has 3 lucky numbers. Check example 2: 16, 20, 24 for k=4. For k=1, the block is 1..3 and all three are lucky, which matches. So let m = floor(sqrt(n)). Blocks 1 to m-1 are full and give 3*(m-1). The last block is partial: count which of m^2, m^2+m, m^2+2m are <= n. The pitfall is floating point sqrt at 10^18. Use integer sqrt and adjust up or down with a correction loop. Also watch overflow in m^2 + 2m. If you freeze mid-OA, StealthCoder is the hedge that gets you the closed form fast.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Count Lucky Numbers in a Range 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Target's OA.
Target reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Lucky Numbers in a Range FAQ
What's the trick in Count Lucky Numbers in a Range?+
Group numbers by k = floor(sqrt(x)). Each block [k^2, k^2+2k] holds exactly three multiples of k: k^2, k^2+k, k^2+2k. So full blocks contribute 3 each, and only the last block needs a partial check. No iteration over the range.
How do I handle the 10^18 constraint?+
Use 64-bit integers and an exact integer square root. Compute sqrt with floating point as a guess, then fix it by while loops so m*m <= n < (m+1)*(m+1). Floating point alone can be off by one near 10^18 and silently give wrong answers.
How do I handle the range [l, r] instead of [1, n]?+
Define f(n) as the count of lucky numbers from 1 to n, with f(0) = 0. Return f(r) - f(l-1). It's a prefix-count trick, and it keeps the partial-block logic in one place instead of two.
Does the brute force pass?+
No. With r up to 10^18, even a single linear scan is impossible. Brute force is only useful to verify your formula on small ranges, like example 1 where 1 to 10 gives 7. Test the closed form against it locally if you have time.
How should I prepare in 48 hours for this kind of OA question?+
Practice number theory counting problems where you bucket values by a floor function, then count per bucket. Write an integer sqrt helper and test edge cases: perfect squares, l = 1, and l = r = 10^18. That covers most of what this problem tests.