PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

Return the first and last index of target in a sorted array, or [-1, -1].

Constraints

  • nums is sorted non-decreasing

Examples

Input: ([5, 7, 7, 8, 8, 10], 8)

Expected Output: [3, 4]

Explanation: Prompt example.

Input: ([1, 2, 3], 4)

Expected Output: [-1, -1]

Explanation: Absent target.

Input: ([], 0)

Expected Output: [-1, -1]

Explanation: Empty array.

Input: ([2, 2, 2], 2)

Expected Output: [0, 2]

Explanation: All values match.

Hints

  1. Use lower and upper bounds.
Last updated: Jun 27, 2026

Loading coding console...

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.

Related Coding Questions

  • Multi-Agent Collision Simulator (Unicycle Kinematics) - Applied (medium)
  • 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)