Math for coding interviews/Modulo - the remainder operation
Modulo (%), the Remainder, Explained Simply
The % symbol means "what is left over after dividing?" 7 % 3 = 1 because 7 = 2×3 + 1. 10 % 4 = 2 because 10 = 2×4 + 2. The result is always between 0 and (divisor - 1).
See it for yourself
Imagine 7 items being sorted into 3 boxes. Fill each box one at a time: box 0 gets item 0, box 1 gets item 1, box 2 gets item 2, box 0 gets item 3... The last item lands in box (7 % 3) = box 1.
Modulo - the remainder operation
The % symbol means "what is left over after dividing?" 7 % 3 = 1 because 7 = 2×3 + 1. 10 % 4 = 2 because 10 = 2×4 + 2. The result is always between 0 and (divisor - 1).
Think of it this way: A clock with 12 positions. If it is 10 o'clock and 5 hours pass, the hand lands at (10 + 5) % 12 = 3. The clock "wraps around" just like modulo does.
For algorithms: Hash tables use modulo to turn any key (even a huge number from hashing a string) into a valid index. myKey.hashCode() % tableSize = which slot to use.
Real-world analogy
A clock with 12 positions. If it is 10 o'clock and 5 hours pass, the hand lands at (10 + 5) % 12 = 3. The clock "wraps around" just like modulo does.
Why it matters in interviews
Hash tables use modulo to turn any key (even a huge number from hashing a string) into a valid index. myKey.hashCode() % tableSize = which slot to use.