First and Last Position

Talk it through out loud with your AI coach.

The problem

  • medium
  • Pro

Given a sorted list that may contain repeats and a target value, find the first and last positions where the target appears, or report that it's missing. Explain how to do it in logarithmic time.

What a strong answer covers

  • Why finding one match and walking outwards can be slow.
  • Adjusting the search so it keeps going after a match.
  • Finding both ends of the run of matches.
  • The overall cost and the not-found case.

Common mistake: Stopping at the first match found, which may sit anywhere inside the run.

Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.

Practise it out loud