FastPrepCount Lucky Numbers in a Range
Problems/Count Lucky Numbers in a Range

Count Lucky Numbers in a Range

Target logoTarget● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Target online assessment. The source defines lucky numbers and a range limit of 10^18. It does not say whether to return the values or their count. Our version is about 90% the same as the reported question.

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 = 1r = 10return = 7

The lucky numbers are 1, 2, 3, 4, 6, 8, 9.

Example 2

l = 16r = 24return = 3

Throughout this interval floor(sqrt(x)) = 4. The divisible values are 16, 20, 24.

Example 3

l = 1000000000000000000r = 1000000000000000000return = 1

The endpoint is 10^18 = (10^9)^2, so it is divisible by its floored square root.

Constraints

  • 1 <= l <= r <= 10^18.

More Target Online Assessment questions

  • First-Available Seat BookingOA · Seen Aug 2026
  • Minimum Edit-Distance String for Each QueryOA · Seen Aug 2026

Similar questions at other companies

  • Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
  • Perfect Squares Matching a PatternNielsen · OA
  • Maximum Alloy Production Within BudgetMicrosoft · OAMicrosoft interview questions
  • Linear InterpolatorTwo Sigma · OATwo Sigma interview questions
  • Rock JumpingIMC Trading · OAIMC Trading interview questions
  • Cyber Beacon DetectionRippling · OARippling interview questions
  • Souvenir Shop PurchasesAmazon · OA
  • TikTok Video Relevance AdjustmentTikTok · OATikTok interview questions
  • Paint the CeilingSnowflake · OASnowflake interview questions
  • K-th Character in an Infinite StringArcesium · OAArcesium interview questions
See Target hiring insights
Saved
class Solution {
    public long countLuckyNumbers(long l, long r) {
      // Write your code here.
    }
}
Checking account…