No results found for the specified position. 15 Python Intermediate Level Binary Search Interview Questions

MockQuestions

Python Intermediate Level Binary Search Mock Interview

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

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

Question 1 of 15

What is the smallest divisor with a given limit?

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

You are given an array of integers 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

def smallestDivisor(nums, limit):
# choose the range for making a binary search
	mini, maxi = 1, max(nums)

	while mini < maxi:
		mid = (mini + maxi) // 2

		# if mid passes the limit or not
		if isLessOrEqLimit(mid, nums, limit):
			maxi = mid
		else:
			mini = mid + 1

	return mini

def ceil_divison(num, den):
	''' returns ceil division of two numbers '''
	return (num + den - 1) // den

def isLessOrEqLimit(divisor, nums, limit):
	'''
		returns true or false based on the limit and divisor
		whether the final sum after division is under limit 
		or not
	'''
	divison_result = [ ceil_divison(num,divisor) for num in nums] 
	return sum(divison_result) <= limit

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

Written by on May 22nd, 2021

Next Question

15 Python Intermediate Level Binary Search Interview Questions & Answers

Below is a list of our Python 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. Find a single occurring integer in the given sorted array.

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

  • 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. What is the minimum speed to arrive on time?

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

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

  • 15. What is the random pick with weight?