Monotonic-Run Shuffle Score
Updated
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[]) → doubleExamples
Example 1
nums = [1,2,3,4]return = 0.25The entire array is one increasing run, so the score is 1 / 4.
Example 2
nums = [1,3,2,4]return = 0.75The 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 Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEWGoogle interview questions
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Policy Violation Set ChecksPinterest · ONSITE INTERVIEWPinterest interview questions
- Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
- Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Alternate Positive and Negative ValuesTesla · ONSITE INTERVIEWTesla interview questions