Top 3 Modular arithmetic Interview Questions
Browse 3 real-world technical and behavioral interview questions about Modular arithmetic. Review scenarios, edge cases, and architectural best practices.
You need to find a substring in linear time and the interviewer will not accept a library call. What do you compute, and what goes wrong on a collision?
Rabin-Karp computes a polynomial hash of the pattern and a rolling hash of each text window, updated in constant time per step modulo a large prime. Equal hashes only nominate a candidate, so you compare characters before reporting it. Skip that check and a collision is a wrong answer; keep it and collisions cost time.
Every number in the array appears three times except one. Find the single number in O(1) extra space, and make sure it works for negatives.
Count, for each of the 32 bit positions, how many numbers have that bit set, and keep the count modulo 3. Tripled values contribute multiples of 3, so the bits with a remainder belong to the single number. Setting bit 31 makes a Java int negative, so negative answers come out right in O(32n) time and constant space.
Rotate an array k positions to the right, in place.
Rotate array by k in place by normalising k modulo n, reversing the whole array, then reversing the first k and remaining n-k elements. It is O(n) time, O(1) extra space, and avoids the cyclic-replacement pitfalls unless write count matters. Use this ARRAYS answer to show the decision, trade-off, and evidence rather than a memorised definition.