PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Applied

Find first and last occurrence in sorted array

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in array search algorithms and algorithmic reasoning, specifically the use of binary search techniques and careful boundary/edge-case handling to identify first and last occurrences.

  • medium
  • Applied
  • Coding & Algorithms
  • Software Engineer

Find first and last occurrence in sorted array

Company: Applied

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given an array of integers `nums` sorted in non-decreasing order and an integer `target`. Return a pair of indices `[first, last]` such that: - `first` is the smallest index `i` with `nums[i] == target` - `last` is the largest index `j` with `nums[j] == target` If `target` does not exist in the array, return `[-1, -1]`. ## Requirements - Aim for an algorithm better than linear time (i.e., use binary search style reasoning). ## Input / Output - **Input:** `nums: int[]`, `target: int` - **Output:** `int[2]` representing `[first, last]` ## Constraints (typical) - `0 <= nums.length <= 10^5` - `nums` is sorted in non-decreasing order - Values fit in 32-bit signed integers ## Example - `nums = [5,7,7,8,8,10]`, `target = 8` → `[3,4]` - `nums = [1,2,3]`, `target = 4` → `[-1,-1]`

Quick Answer: This question evaluates proficiency in array search algorithms and algorithmic reasoning, specifically the use of binary search techniques and careful boundary/edge-case handling to identify first and last occurrences.

Related Interview Questions

  • Merge Overlapping Collinear Segments - Applied (hard)
  • Implement a Fixed-Capacity Deque - Applied (medium)
  • Implement Nested Transactional Key-Value Store - Applied (hard)
  • Merge overlapping 2D line segments - Applied (medium)
  • Find intersection of two line segments - Applied (easy)
Applied logo
Applied
Feb 12, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

Problem

You are given an array of integers nums sorted in non-decreasing order and an integer target.

Return a pair of indices [first, last] such that:

  • first is the smallest index i with nums[i] == target
  • last is the largest index j with nums[j] == target

If target does not exist in the array, return [-1, -1].

Requirements

  • Aim for an algorithm better than linear time (i.e., use binary search style reasoning).

Input / Output

  • Input: nums: int[] , target: int
  • Output: int[2] representing [first, last]

Constraints (typical)

  • 0 <= nums.length <= 10^5
  • nums is sorted in non-decreasing order
  • Values fit in 32-bit signed integers

Example

  • nums = [5,7,7,8,8,10] , target = 8 → [3,4]
  • nums = [1,2,3] , target = 4 → [-1,-1]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Applied•More Software Engineer•Applied Software Engineer•Applied Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.