No results found for the specified position. 2 JavaScript Advanced Level Bit Manipulation Interview Questions

MockQuestions

JavaScript Advanced Level Bit Manipulation Mock Interview

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

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

Question 1 of 2

Find two singlet numbers.

This interview question concentrates on using JavaScript maps and manipulation.

You are given an array of 32-bit integers. Every number in the array occurs twice except two distinct numbers which occur exactly one time. For example, in an array [1, 2, 5, 1, 3, 5, 2, 4], 1, 2 and 5 occurs twice but two numbers 3 and 4 occur exactly once in the array. You have to implement a function that finds and returns the numbers in the given array which occurs one time.

NOTE- You need to return the required numbers in an array of length 2. The numbers can be in any order.

/*Example*/

Given- [1, 2, 5, 1, 3, 5, 2, 4]
Expected output- [3, 4] or [4, 3]

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

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, 5, 1, 3, 5, 2, 4], the resultant map will be-
{
1 -> 2 // 1 occurs two times
2 -> 2 // 2 occurs two times
5 -> 2 // 5 occurs two times
3 -> 1 // 3 occurs one time
4 -> 1 // 4 occurs one time
}

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

    const result = []
 
    for (const key of occurrences.keys()) {
        if (occurrences.get(key) === 1) {
            result.push(key)
        }
    }

    return result
}

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

Approach II (using bit-manipulation):

Note- all bit positions are 0-indexed from least significant to most significant. Thus the least significant bit is 0 and the most significant bit is 31 (in 32-bit integer).

There are two observations are two be made while xor-ing numbers.
First— the bitwise xor of a number with itself is 0.
More formally,
a ^ a = 0, for every integer a
Second— if some bit position is 1 in xor of two numbers, then that bit position must be 0 in one number and 1 in the other number
More formally,
xxx1xxx
xxx0xxx
xxx1xxx

Now, let the array be the given array.
Let firstNumber and secondNumber are two required numbers.
We will xor all the numbers in the array. The result after XORing all numbers will be xor of firstNumber and secondNumber. It is because the other numbers will vanish by XORing them with themselves as they occur twice. But firstNumber and secondNumber will not vanish as they occur once.
Let xorOfNumbers = firstNumber ^ secondNumber such that xorOfNumbers = xor( of all numbers in array)
Next, we will find the least-significant set bit of xorOfNumbers. This can easily be done using—
leastSignificantBitNumber = xorOfNumbers & (-xorOfNumbers)
Let the ith bit of xorOfNumbers be the least-significant set bit. That is, all the bits from 0 to i - 1 will be 0 in xorOfNumbers.
Let the ith bit of firstNumber is 1. Then, from the second observation, the ith bit in secondNumber will be 0.
Now, we can make two exclusive partitions of the array with one having numbers with the ith bit equal to 1 and the other partition having numbers with the ith bit equal to 0. We will take xor of both the partitions separately. The resultant xor will be our required numbers. Here’s why:
Let firstPartition represent the numbers with the ith bit equal to 1 and the secondPartition represent the numbers with the ith bit equal to 0.
firstNumber belongs to firstPartition
secondNumber belongs to secondPartition
If a repeating number belongs to one partition, then its duplicate also belongs to the same partition.
firstPartition = a, a, b, b, …., firstNumber
secondPartition = p, p, q, q, …., secondNumber
Thus, taking xor of numbers firstPartition will vanish other numbers except for the firstNumber and taking xor of numbers secondPartition will vanish other numbers except for the secondNumber.

/**
 * @param {number[]} array
 * @return {[number, number]}
 */
 function findSingletNumbers(array) {
    let xorOfNumbers = 0

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

    const leastSignificantBitNumber = xorOfNumbers & (-xorOfNumbers)

    let firstNumber = 0
    let secondNumber = 0

    for (const num of array) {
        if (num & leastSignificantBitNumber) {
            // num belongs to firstPartition
            firstNumber = firstNumber ^ num
        } else {
            // num belongs to secondPartition
            secondNumber = secondNumber ^ num
        }
    }

    return [firstNumber, secondNumber]
}

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

Written by on May 22nd, 2021

Next Question

2 JavaScript Advanced Level Bit Manipulation Interview Questions & Answers

Below is a list of our JavaScript Advanced 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 two singlet numbers.

  • 2. Find a singlet number.