Math & Binary
Math
If code involves division or modulo, remember to check for division or modulo by 0 case. When a question involves "a multiple of a number", perhaps modulo might be useful.
Check for and handle overflow/underflow if you are using a typed language like Java and C++. At the very least, mention that overflow/underflow is possible and ask whether you need to handle it.
Consider negative numbers and floating point numbers. This may sound obvious, but under interview pressure, many obvious cases go unnoticed.
If the question asks to implement an operator such as power, square root or division and want it to be faster than O(n), binary search is usually the approach to go.
Some common formulas
-
Sum of 1 to N = (n+1) * n/2
-
Sum of GP = 20
+ 21
+ 22
+ 23
+ ... 2n
= 2n+1
- 1
-
Permutations of N = N! / (N-K)!
-
Combinations of N = N! / (K! * (N-K)!)
Practice Math Online
Typical Math Questions
-
Given a string such as "123" or "67", write a function to output the number represented by the string without using casting. (
Solution
)
-
Make a program that can print out the text form of numbers from 1 to 9999 (ex. 20 is "twenty", 105 is "one hundred and five", 2655 is "two thousand six hundred fifty five"). (
Solution
)
-
How would you convert Roman numerals into decimals? E.g.
XIV
becomes
14
. (
Solution
)
-
Compute the square root of an Integer without using any existing built in math functions. (
Solution
)
Binary
Questions involving binary representations and bitwise operations are asked sometimes and you must be absolutely familiar with how to convert a number from decimal form into binary form (and vice versa) in your chosen programming language.
Some helpful utility snippets:
-
Test kth
bit is set: num & (1 << k) != 0
.
-
Set kth
bit: num |= (1 << k)
.
-
Turn off kth
bit: num &= ~(1 << k)
.
-
Toggle the kth
bit: num ^= (1 << k)
.
-
To check if a number is a power of 2, num & num - 1 == 0
.
Corner Cases
Practice Binary Online
Read Further Binary Study Online
Typical Binary Interview Questions