Technical Interview Questions: Binary Search Algorithm

Technical Interview Questions - Binary Search Algorithm

Search algorithms are the essential part of the interviews. And there is a high possibility to face a search algorithm during an interview, tech stack independently. That’s why I decided to walk you through one of the most efficient searching algorithms.

The first round of the most common Interview questions was Sliding Window Technique. So, in Technical Interview Questions: Sliding Window Technique tutorial, I tried to solve a challenging most-asked question with this technique. 

The winner of the second round indeed will be the Binary Search Algorithm. 

Binary Search is one of the most efficient searching algorithms with O(log n) time complexity for the worst-case scenario. If the central index matches the desired value, the best case time complexity would be O(1). 

Other common names of Binary Search are Half-Interval Search and Logarithmic Search.

How Binary Search Works?

The binary search algorithm is working on the principle of repeatedly dividing the search interval in half and finding a target within this half. It compares the desired value to the middle element of the array. In case the middle element does not match the desired value, it will keep looking in half of the array and eliminate another half. To discard half of the array and apply Binary Search, the array must be sorted beforehand.

sortedArray = [1,4,5,7,9,13,22];

searchTarget = 9;

Iteration Start;

  1. Element at index 0 sortedArray[0]=1
  2. Last element at index 6 sortedArray[6]=22
  3. Middle element at index 3 is sortedArray[3]=7
  4. Is the middle element equals to the target ? 
  5. 7==9? No!
  6. The target is greater than the middle element, so we can discard the left side of the array and continue to iterate the right side. The remained part of the array we need to consider = [9,13,22];
  7. The new middle is sortedArray[5]=13
  8. Is the middle element equal to the target? 
  9. 13==9? No!  
  10. The target is lower than the middle element, so we can discard the right side of the array and continue to iterate the left side. 
  11. The remained part of the array we need to consider = [9];
  12. Is the middle element equal to the target? 
  13. 9==9?
  14. Yes, the only remaining element in the array is equal to the target, finalizing the binary search iteration for this particular dataset.
  15. The target element has been found for the given array!
int[] sortedArray = {1, 4, 5, 7, 9, 13, 22};
int searchTarget = 9;
binarySearch(sortedArray, searchTarget);
public void binarySearch(int[] sortedArray, int searchTarget) {
 int leftIndex = 0;
 int rightIndex = sortedArray.length - 1;
 while (leftIndex <= rightIndex) {
   int middleElement = leftIndex + ((rightIndex - leftIndex) / 2);
   if (sortedArray[middleElement] < searchTarget) {
     leftIndex = middleElement + 1;
   } else if (sortedArray[middleElement] > searchTarget) {
     rightIndex = middleElement - 1;
   } else if (sortedArray[middleElement] == searchTarget) {
     System.out.println("Target element is found at index: " + middleElement);
     break;
   }
 }
 if (leftIndex > rightIndex) {
   System.out.println("Target element is not found!");
 }
}

Interviewing for a new job might cause anxiety and stress, but hey, you can prepare for most of the questions. Eventually, with a good plan, you can impress even the toughest interviewers with your successful performance. Feel free to follow the Exceptionly Tech Blog to keep yourself updated. Exceptionly Blog is challenging tools, best practices, and methodologies.

Check out some of my other posts that might help you with your interview preparation!

Technical Interview Questions: Sliding Window Technique

Common Mistakes Every Beginner Java Programmer Makes  

One thought on "Technical Interview Questions: Binary Search Algorithm"

  1. Pingback: Exceptionly

Leave a Reply

Your email address will not be published.

#

“Exceptionly was such a fast and efficient process. Complete game changer for hiring remote.”

Liam Martin
Co-Founder at TimeDoctor

#

“Great service, outstanding speed, stunning quality.”

Andy Tryba
CEO at Gigster

Is Exceptionly an outsourcing business?

No. We encourage our clients to do direct hires. We provide payroll consultancy and services in a 100% transparent way if our clients are new to remote hiring.

Is Exceptionly a database of candidates?

Not really, data is only a small part of our business. While we own over 2 million publicly available software engineer data evaluated, our power lies in hands-on testing and engineer-to-engineer technical interviews.

Is Exceptionly expensive?

Exceptionly is more cost-efficient than any other serious competitor who owns a decent technical testing funnel. If you compare Exceptionly with HR agencies who only do resume screening before endorsement, yes, it is expensive.

Who is the founder of Exceptionly?

Sinan Ata is the founder of Exceptionly, who previously built two global technical talent acquisition enginess and tested over 2.5 million software engineers in his career.

Still have questions? Contact us.

Ready to meet top-notch engineers?

Let's get your talent pipelines up and running!

HIRE WITH EXCEPTIONLY

No hidden fees.