0% found this document useful (0 votes)
4 views2 pages

Coding Math Shortcuts

The document outlines various coding interview shortcuts for math and bit manipulation, including techniques for digit manipulation, prime number checks, bit manipulation operations, GCD/LCM calculations, fast exponentiation, XOR tricks, divisibility rules, prefix sums, and sliding window methods. It emphasizes the importance of practicing these techniques regularly to prepare for coding interviews and assessments. Each section provides concise formulas and methods to efficiently solve common problems.

Uploaded by

kunalempire12345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Coding Math Shortcuts

The document outlines various coding interview shortcuts for math and bit manipulation, including techniques for digit manipulation, prime number checks, bit manipulation operations, GCD/LCM calculations, fast exponentiation, XOR tricks, divisibility rules, prefix sums, and sliding window methods. It emphasizes the importance of practicing these techniques regularly to prepare for coding interviews and assessments. Each section provides concise formulas and methods to efficiently solve common problems.

Uploaded by

kunalempire12345
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like