an HCL GUVI product

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 = 29

Expected Output:

4

Code In Python

def count_set_bits(n): # Write your logic here pass # Prefilled input n = 29 print(count_set_bits(n))

Run Code?

Click Run Button to view compiled output

2. Check if a Number is a Power of Two

Required Input:

n = 16

Expected Output:

True

Code In Python

def is_power_of_two(n): # Write your logic here pass # Prefilled input n = 16 print(is_power_of_two(n))

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

def single_non_repeating(nums): # Write your logic here pass # Prefilled input nums = [4, 1, 2, 1, 2] print(single_non_repeating(nums))

Run Code?

Click Run Button to view compiled output

4. Swap Two Numbers Without Using a Temporary Variable

Required Input:

a = 5, b = 10

Expected Output:

10 5

Code In Python

def swap_numbers(a, b): # Write your logic here pass # Prefilled input a = 5 b = 10 a, b = swap_numbers(a, b) print(a, b)

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

def array_xor(nums): # Write your logic here pass # Prefilled input nums = [3, 5, 7, 9] print(array_xor(nums))

Run Code?

Click Run Button to view compiled output

6. Check if a Number is Odd or Even Using Bit Manipulation

Required Input:

n = 42

Expected Output:

Even

Code In Python

def is_odd_or_even(n): # Write your logic here pass # Prefilled input n = 42 print(is_odd_or_even(n))

Run Code?

Click Run Button to view compiled output

7. Reverse the Bits of a Given 32-bit Integer

Required Input:

n = 5

Expected Output:

2684354560

Code In Python

def reverse_bits(n): # Write your logic here pass # Prefilled input n = 5 print(reverse_bits(n))

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

def find_missing_number(nums): # Write your logic here pass # Prefilled input nums = [1, 2, 3, 5] print(find_missing_number(nums))

Run Code?

Click Run Button to view compiled output

9. Compute the Square of a Number Without Using * or /

Required Input:

n = 7

Expected Output:

49

Code In Python

def square(n): # Write your logic here pass # Prefilled input n = 7 print(square(n))

Run Code?

Click Run Button to view compiled output

10. Find the GCD (Greatest Common Divisor) of Two Numbers

Required Input:

a = 56, b = 98

Expected Output:

14

Code In Python

def gcd(a, b): # Write your logic here pass # Prefilled input a = 56 b = 98 print(gcd(a, b))

Run Code?

Click Run Button to view compiled output

1 of 3