Binary Search


Difficulty: 3 | Problem written by zeyad_omar
Problem reported in interviews at


Binary search is a searching algorithm that is very important and widely used in Computer Science.

Binary search is beautiful due to the fact that it is very fast compared to most searching techniques: complexity O(log(n)).

In this problem you are asked to implement binary search function given a 1D array and a target value.

Return the index of the target value, and if it is not found, return -1.

*Hint: Assume we are searching for the word "engineer" in the dictionary. At first, we open the middle of the dictionary. We find that it is in the "L" section so we know that our word is in the first half of the book. So we search in the middle of the first half. And so on...

**Note: Binary search only works when the input is sorted.

Sample Input:
<class 'list'>
x: [1, 2, 3, 4, 5]
<class 'int'>
target: 3

Expected Output:
<class 'int'>

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

This is a premium feature.
To access this and other such features, click on upgrade below.

Log in to post a comment

Jump to comment-147
abhishek_kumar • 3¬†months, 1¬†week ago


I don't know why but only iterative approach is acceptable here!!!


Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)