Searching & Sorting · Coding problem

Binary Search

A free coding problem from the Binary Search Basics skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

nums is sorted ascending and holds no repeated values. Return the position of target in it, or -1 if it isn't there.

Example

Input:  nums = [-8, -3, 2, 7, 15, 21, 30], target = 15
Output: 4

Solve it in O(log n) time.

Constraints

  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i], target <= 10^4
  • All values in nums are unique, sorted ascending

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Binary Search Basics

Solve it Binary Search free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime