AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Topic

Math coding problems

Problems whose solution is a fact about numbers rather than a data structure: digit sums, divisibility, parity, arithmetic sequences, geometry on a grid, and the integer overflow and truncating division that trip a correct idea.

337 problems · 123 easy · 151 medium · 63 hard · judged by hidden tests in 13 languages

337problems

123 easy

151 medium

63 hard

Sign in to tick off what you solve.

Study plan: 14 problems · about 8 h 10 min

Start the study plan

Next up: Sum of All Odd Length Subarrays

How it works

Math, step by step

Palindrome number without a string, by peeling digits with % 10 and // 10

x = 1234321

xrev1234321x % 10x = 1234321rev = 0digit = x % 10, x = x // 10rev = rev × 10 + digit1234321 > 0: peel while x is the larger

Is 1234321 a palindrome? Rather than make a string, peel digits off the end: x % 10 is the last digit and x // 10 drops it. Each peeled digit is appended to rev as rev × 10 + digit, so rev grows into the right half read backwards.

0:01 / 0:30Step 1 of 7
Transcript · 7 steps

Study plan

Learn math in 8 days

14 of the 337 Math problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 323 in the full list below are practice at your own pace. Then move on to Two Pointers.

  1. Day 1

    about 1 h 15 min

    Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

  2. Day 2

    about 55 min

    Medium problems: the same pattern with one twist each. Name the twist before you code.

  3. Day 3

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  4. Day 4

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  5. Day 5

    about 1 h 10 min

    More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

  6. Day 6

    about 50 min

    Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

  7. Day 7

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

  8. Day 8

    about 50 min

    Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

    Next topic: Two Pointers

Math Coding Problems: 337 Questions with Solutions

337 math coding problems — 123 easy · 151 medium · 63 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 337
  • By difficulty: 123 easy · 151 medium · 63 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

Problems whose solution is a fact about numbers rather than a data structure: digit sums, divisibility, parity, arithmetic sequences, geometry on a grid, and the integer overflow and truncating division that trip a correct idea. The habit these build is to look for a closed form or an invariant before reaching for a loop.

How math works, step by step

xrev1231234x = 123rev = 1234palindrome(1234321) = true4 of 7 digits peeled
Palindrome number without a string, by peeling digits with % 10 and // 10. Example: x = 1234321
  1. Is 1234321 a palindrome? Rather than make a string, peel digits off the end: x % 10 is the last digit and x // 10 drops it. Each peeled digit is appended to rev as rev × 10 + digit, so rev grows into the right half read backwards.
  2. The last digit of 1234321 is 1234321 % 10 = 1; it moves onto rev, which becomes 0 × 10 + 1 = 1, and x becomes 1234321 // 10 = 123432. x is still the larger, so the two halves have not met yet.
  3. The last digit of 123432 is 123432 % 10 = 2; it moves onto rev, which becomes 1 × 10 + 2 = 12, and x becomes 123432 // 10 = 12343. x is still the larger, so the two halves have not met yet.
  4. The last digit of 12343 is 12343 % 10 = 3; it moves onto rev, which becomes 12 × 10 + 3 = 123, and x becomes 12343 // 10 = 1234. x is still the larger, so the two halves have not met yet.
  5. The last digit of 1234 is 1234 % 10 = 4; it moves onto rev, which becomes 123 × 10 + 4 = 1234, and x becomes 1234 // 10 = 123. Now x (123) is no larger than rev (1234): rev holds the right half, so the peeling stops.
  6. 7 digits is odd, so rev also took the middle digit 4; rev // 10 = 123 drops it. x = 123 and rev // 10 = 123 are equal, so the left half matches the right half read backwards.
  7. 1234321 is a palindrome. Only half its digits were peeled, so the test is O(log n) time and O(1) space, with no string and no full reverse to overflow; negative numbers and numbers ending in 0 are ruled out before the loop.

Math study plan

14 of the 337 Math problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 323 in the full list below are practice at your own pace. Then move on to Two Pointers.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Two Pointers

Math: the essentials

When to reach for it

Numbers too large to loop up to (n up to 10⁹ or beyond), questions about digits, remainders or parity, and an operation repeated so often that it must settle into a pattern. If the brute force is "simulate a billion steps", the intended answer is a formula, a cycle or an invariant.

The pattern

Work the small cases by hand and tabulate them; the rule often shows within the first half-dozen. Take digits off an integer with n % 10 and n // 10 rather than converting to a string, which keeps sign and overflow handling explicit. Check a result against the type's limit before the operation that could exceed it, not after.

def reverse(x):                     # 32-bit signed result, or 0
    LIMIT = 2**31 - 1
    sign, x, out = (-1 if x < 0 else 1), abs(x), 0
    while x:
        x, d = divmod(x, 10)
        if out > (LIMIT - d) // 10:  # out * 10 + d would overflow
            return 0
        out = out * 10 + d
    return sign * out

Cost

A digit loop is O(log n), trial division up to √n is O(√n), and a closed form is O(1). That gap is the point: for n = 10¹² a loop to n never finishes, while a loop to √n is a million steps.

Common mistakes

  • Overflow in a 32-bit int: a * b or a + b can pass 2³¹ − 1 long before the final answer does. Widen to 64 bits first.
  • Division and remainder with negatives: in Java and C++ -7 / 2 is -3 and -7 % 2 is -1 (JavaScript's % agrees); in Python -7 // 2 is -4 and -7 % 2 is 1.
  • Using floating point for an integer question: sqrt or pow can return 2.9999… for an exact square, so confirm with integer arithmetic.
  • Forgetting 0, 1 and negative inputs, which most formulas treat specially.

Start with

All math problems

Easy (123)

Medium (151)

Hard (63)

Companies that ask math problems

Next topic: Two Pointers