Beyond Big O

First target index

First target index

Easy

Your task

  • Find the first occurrence of target in a sorted array.
  • Return its smallest matching index, or -1 if target is absent.
  • Return the index, not the value.

Required API

  • FirstTargetIndex
    func FirstTargetIndex(values []int, target int) int

Behavior

  • The array is in nondecreasing order and may contain duplicates.
  • The input is valid: 0–1,000 integers, with every value and target between -100,000 and 100,000 inclusive.
  • Leave the input unchanged and keep separate calls independent.
  • Aim for O(log n) time and O(1) extra space using only the standard library.
  • Explain which interval can still contain the first match.
Notes and example notation

Examples use {"values": [...], "target": ...}.

Examples

The runner calls your functions directly; you do not need to parse JSON.

Example 1 · empty

Arguments
values
[]
target
0
Expected return
-1
Test data
Input
{"values":[],"target":0}
Expected output
-1

Example 2 · first match

Arguments
values
[
  1,
  2,
  2,
  2,
  5
]
target
2
Expected return
1
Test data
Input
{"values":[1,2,2,2,5],"target":2}
Expected output
1

Example 3 · missing

Arguments
values
[
  1,
  3,
  7
]
target
4
Expected return
-1
Test data
Input
{"values":[1,3,7],"target":4}
Expected output
-1

Example 4 · last match

Arguments
values
[
  -4,
  0,
  8
]
target
8
Expected return
2
Test data
Input
{"values":[-4,0,8],"target":8}
Expected output
2
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…