← All questions
MediumCoding

Find First and Last Position of Element in Sorted Array

Asked atSlice
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Use lower bound to find first index with value at least target.
  2. 02Verify it equals target.
  3. 03Use upper bound to find first index greater than target.
  4. 04Subtract one for the last occurrence.
  5. 05Return missing markers if target is absent.

Reference answer

Then expect these follow-ups

  • How does lower bound differ from a normal search?

    Tests: follow-up reasoning

  • Can you write this with inclusive bounds safely?

    Tests: follow-up reasoning

Hiring now

Slice has 19 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Social Media Coordinator (Part-time)

    Pristina, Kosovo

  • Senior Inside Sales Representative

    Skopje, Pristina

  • Engineering Manager, Services

    Remote

Or practice this question first

Next question