Implement a binary search algorithm.
Answer:
Python3
def binary_search(arr, target): low = 0 high = len (arr) - 1 while low < = high: mid = (low + high) / / 2 if arr[mid] = = target: return mid elif arr[mid] < target: low = mid + 1 else : high = mid - 1 return - 1 # Example usage arr = [ 1 , 3 , 5 , 7 , 9 ] target = 5 print (binary_search(arr, target)) # Output: 2 (index of 5 in arr) |
Explanation: This function implements the binary search algorithm to find the index of a target element in a sorted array. It repeatedly divides the array in half and narrows down the search space until the target is found or the search space is empty.
Software Developer Interview Questions
Software developer interview questions can cover a wide range of topics, depending on the specific role, company, and level of experience being targeted. The software developer interview questions aim to assess a candidate’s technical skills, problem-solving abilities, communication skills, and fit for the role and the company.
Table of Content
- Interview Questions for Internship and Fresher Level
- Interview Questions for Software Development Engineer SDE 1 level
- Interview Questions for Software Development Engineer SDE 2 level
- Interview Questions for Software Development Engineer SDE 3 level