Skip to content
Tech Interview Prep
Technical interview guide

Math & Geometry

Problems that lean on a specific mathematical insight (number theory, combinatorics, or coordinate geometry) rather than a general algorithmic pattern.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v8
Editorial status
Reviewed

Scope: Language-neutral, with integer semantics cited from CPython 3.14.

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

gcd⁡(a,b) by Euclid:O(log⁡min⁡(a,b))\gcd(a,b)\ \text{by Euclid}: O(\log \min(a,b)) ae mod m:O(log⁡e) multiplications by squaringa^e \bmod m: O(\log e)\ \text{multiplications by squaring} Trial division primality:O(n) divisor tests\text{Trial division primality}: O(\sqrt{n})\ \text{divisor tests} Sieve of Eratosthenes:O(nlog⁡log⁡n)\text{Sieve of Eratosthenes}: O(n \log \log n) cross(A,B,C)=(Bx−Ax)(Cy−Ay)−(By−Ay)(Cx−Ax)\text{cross}(A,B,C) = (B_x - A_x)(C_y - A_y) - (B_y - A_y)(C_x - A_x) ∣cross∣≤8M2 for coordinates in [−M,M]|\text{cross}| \le 8M^2 \ \text{for coordinates in } [-M, M] Closest pair:O(nlog⁡n)\text{Closest pair}: O(n \log n)

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.
yes no yes yes no no yes no Compute four cross signs d1 d2 d3 d4 d1 and d2 opposite, d3 and d4 opposite? Segments intersect All four d are zero? Bounding boxes overlap on x and y? No intersection Zero endpoint inside other segment's bounding box?

When to use and when not to

DecisionPreferAvoid when
Integral coordinatesInteger cross product, squared distancesInput is real-valued and needs a tolerance policy
Many primality queries under one boundOne sieve, then one array readQueries are sparse or the bound is very large
Sieve memory at 10^8Odd-only bit array, about 6.25 MBA byte-per-entry array, about 100 MB
One large primality testMiller-RabinA deterministic answer is required without a proven bound
Modular divisionModular inverse, after checking coprimalityModulus composite and operand not coprime
MoneyInteger minor units or DecimalBinary float

Traps interviewers probe

  1. 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.
  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.
  3. 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.
  4. 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.
  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.