Overview
Curated: · Written: · Reviewed:
Math & Geometry
In one line
Reach for this when the answer turns on an exact number-theory, combinatorial or coordinate fact, and a wrong method still returns a plausible number. Keep the arithmetic in exact integers first, then apply the specific identity (Euclid, Fermat, the cross-product sign) only where the problem needs it.
Key definitions
- Intermediate overflow: a sub-expression exceeds the type's range even though the final value fits.
- Truncated vs floored modulo: C, C++, Java and Go truncate toward zero, so -7 % 3 is -1; Python floors, so it is 2.
- Modular inverse: the x with a·x ≡ 1 (mod m); it exists exactly when gcd(a, m) = 1.
- Orientation: the sign of the 2D cross product of (B - A) and (C - A): positive is a left turn, negative a right turn, zero collinear.
- Catastrophic cancellation: subtracting two nearly equal floats leaves mostly rounding error.
- Pigeonhole principle: n items in k slots guarantees some slot holds at least ceil(n / k) items, so with n > k at least one slot holds two or more.
Formulas and complexity
How it works
- Reduce modulo m after every multiply and add, and reorder divisions only where they stay exact.
- Geometry becomes sign tests on integer cross products, so there is no tolerance to tune.
- Two segments intersect when each straddles the other's line. If all four orientations are zero, the segments are collinear and overlap only if their bounding boxes overlap on both axes.
- Point-in-polygon counts ray crossings with a half-open rule, so a vertex on the ray is counted once.
When to use and when not to
| Decision | Prefer | Avoid when |
|---|---|---|
| Integral coordinates | Integer cross product, squared distances | Input is real-valued and needs a tolerance policy |
| Many primality queries under one bound | One sieve, then one array read | Queries are sparse or the bound is very large |
| Sieve memory at 10^8 | Odd-only bit array, about 6.25 MB | A byte-per-entry array, about 100 MB |
| One large primality test | Miller-Rabin | A deterministic answer is required without a proven bound |
| Modular division | Modular inverse, after checking coprimality | Modulus composite and operand not coprime |
| Money | Integer minor units or Decimal | Binary float |
Traps interviewers probe
- Overflow in the midpoint. With a = b = 1,500,000,000 in 32-bit int, a + b wraps to -1,294,967,296 and (a + b) / 2 returns -647,483,648. Fix: a + (b - a) / 2.
- Reducing only at the end. Mod distributes over + and ×, but a 64-bit accumulator wraps at 2^64, which is not a multiple of the modulus, so the final reduction is wrong. Fix: reduce on every step.
- Raw modulo as an index. In Go or C, -1 % 16 is -1 and buf[-1] is out of range. Fix: ((x % n) + n) % n.
- Dividing, then reducing. (6 // 4) % 7 is 1, not the answer. Fix: multiply by the modular inverse, so 6 × pow(4, -1, 7) % 7 is 5.
- Float equality on sums. Ten additions of 0.1 do not equal 1.0 in binary float. Fix: compare with a tolerance, or use Decimal or integer minor units for money.
Interview tips
- Open by naming the silent failure you guard against: overflow, the sign convention, or rounding. Then state the predicate.
- Expect these follow-ups: How do you test negative and zero inputs? Is the modulus prime, and is the operand coprime? Why squared distance? How are collinear boundary points handled in the hull, and is the rule the same in both passes?
- Expect a question on why a sieve beats trial division for many queries, and where Miller-Rabin replaces both.
- 30-second outline: state the predicate (cross-product sign, or Euclid in O(log n)); say integers keep it exact; give the complexity; name the edge case you handle (collinear, zero, negative); say which test you would write first.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.