FastPrepMonotonic-Run Shuffle Score

Monotonic-Run Shuffle Score

Microsoft logoMicrosoft● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a nonempty array nums of distinct integers, divide it into maximal contiguous strictly monotonic runs. A run is either strictly increasing or strictly decreasing, and a new run begins whenever the sign of the adjacent difference changes.

Return the number of runs divided by nums.length as a simple shuffledness score.

Function

shuffleScore(nums: int[]) → double

Examples

Example 1

nums = [1,2,3,4]return = 0.25

The entire array is one increasing run, so the score is 1 / 4.

Example 2

nums = [1,3,2,4]return = 0.75

The direction changes twice, creating three runs, so the score is 3 / 4.

Constraints

  • 1 <= nums.length <= 100000
  • -10^9 <= nums[i] <= 10^9
  • All values are distinct
  • Results use absolute tolerance 1e-12

More Microsoft problems

See Microsoft hiring insights
public double shuffleScore(int[] nums) {
  // Write your code here.
}
nums[1,2,3,4]
expected0.25
Checking account…