Math Concepts

⭐ Interview Importance: MEDIUM
⏱️ Revision Time: 1 min

Concept

In FAANG interviews, pure mathematical algorithms are less common than Graph or Tree problems, but when they do appear, they are often “pass/fail”. If you don’t know the specific mathematical formula or trick, it is nearly impossible to derive it from scratch under 45 minutes of pressure.

Math questions typically fall into three categories:

  1. Prime Numbers (Sieve of Eratosthenes)
  2. Number Theory (Greatest Common Divisor, Modulo Arithmetic)
  3. Geometry/Combinatorics (Overlapping Rectangles, Permutation math)

The Modulo Operator (%)

The Modulo operator is the absolute most frequently used mathematical tool in software engineering. It returns the Remainder of a division operation.

  • 10 % 3 = 1 (3 goes into 10 three times. The remainder is 1).
  • 10 % 2 = 0 (Perfectly divisible!).

Core Use Cases:

  1. Even/Odd Check: n % 2 === 0 (Even). n % 2 !== 0 (Odd).
  2. Circular Arrays / Wrapping: If you have an array of length 5, and your index pointer goes to 6, it will crash. index = (index + 1) % 5 mathematically forces the pointer to wrap perfectly back to 0!
  3. Extracting Digits: 123 % 10 instantly extracts the final digit 3. Math.floor(123 / 10) perfectly lops off the final digit, leaving 12. (This is how you reverse an integer mathematically without converting it to a string!).

Handling Overflow

In JavaScript, all numbers are 64-bit IEEE-754 Floats. The absolute maximum safe integer is 9,007,199,254,740,991 (Number.MAX_SAFE_INTEGER).

Many LeetCode math problems explicitly state: “Since the answer may be very large, return it modulo 10^9 + 7”.

Why 10^9 + 7?

  1. It is a massive Prime Number.
  2. It easily fits inside a standard 32-bit integer.
  3. Because it fits inside a 32-bit integer, you can safely add two of them together without triggering a 64-bit hardware integer overflow.

If a problem asks for modulo 10^9 + 7, you must physically apply % 1000000007 at every single step of your DP or Math loop, not just at the very end! If you wait until the end, the number will have already silently overflowed and corrupted in memory.

Interview Strategy

When faced with a Math problem, the Brute Force solution usually involves a for loop that takes O(N)O(N) time (e.g., looping 1 to N to check for primes).
The optimal solution almost always drops the time complexity to O(√N)O(√N) or O(log⁡N)O(\log N) by exploiting mathematical symmetry.

Always ask yourself: “Do I really need to check every number, or does the math mirror itself halfway?”

Interview Questions

Q: A developer tries to reverse an integer in JavaScript using const reversed = parseInt(n.toString().split('').reverse().join('')). Why is this bad?
A: Aside from the massive string allocation overhead, it fails mathematically on negative numbers! -123 becomes ["-", "1", "2", "3"]. Reversed, it is ["3", "2", "1", "-"]. Parsed into an Int, it returns NaN or 321. A pure mathematical approach using Modulo (n % 10) flawlessly handles the math without creating string garbage in memory.

Q: In JavaScript, what does -5 % 3 evaluate to?
A: It evaluates to -2. Unlike Python (which always returns a positive modulo), JavaScript’s % operator is technically a “Remainder” operator, not a true mathematical Modulo. It perfectly preserves the negative sign. If you need a strict positive wrap-around index in JS, you must use the formula: ((n % m) + m) % m.