PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/TikTok

Find first and last index of target

Last updated: Mar 29, 2026

Quick Overview

This question evaluates mastery of searching algorithms and algorithmic complexity analysis, particularly the ability to locate boundary positions within a sorted array.

  • easy
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Find first and last index of target

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are given an integer array `nums` sorted in non-decreasing order and an integer `target`. Return a pair `[left, right]` where: - `left` is the index of the first occurrence of `target` in `nums` - `right` is the index of the last occurrence of `target` in `nums` If `target` does not exist in the array, return `[-1, -1]`. ## Requirements - Your algorithm must run in **O(log n)** time. ## Input/Output - **Input:** `nums: int[]`, `target: int` - **Output:** `int[2]` representing `[left, right]` ## Constraints - `0 <= nums.length <= 1e5` - `-1e9 <= nums[i], target <= 1e9` - `nums` is sorted in non-decreasing order. ## Examples - `nums = [5,7,7,8,8,10]`, `target = 8` → `[3,4]` - `nums = [5,7,7,8,8,10]`, `target = 6` → `[-1,-1]` - `nums = []`, `target = 0` → `[-1,-1]`

Quick Answer: This question evaluates mastery of searching algorithms and algorithmic complexity analysis, particularly the ability to locate boundary positions within a sorted array.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Oct 19, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
5
0

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

Return a pair [left, right] where:

  • left is the index of the first occurrence of target in nums
  • right is the index of the last occurrence of target in nums

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

Requirements

  • Your algorithm must run in O(log n) time.

Input/Output

  • Input: nums: int[] , target: int
  • Output: int[2] representing [left, right]

Constraints

  • 0 <= nums.length <= 1e5
  • -1e9 <= nums[i], target <= 1e9
  • nums is sorted in non-decreasing order.

Examples

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

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

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

Master your tech interviews with 8,500+ 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.