Skip to the code
cf.
+25 XP

#167 Oldest Entry in a Ring Log

medium

An app keeps its last few log timestamps in a ring buffer, log. The timestamps were written in increasing order and the buffer wrapped around, so log is a sorted list rotated by some amount. All timestamps are different.

The oldest entry is the smallest timestamp. Write oldestIndex(log) that returns its index, in O(log n) time.

Example 1

Input: log = [50,60,70,10,20,30,40]
Output: 3

The values drop from 70 to 10, so the oldest is at index 3.

Example 2

Input: log = [10,20,30]
Output: 0

No wrap at all: the first entry is the oldest.

Constraints

  • 1 <= log.length <= 100000
  • All timestamps are different.
  • log 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).