Randomly Select a Maximum-Value Index
Updated
Problem statement
Choose an index uniformly at random among the positions containing the largest value in nums. Equal maximum values at different positions are separate choices. Use O(1) extra space; multiple passes over the array are allowed.
For this exercise, assume the single random choice is supplied as draw. If the maximum occurs m times, draw is a zero-based rank in [0,m-1], drawn uniformly from that range. Return the index of that ranked maximum occurrence when positions are ordered from left to right. This makes each supplied draw reproducible; do not make another random call.
Return the original array index, not the maximum value or its rank. Do not reorder the array.
Function
pickMaximumIndex(nums: int[], draw: int) → intExamples
Example 1
nums = [4,1,4,2,4]draw = 1return = 2The maximum is 4 at indices 0, 2 and 4. Rank 1 selects index 2.
Example 2
nums = [-8,-2,-5,-2]draw = 0return = 1The maximum is -2, not zero. Its first occurrence is index 1.
Example 3
nums = [3,3,7,2,7]draw = 1return = 4Occurrences of the earlier maximum 3 do not count after 7 appears. Rank 1 among the final maxima selects index 4.
Example 4
nums = [9]draw = 0return = 0The sole index is the only eligible outcome.
Constraints
- For this exercise, assume
1 <= nums.length <= 20000and-10^9 <= nums[i] <= 10^9. 0 <= draw < m, wheremis the number of maximum-value positions.- Use constant extra space, excluding the input array.
More Meta Onsite Interview questions
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
- Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
- Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
- Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Sliding-Window Averages as Reduced FractionsCoinbase · ONSITE INTERVIEWCoinbase interview questions
- Apply a Sequence of Array RotationsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Generate Unique Random NumbersGoogle · ONSITE INTERVIEWGoogle interview questions
- K-Means Delivery ZonesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences