Coding Interview Math & Bit Manipulation Shortcuts
Digit Manipulation
• digit = n % 10
• n /= 10
• Left-to-right extraction using divisor
• rev = rev * 10 + digit
Prime Number Tricks
• Check prime using i*i <= n
• Use Sieve of Eratosthenes for multiple primes
Bit Manipulation
• Check odd/even using n & 1
• Check power of 2 using n & (n-1)
• Remove last set bit using n = n & (n-1)
• Check kth bit using n & (1<<k)
• Toggle kth bit using n ^= (1<<k)
• Set kth bit using n |= (1<<k)
• Unset kth bit using n &= ~(1<<k)
GCD / LCM
• GCD(a,b) = GCD(b, a%b)
• LCM(a,b) = (a*b)/GCD(a,b)
Fast Exponentiation
• Binary exponentiation for O(log n) power calculation
XOR Tricks
• a^a=0
• Use XOR for Single Number
• Use XOR for Missing Number
Divisibility Tricks
• Divisible by 2 -> last digit even
• Divisible by 3 -> digit sum divisible by 3
• Divisible by 5 -> ends with 0 or 5
• Divisible by 9 -> digit sum divisible by 9
Prefix Sum
• prefix[i] = prefix[i-1] + arr[i]
• Range sum using prefix[r] - prefix[l-1]
Sliding Window
• windowSum += arr[r]
• windowSum -= arr[l]
Practice these shortcuts regularly and dry run them on paper. These techniques are commonly used in coding
interviews and online assessments.