No results found for the specified position. 16 JavaScript Intermediate Level Bit Manipulation Interview Questions

MockQuestions

JavaScript Intermediate Level Bit Manipulation Mock Interview

To help you prepare for your JavaScript Intermediate Level Bit Manipulation interview, here are 16 interview questions and answer examples.

Get More Information About Our JavaScript Intermediate Level Bit Manipulation Interview Questions

Question 1 of 16

Find one singlet number.

This interview question concentrates on using JavaScript maps and operations.

You are given an array of 32-bit integers. Every number in the array occurs twice except one number. For example, in array [1, 2, 1, 3, 2], 1 and 2 occurs twice but 3 occurs only one time. You have to implement a function that finds and returns the single number in the given array.

/*Example*/

Given- [1, 2, 1, 3, 2]
Expected output- 3

Given- [100, 48, 73, 48, 73, 41, 100]
Expected output- 41

Solution:

Approach I (using maps):

We can iterate over all the numbers and find their number of occurrences. We will store all the counts on a map. The numbers will be the keys in the map and their number of occurrences will be the values. After iterating over the array, we will find elements in the map with an occurrence equal to 1.
For example, if the given array is [1, 2, 1, 3, 2], the resultant map will be-
{
1 -> 2 // 1 occurs two times
2 -> 2 // 2 occurs two times
3 -> 1 // 3 occurs one time
}

/**
 * @param {number[]} array
 * @return {number}
 */
function findSingletNumber(array) {
    const occurrences = new Map()
 
    for (const num of array) {
        const prevCount = occurrences.get(num) || 0
        occurrences.set(num, prevCount + 1)
    }

    for (const key of occurrences.keys()) {
        if (occurrences.get(key) === 1) {
            return key
        }
    }
}

Time complexity - O(n)
Space complexity - O(n)

Approach II (using bit operations):

Since, all the elements occur twice except, we can xor all the elements together. We know that the bitwise xor of a number with itself is 0.

More formally,
a ^ a = 0, for every integer a
It is evident by:
Lets say, a = 23 = (10111)binary
now xor-ing the a with itself,
10111
10111
00000
Thus, for example
1 ^ 2 ^ 1 ^ 3 ^ 2
= (1 ^ 1) ^ (2 ^ 2) ^ 3
= 0 ^ 0 ^ 3
= 3
(1 and 2 vanishes because they occur two times)

/**
 * @param {number[]} array
 * @return {number}
 */
function findSingletNumber(array) { 
    let result = 0

    for (const num of array) {
        result = result ^ num
    }

    return result
}

Time complexity - O(n)
Space complexity - O(1)
Along with decreasing space complexity, the solution is quite efficient, because it is using bitwise operations. Bitwise operations are faster than arithmetic operations like addition, multiplication etc.

Written by on May 22nd, 2021

Next Question

16 JavaScript Intermediate Level Bit Manipulation Interview Questions & Answers

Below is a list of our JavaScript Intermediate Level Bit Manipulation interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Find one singlet number.

  • 2. How many 1's can you find?

  • 3. Is the number a power of 2 or not?

  • 4. Generate an XOR array.

  • 5. What is the maximum XOR for each query?

  • 6. Find the subsets.

  • 7. What are the minimum number of operations to reduce the integer to 1?

  • 8. Check if a string contains all binary codes of size K.

  • 9. What is the total hamming distance?

  • 10. Reduce the binary string to 1.

  • 11. How do you compute the bitwise AND within the numbers range?

  • 12. What is maximum product of word lengths?

  • 13. Find the repeated DNA sequences.

  • 14. Does the data form a valid UTF-8 string?

  • 15. Find bitwise OR subarrays.

  • 16. What is the gray code sequence?