No results found for the specified position. 16 JavaScript Intermediate Level Binary Search Interview Questions

MockQuestions

JavaScript Intermediate Level Binary Search Mock Interview

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

Get More Information About Our JavaScript Intermediate Level Binary Search Interview Questions

Question 1 of 16

What is the smallest divisor with a given limit.

This question focuses on the developer's knowledge of JavaScript binary search and arrays.

You are given an array of integer nums and an integer limit. You will choose an integer divisor, divide all the integers of nums by divisor, and will add the divisions’ result. Find the smallest divisor such that the sum is less than or equal to the limit.

The result of a division is rounded to the nearest integer greater than the actual real result (i.e ceil of the division). For example:- 17 / 3 = 6, and 30 / 6 = 5.

/*Example*/

nums = [1, 2, 5, 9]		limit = 6
expected output = 5
explanation:
●	if the divisor were smaller (say 4), the sum after division would be 7 (1 + 1 + 2 + 3) which is greater than limit.
●	if the divisor were greater (say 6), the sum after division would be 5 (1 + 1 + 1 + 2) which is smaller than limit.

Solution:

We can use binary search. Let’s say min is the smallest divisor required. There are two observations:

● Given a divisor smaller than min, the sum after division will always be greater than the limit.
● Given a divisor larger than or equal to min, the sum after division will always be less than or equal to the limit.

divisors- 1 2 … min min + 1 min + 2 max-divisor
is sum F F F T T T T
less than
or equal
to limit

/**
 * @param {number[]} nums
 * @return {number}
 */
function findMinimumEffort(nums, limit) {
    let left = 1
    let right = Math.max(...nums)
    
    while(left < right) {
        let mid = (left + right) / 2
        mid = Math.floor(mid)
        
        // is sum less than or equal to limit        
        if (isLessOrEqLimit(mid, nums, limit)) {
            right = mid
        } else {
            left = mid + 1
        }
    }
    
    return left
}

function isLessOrEqLimit(divisor, nums, limit) {
    let sum = 0
    
    for (const num of nums) {
        let quotient = num / divisor
        quotient = Math.ceil(quotient)
        sum += quotient
    }
    
    return sum <= limit
}

Time complexity- O(n log (max number))
Space complexity- O(1)

Written by on May 22nd, 2021

Next Question

16 JavaScript Intermediate Level Binary Search Interview Questions & Answers

Below is a list of our JavaScript Intermediate Level Binary Search 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. What is the smallest divisor with a given limit.

  • 2. What are the minimum number of days to make bouquets?

  • 3. How can you minimize the maxOperations penalty?

  • 4. Create a key-value store with timestamp.

  • 5. Find a single occurring integer in the given sorted array.

  • 6. What is the maximum value at a given index in a bounded array?

  • 7. Compare strings by frequency of the smallest character.

  • 8. Can you find the right interval indices for each interval?

  • 9. What is the magnetic force between two balls?

  • 10. How do you search in a rotated sorted array?

  • 11. Find the minimum radius of the heaters where all houses can be warmed.

  • 12. Can you find the H-index?

  • 13. What is the minimum speed to arrive on time?

  • 14. Find the minimum in a rotated sorted array.

  • 15. How do you find the K closest elements?

  • 16. What is the random pick with weight?