Asked at 11 companies
5 recent
- Bloomberg
- Amazon
- Microsoft
- Meta
- SAP
- Pure Storage
- Infosys
- Oracle
- tcs
+1 moreShow fewer
- Apple
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Two pointers from opposite ends · When sorting first unlocks the pattern · Make the input smaller every call
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · git bisect: To find the commit that broke a build, git halves the range of commits on every test, so even 1,000 commits take about 10 checks.
ProblemProblem statementGiven an array of integers nums sorted in ascending order, and an integer target, write a function to search target in nums. Return its index, or -1 if it doesn't exist.ReadHide
Problem Statement
Given an array of integers nums sorted in ascending order, and an integer target, write a function to search target in nums. Return its index, or -1 if it doesn't exist.
Imported 2026-10-09 from NeetCode auto-sync (reviewed, debug prints removed).
Input / Output
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Halving the Search Space — the array is sorted, so one comparison at the middle eliminates half the candidates. The <= loop condition and mid ± 1 updates are the details that matter.
Approach
Brute Force — Linear Scan, O(n)
for i, x in enumerate(nums): if x == target: return i
return -1
Optimized — Binary Search, O(log n)
left = 0, right = n - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target: return mid
if nums[mid] < target: left = mid + 1
else: right = mid - 1
return -1
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Linear scan | O(n) | O(1) | Ignores sortedness |
| Binary search | O(log n) | O(1) | Halves candidates each step |
Solution code
from typing import List
class SolutionBruteForce:
"""O(n) time | O(1) space — linear scan, ignores sortedness."""
def search(self, nums: List[int], target: int) -> int:
for i, x in enumerate(nums):
if x == target:
return i
return -1
class Solution:
"""O(log n) time | O(1) space — halve the search space each step."""
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language