Skip to the code
+25 XP

#156 Span of a Repeated Rating

medium

A review page lists every rating of a product in ratings, sorted from lowest to highest, so equal ratings sit next to each other. A filter chip needs to highlight all ratings equal to target.

Write ratingSpan(ratings, target) that returns [first, last]: the first and last index holding target. If target is not in the list, return [-1, -1].

Use O(log n) time.

Example 1

Input: ratings = [1,2,4,4,4,5], target = 4
Output: [2,4]

The 4s sit at indexes 2, 3, and 4.

Example 2

Input: ratings = [1,2,4,4,4,5], target = 3
Output: [-1,-1]

No rating is 3.

Constraints

  • 0 <= ratings.length <= 100000
  • ratings is sorted ascending.
  • -10^9 <= ratings[i], target <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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