FastPrepMagic Number Range Counter
Problems/Magic Number Range Counter

Magic Number Range Counter

Graviton logoGraviton● HardFULLTIMEOA

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported from a Graviton online assessment for a full-time quantitative researcher role that the candidate had applied to online. The assessment was half coding and half probability questions; this is one of the coding tasks. A positive integer counts as magic when every one of the digits 2, 3, 5, 7 appears in it; solve(L, R) returns the count of magic values x with L <= x <= R, with L and R between 1 and 10^18 and L no larger than R. The report gives the worked range used as Example 1. The candidate did not pass.

The report batches as many as 20,000 queries; here each test is one call to solve. Examples 2 and 3 are ours.

Our version is about 95% the same as the reported question.

A positive integer is magic if its decimal digits include each of 2, 3, 5 and 7 at least once. Other digits may appear too, and digits may repeat. For example, 22357 and 73052 are magic, but 2350 is not, because it has no 7.

Given integers L and R, return the number of magic integers x with L <= x <= R. The answer can be larger than a 32-bit integer can hold, so return it as a 64-bit integer.

Function

solve(L: long, R: long) → long

Examples

Example 1

L = 10077R = 24391return = 98

Every number in this range has five digits and starts with 1 or 2.

  • Starting with 1: the other four digits must be exactly 2, 3, 5 and 7 in some order. That gives 4! = 24 numbers, from 12357 to 17532, all inside the range.
  • Starting with 2: the last four digits must contain 3, 5 and 7, and the number must be at most 24391. Look at the second digit:
    • 0, 1 or 2: the last three digits are 3, 5 and 7 in some order, so 3 × 6 = 18 numbers.
    • 3: the last three digits must contain 5 and 7. If the third of them is one of the 8 digits other than 5 and 7, the three different digits can be arranged in 6 orders: 8 × 6 = 48 numbers. If it repeats 5 or 7, 557 and 577 each have 3 orders: 6 numbers. That is 54 numbers, all below 24000.
    • 4: the last three digits are 3, 5 and 7 in some order, and only 24357 and 24375 are at most 24391: 2 numbers.
    • 5 or more: the number is larger than 24391.

In total 24 + 18 + 54 + 2 = 98.

Example 2

L = 22350R = 22380return = 2

Every number in this range starts with 223, so it already has a 2 and a 3. It is magic exactly when its last two digits are 5 and 7, in either order. That gives 22357 and 22375, and both are in the range, so the answer is 2. The digit 2 may appear more than once.

Example 3

L = 1R = 1000000000000000000return = 501295755433922040

10^18 itself has only the digits 1 and 0, so count the magic numbers below it. Write each of them with exactly 18 digits by adding leading zeros: the added zeros are never 2, 3, 5 or 7, so a number is magic exactly when its 18-digit string contains all four of these digits.

There are 10^18 strings. By inclusion-exclusion, remove those missing one chosen digit (4 × 9^18), add back those missing two (6 × 8^18), remove those missing three (4 × 7^18) and add back those missing all four (6^18). The result, 501295755433922040, needs a 64-bit integer.

Constraints

  • 1 <= L <= R <= 10^18

More Graviton Online Assessment questions

  • API Shield with Two Rolling LimitsOA · Seen Dec 2026

Similar questions at other companies

  • Count Nondecreasing Digit-Sum ArraysMicrosoft · OAMicrosoft interview questions
  • Stable Workload Redistribution TreesTower Research Capital · OA
  • Count Valid A-B-C Sequences Under a Modulo-Four RuleInfosys · OAInfosys interview questions
  • Count Permutations Without Three Equal Parities in a RowGoogle · OAGoogle interview questions
  • Enough Muffins for the LineSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
  • Find Encrypted NumberAmazon · OA
  • Valid PasswordsMathWorks · OAMathWorks interview questions
  • Coloring HousesSnowflake · OASnowflake interview questions
  • Find Maximum Number of StringsMoveworks · OAMoveworks interview questions
See Graviton hiring insights
Saved
class Solution {
    public long solve(long L, long R) {
        // Write your code here.
    }
}
Checking account…