Element Appearing More Than 25% In Sorted Array

EasyArrays & Hashingbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array arr sorted in non-decreasing order. Exactly one value in it occurs strictly more than 25% of the time (more than arr.length / 4 occurrences). Return that value. A linear scan works; the sorted order also allows an O(1)-extra-space check that compares each element with the one a quarter of the array ahead.

Examples

Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6
6 occurs 4 times out of 9, which is more than 9 / 4 = 2.25.
Input: arr = [5,5]
Output: 5
The only value occurs 100% of the time.

Constraints

  • 1 <= arr.length <= 10^4
  • 0 <= arr[i] <= 10^5 (tests may also use negatives)
  • `arr` is sorted in non-decreasing order.
  • Exactly one value occurs more than `arr.length / 4` times.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems