FastPrepNearest-Rank Percentile Without Sorting

Nearest-Rank Percentile Without Sorting

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a nonempty integer array values and an integer percentile p from 1 through 100, return the nearest-rank percentile.

Its one-based rank is ceil(p * values.length / 100). Return the value at that rank in sorted order, but do not sort the complete array. You may rearrange values.

Function

nearestRankPercentile(values: int[], p: int) → int

Examples

Example 1

values = [7,1,3,9,5]p = 50return = 5

The nearest rank is ceil(50 * 5 / 100) = 3; the third sorted value is 5.

Example 2

values = [10,20,30,40]p = 75return = 30

The nearest rank is 3, so the answer is 30.

Constraints

  • 1 <= values.length <= 200000
  • -10^9 <= values[i] <= 10^9
  • 1 <= p <= 100

More Microsoft problems

See Microsoft hiring insights
public int nearestRankPercentile(int[] values, int p) {
  // Write your code here.
}
values[7,1,3,9,5]
p50
expected5
Checking account…