MockQuestions

Java Intermediate Level Binary Search Mock Interview

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

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

Question 1 of 16

What is the smallest divisor with a given limit?

This interview question concentrates on the developer's skills with Java array and binary search.

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

import java.util.*;
 
class Solution {
    public int smallestDivisor(int[] nums, int threshold) {
        int left = 1;
        int right = Arrays.stream(nums).max().getAsInt();
        while(left < right) {
            int mid = left + (right -left) / 2;
       	 
            // is sum less than or equal to limit
            boolean isOk = isLessOrEqLimit(mid, nums, threshold);
            if (isOk) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
    
    boolean isLessOrEqLimit(int divisor, int[] nums, int limit) {
        int sum = 0;
        for (int num : nums) {
            double quotient = ((double)num / (double)divisor);
            quotient = Math.ceil(quotient);
            sum += (int)quotient;
        }
        return sum <= limit;
    }
 
}

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

Written by on May 22nd, 2021

Next Question

16 Java Intermediate Level Binary Search Interview Questions & Answers

Below is a list of our Java 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. Can you find the single integer in a sorted array?

  • 5. Find the key-value store with timestamp.

  • 6. How do you find the maximum value at a given index in a bounded array?

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

  • 8. Return the right interval index.

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

  • 10. Can you search a rotated sorted array?

  • 11. What is the minimum radius of the heaters to heat all of the houses?

  • 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. Can you find the K closest elements in the array?

  • 16. What is the random pick with weights?