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.
Curated: · Written: · Reviewed:
MCQ-24A ticketing service renders sequence numbers as digit strings by joining the output of this Python 3 helper with `''.join(map(str, digits(n)))`: ```python def digits(n): out = [] while n: out.append(n % 10) n //= 10 return out ``` A downstream system compares each rendered ID to `str(n)` and rejects any mismatch. For sequence number 4052 the ID is rejected. What string did the service emit?
Answer to see the discussion.
MCQ-20An observability service issues 5-character uppercase alert codes from A to Z. Characters may repeat, and different orderings are different codes. A code is classified urgent if it contains at least one A or at least one B (codes with both are included). How many urgent codes are possible?
Answer to see the discussion.
MCQ-21A C++ service answers 10^5 queries of the form C(n, k) mod 1_000_000_007, with 0 ≤ k ≤ n ≤ 10^6, using 64-bit integers. Preprocessing may take up to a second; every query after that must return in constant time. Which implementation satisfies this?
Answer to see the discussion.
MCQ-1A Java method averages two sensor readings with `int avg = (a + b) / 2;`. Both readings are 1,500,000,000, and Java's `int` is 32-bit two's-complement with defined wraparound. What does `avg` hold?
Answer to see the discussion.
MCQ-25A Java 17 service added this helper to halve the gap between two positions: ```java /** Precondition: a <= b, both non-negative. */ static long midpoint(long a, long b) { return (a + b) / 2; } ``` The team treats that docstring as a complete statement of the numeric assumptions. A reviewer then exercises the top of the range with `midpoint(9000000000000000000L, 9000000000000000000L)` and asserts the result is `9000000000000000000L`; the assertion fails. What does the method return for that call?
Answer to see the discussion.
MCQ-9A Python 3.12 service charges 0.1 per item and accumulates the total in a float: `total = 0.0`, then `total += 0.1` once per item for 10 items. The final `assert total == 1.0` fails in production. Which statement correctly describes `total` and why the assertion fires?
Answer to see the discussion.
MCQ-3A Go service keeps a 16-slot ring buffer. It computes an index as `idx := (writePos - readPos) % 16` and then reads `buf[idx]`. At one point writePos is 3 and readPos is 11. What happens when the code evaluates `buf[idx]`?
Answer to see the discussion.
MCQ-2A service computes a polynomial rolling hash over a 1 GB file with a 64-bit unsigned accumulator: for each byte, `h = (h * 131 + byte) % (2**61 - 1)`. A teammate deletes the per-byte `%` to speed up the loop, arguing that because mod distributes over + and ×, reducing once at the end gives the same hash. The loop now runs in plain 64-bit unsigned arithmetic. What is the result?
Answer to see the discussion.
MCQ-6While profiling a legacy batch job you find this helper, called once per record: ```python def gcd(a, b): while a != b: if a > b: a -= b else: b -= a return a ``` The profiler shows it dominating CPU time on records whose two fields are 1,071 and 462. For that input, how many times does the loop body execute before the function returns, and what does it return?
Answer to see the discussion.
MCQ-5A payments service computes g^x mod p. A reviewer rewrote the loop as: ```python def power(b, e, m): r = 1 while e: b = b * b % m if e & 1: r = r * b % m e >>= 1 return r ``` What does power(3, 5, 7) return?
Answer to see the discussion.
MCQ-4A rolling-hash routine computes `(h * inv(k)) % p` with h = 6, k = 4, p = 7, where `inv(k)` is the modular inverse of k modulo p. The routine must return the value r that satisfies `k * r ≡ h (mod p)`. What value does the correct expression produce?
Answer to see the discussion.
MCQ-7A hash-table library sizes tables by scanning upward from the requested capacity to the next prime; capacities reach 10^9 and the scan dominates startup time. The current check is: ```python def is_prime(n): if n < 2: return False i = 2 while i <= n // 2: if n % i == 0: return False i += 1 return True ``` A reviewer proposes changing the loop condition to `while i * i <= n`. For a prime just under 10^9, what does that change?
Answer to see the discussion.
MCQ-8A Go service precomputes primes up to N = 10^8 at startup so that every primality query is answered by one array read. The container has a 32 MB memory limit, and the HTTP server and query index already hold 24 MB, leaving 8 MB for the sieve. The current code runs `sieve := make([]bool, n+1)` and the container is OOM-killed during startup. Which change keeps the sieve inside the 8 MB budget and preserves the one-read query path?
Answer to see the discussion.
MCQ-22A dedup pipeline hashes each of 5,000,000 distinct record IDs into a bitmap with 4,194,304 (2^22) slots. An engineer argues that because the hash is uniform, the batch should fit with few collisions. What does the pigeonhole principle establish about this batch?
Answer to see the discussion.
MCQ-23A service walks a functional graph x_{n+1} = f(x_n) over 32-bit states, starting at x_0 = 7. It must stop at the first repeated value. The first repeat can be 10^8 steps away, and the process has only 64 KB free for bookkeeping. Which approach fits this constraint?
Answer to see the discussion.
MCQ-19A dispatcher selects the nearest driver to a pickup using integer-metre grid offsets. It compares dx*dx + dy*dy instead of math.hypot(dx, dy). The three closest candidates are A at (600, 800), B at (700, 700), and C at (200, 979). Which driver does the dispatcher select, and what is that driver's squared distance?
Answer to see the discussion.
MCQ-10When evaluating the turn orientation formed by an ordered triplet of points A, B, and C in the Cartesian plane, how does the 2D cross product of vectors (B - A) and (C - A) indicate whether the sequence makes a counter-clockwise (left) turn?
Answer to see the discussion.
MCQ-12When evaluating the 2D cross product of vectors (p2 - p1) x (p3 - p1) to determine the orientation of three points using integer coordinates, what is the required integer bit-width to prevent overflow given coordinates bounded within [-M, M]?
Answer to see the discussion.
MCQ-13A 2D collision routine tests segment AB from (0,0) to (4,4) against segment CD from (0,4) to (4,0). It computes cross(A,B,C), cross(A,B,D), cross(C,D,A), cross(C,D,B) in that order, where cross(U,V,W) = (V-U) x (W-U). Which result and conclusion is correct?
Answer to see the discussion.
MCQ-14A collision checker tests the wall segment W from (0,0) to (4,4) against each candidate segment using only the strict test d1*d2 < 0 and d3*d4 < 0, where d1 and d2 are the orientations of W's endpoints relative to the candidate and d3 and d4 are the orientations of the candidate's endpoints relative to W. For which candidate does this test report no intersection even though the two segments share at least one point?
Answer to see the discussion.
MCQ-15A GIS pipeline stores building footprints as integer vertex lists. Before triangulating, it must confirm each ring's orientation and area in standard Cartesian coordinates. For the ring [(0,0), (4,0), (4,3), (0,3)], what does the shoelace sum S = Σ(x_i·y_{i+1} − x_{i+1}·y_i) give, and what should the pipeline do?
Answer to see the discussion.
MCQ-16A point-in-polygon routine casts a +x ray from the query point and counts edge crossings. A query point strictly inside a simple polygon is reported outside; a trace shows the ray passes exactly through one polygon vertex, where both edges meeting there register a crossing, so the count comes back even. Which change makes the classification correct?
Answer to see the discussion.
MCQ-17A team computes the 2D footprint of a rectangular robot by sampling points along its chassis edges and running the routine below, where `cross(o,a,b)` is `(a.x-o.x)*(b.y-o.y) - (a.y-o.y)*(b.x-o.x)`. ```python def hull(pts): pts = sorted(set(pts)) if len(pts) <= 1: return pts low, high = [], [] for p in pts: while len(low) >= 2 and cross(low[-2], low[-1], p) < 0: low.pop() low.append(p) for p in reversed(pts): while len(high) >= 2 and cross(high[-2], high[-1], p) < 0: high.pop() high.append(p) return low[:-1] + high[:-1] ``` The sampled points are `[(0,0), (1,0), (2,0), (2,2), (0,2), (0,1)]`. What does `hull` return?
Answer to see the discussion.
MCQ-18A closest-pair routine splits its points at the median x-coordinate, recurses on both halves, and in the merge step takes the points with |x − x_mid| < d (d = the smaller of the two halves' closest-pair distances), sorts that strip by x, and compares each strip point with the next seven in that order. On a 10,000-point test set it returns a pair at distance 3.0; brute force finds a pair at distance 1.0 that straddles the dividing line. Which change makes the routine return 1.0?
Answer to see the discussion.
MCQ-11When calculating the area of a long, needle-thin triangle with side lengths a >= b >= c using standard IEEE 754 double-precision floating-point arithmetic, the traditional Heron's formula sqrt(s * (s - a) * (s - b) * (s - c)) frequently yields zero or severe relative error. Why does this catastrophic cancellation happen, and which algebraic restructuring eliminates it?
Answer to see the discussion.