Math & Bit Manipulation - DSA Interview Questions
Math and Bit Manipulation are fundamental techniques used to solve problems efficiently, especially in number theory, cryptography, and optimization. We can simplify computations, optimize algorithms, and reduce space complexity by leveraging mathematical properties and bitwise operations.
Mastering these concepts is essential for tackling efficient arithmetic operations, bitwise tricks, and modular arithmetic problems.
Practice Math & Bit Manipulation DSA Coding Problems with Solutions
Learning Objectives:
Understand mathematical techniques like GCD, modular arithmetic, prime numbers, and number theory in problem-solving. Learn how bitwise operations can optimize computations, detect patterns, and solve problems using O(1) space.
Exercise Instructions:
- Start with the first problem and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you with more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Count the Number of Set Bits in an Integer
Required Input:
n = 29Expected Output:
4
Code In Python
Run Code?
Click Run Button to view compiled output
2. Check if a Number is a Power of Two
Required Input:
n = 16Expected Output:
True
Code In Python
Run Code?
Click Run Button to view compiled output
3. Find the Single Non-Repeating Element in an Array
Required Input:
nums = [4, 1, 2, 1, 2]Expected Output:
4
Code In Python
Run Code?
Click Run Button to view compiled output
4. Swap Two Numbers Without Using a Temporary Variable
Required Input:
a = 5, b = 10Expected Output:
10 5
Code In Python
Run Code?
Click Run Button to view compiled output
5. Find the XOR of All Elements in an Array
Required Input:
nums = [3, 5, 7, 9]Expected Output:
8
Code In Python
Run Code?
Click Run Button to view compiled output
6. Check if a Number is Odd or Even Using Bit Manipulation
Required Input:
n = 42Expected Output:
Even
Code In Python
Run Code?
Click Run Button to view compiled output
7. Reverse the Bits of a Given 32-bit Integer
Required Input:
n = 5Expected Output:
2684354560
Code In Python
Run Code?
Click Run Button to view compiled output
8. Find the Missing Number in an Array of 1 to N
Required Input:
nums = [1, 2, 3, 5]Expected Output:
4
Code In Python
Run Code?
Click Run Button to view compiled output
9. Compute the Square of a Number Without Using * or /
Required Input:
n = 7Expected Output:
49
Code In Python
Run Code?
Click Run Button to view compiled output
10. Find the GCD (Greatest Common Divisor) of Two Numbers
Required Input:
a = 56, b = 98Expected Output:
14
Code In Python
Run Code?
Click Run Button to view compiled output


