No results found for the specified position. 13 Javascript Beginner Level Arrays Interview Questions

MockQuestions

Javascript Beginner Level Arrays Mock Interview

To help you prepare for your Javascript Beginner Level Arrays interview, here are 13 interview questions and answer examples.

Get More Information About Our Javascript Beginner Level Arrays Interview Questions

Question 1 of 13

Find integers smaller than the current number.

This question proves the developer's ability to work with array elements.

You are given an array of integers num. Your task is to find the count of integers that are less than num[i] for every integer num[i] in the array num. You need to return these counts in an array count, with count[i] representing the count of integers less than num[i].

/*Example*/

num = [5, 1, 2, 2, 4, 3, 10, 3]
expected result- [6, 0, 1, 1, 5, 3, 7, 3]

for num[0] = 5  //there are 6 integers smaller than 5 (1, 2, 2, 4, 3, 3)
for num[1] = 1  //there are 0 integers smaller than 1
for num[2] = 2  //there is 1 integer smaller than 2 (1)
for num[2] = 2  //there is 1 integer smaller than 2 (1)
for num[3] = 4  //there are 5 integers smaller than 4 (1, 2, 2, 3, 3)
for num[4] = 3  //there are 3 integers smaller than 3 (1, 2, 2)
for num[5] = 10  //there are 7 integers smaller than 10 (5, 1, 2, 2, 4, 3, 3)
for num[6] = 3  //there are 3 integers smaller than 3 (1, 2, 2)

Solution:

First, we need to count the number of integers that are less than integer num[i]. To find this, we can make a copy of the given and sort it. Then, the count of integers less than sorted[i] will be equal to i:

Given array- num = [5, 1, 2, 2, 4, 3, 10, 3]
Sorted array- sorted = [1, 2, 2, 3, 3, 4, 5, 10]
For 1, index = 0, so count of integers less than 1 = 0
For 2, index = 1 (we will see smallest index), so count of integers less than 2 = 1
For 3, index = 3 (we will see smallest index), so count of integers less than 3 = 3
For 4, index = 5, so count of integers less than 4 = 5
For 5, index = 6, so count of integers less than 5 = 6
For 10, index = 7, so count of integers less than 10 = 7

We will store these counts in a map, where the keys will be integer itself and the values will be the count of integers less than that integer. So resulting map will be-
{
1 -> 0
2 -> 1
3 -> 3
4 -> 5
5 -> 6
10 -> 7
}

Then, we will create a result array count and will store counts corresponding to each element num[i].

/**
 * @param {number[]} num
 * @return {number[]}
 */
function findSmallerIntegers(num) {
const length = nums.length
    
    // copy the original array and sort it
    const sorted = [...nums]
    sorted.sort((a, b) => a - b)
    
    // create a map to store counts
    // and store count 0 for smallest number
    const counts = new Map()
    counts.set(sorted[0], 0)
    
    for (let i = 1; i < length; i++) {
        const n = sorted[i]

        // if current number is different from
        // last number
        if (n !== sorted[i - 1]) {
            counts.set(n, i)
        }
    }
    
    const result = new Array(length)
    
    for (let i = 0; i < length; i++) {
        const n = nums[i]
        result[i] = counts.get(n)
    }
    
    return result

}

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

Written by on May 21st, 2021

Next Question

13 Javascript Beginner Level Arrays Interview Questions & Answers

Below is a list of our Javascript Beginner Level Arrays 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 integers smaller than the current number.

  • 2. Create a solution to return a rearranged array.

  • 3. How do you find the sum of unique elements?

  • 4. Can you find the richest person and return their wealth?

  • 5. Create a target array in a given order.

  • 6. How do you sort by parity?

  • 7. What ways can you reach the top of the steps?

  • 8. Create a code to return the maximized sum.

  • 9. Find which kids can be happy after extra candy.

  • 10. Can you find the highest altitude?

  • 11. What is the diagonal sum?

  • 12. What is the maximum number of ice cream bars?

  • 13. Find squares of a sorted array.