Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function to perform a binary search on a sorted array.

Hard

Problem Statement

Write a function to perform a binary search on a sorted array.

Sample Input

array = [ 1, 3, 5, 7, 9, 11 ], target = 7

Sample Output

3

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
// Perform binary search on a sorted array function binarySearch(arr, target) { // Initialize left and right pointers // Use a loop to find the target by adjusting pointers // Return the index if found, otherwise -1 } console.log(binarySearch([ 1, 3, 5, 7, 9, 11 ], 7));
Log in or sign up to run or submit your code.Log inSign up
Input
array = [ 1, 3, 5, 7, 9, 11 ], target = 7
Expected Output
3