Skip to the code
cf.
+25 XP

#166 Search a Wrapped Event Buffer

medium

A performance recorder keeps event IDs in a fixed-size circular buffer. The IDs were written in increasing order, but because the buffer wraps around, the array buffer is a sorted list that was rotated: for example [40, 52, 67, 3, 18, 29]. All IDs are different.

Write searchBuffer(buffer, target) that returns the index of target in buffer, or -1 if it is not there.

Use O(log n) time.

Example 1

Input: buffer = [40,52,67,3,18,29], target = 18
Output: 4

18 is at index 4, in the part after the wrap.

Example 2

Input: buffer = [40,52,67,3,18,29], target = 50
Output: -1

50 is not in the buffer.

Constraints

  • 1 <= buffer.length <= 100000
  • All values are different.
  • buffer is a sorted array rotated by some amount (possibly 0).
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).