Find the first index of a target in a bitonic (increasing then decreasing) array

Quick Overview

Given an array that strictly rises to a single peak and then strictly falls, return the first index of a target value, or -1 when it is missing. Tests binary search for the peak, searching each monotonic side in the correct direction, and edge cases such as fully increasing or fully decreasing arrays.

Find the first index of a target in a bitonic (increasing then decreasing) array

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a bitonic array `arr` and an integer `target`, return the index of `target` in `arr`. If `target` does not exist in the array, return `-1`. A bitonic array is first strictly increasing and then strictly decreasing. It contains a single peak element, and the elements on each side of the peak are strictly monotonic. An array that is only increasing or only decreasing is also considered bitonic. If `target` appears more than once in the array, return the index of its first occurrence (the smallest index). ### Function Signature ```python def search_bitonic(arr: list[int], target: int) -> int: ``` ### Rules - Indices are 0-based. - A value can appear once before the peak and once after it. In that case, return the smaller index. ### Constraints - `1 <= len(arr) <= 10^4` - `-10^9 <= arr[i], target <= 10^9` - `arr` is a valid bitonic array (strictly increasing, then strictly decreasing). ### Examples ```text Input: arr = [1, 3, 8, 12, 4, 2], target = 4 Output: 4 ``` The array rises to its peak 12 at index 3 and then falls. The value 4 is at index 4, on the falling side. ```text Input: arr = [10, 9, 8], target = 5 Output: -1 ``` This array is strictly decreasing, which counts as bitonic. The value 5 does not appear, so the answer is `-1`. ```text Input: arr = [1, 3, 8, 12, 4, 3], target = 3 Output: 1 ``` The value 3 appears at index 1 on the rising side and at index 5 on the falling side. The first occurrence is index 1.

Overview: Given an array that strictly rises to a single peak and then strictly falls, return the first index of a target value, or -1 when it is missing. Tests binary search for the peak, searching each monotonic side in the correct direction, and edge cases such as fully increasing or fully decreasing arrays.

Grab logo
Grab
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a bitonic array arr and an integer target, return the index of target in arr. If target does not exist in the array, return -1.

A bitonic array is first strictly increasing and then strictly decreasing. It contains a single peak element, and the elements on each side of the peak are strictly monotonic. An array that is only increasing or only decreasing is also considered bitonic.

If target appears more than once in the array, return the index of its first occurrence (the smallest index).

Function Signature

def search_bitonic(arr: list[int], target: int) -> int:

Rules

  • Indices are 0-based.
  • A value can appear once before the peak and once after it. In that case, return the smaller index.

Constraints

  • 1 <= len(arr) <= 10^4
  • -10^9 <= arr[i], target <= 10^9
  • arr is a valid bitonic array (strictly increasing, then strictly decreasing).

Examples

Input: arr = [1, 3, 8, 12, 4, 2], target = 4
Output: 4

The array rises to its peak 12 at index 3 and then falls. The value 4 is at index 4, on the falling side.

Input: arr = [10, 9, 8], target = 5
Output: -1

This array is strictly decreasing, which counts as bitonic. The value 5 does not appear, so the answer is -1.

Input: arr = [1, 3, 8, 12, 4, 3], target = 3
Output: 1

The value 3 appears at index 1 on the rising side and at index 5 on the falling side. The first occurrence is index 1.