FastPrepValue Filling Over Half of an Array
Problems/Value Filling Over Half of an Array

Value Filling Over Half of an Array

Adobe logoAdobe● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem returns the value that fills more than half of an array's positions, ideally in one pass with constant extra memory. An Adobe MTS-2 (Java) report gives this as the first of two questions in its first DSA round, only as a link to the well-known problem, and notes that this elimination round wanted working code with the best possible time and memory; it gives no examples or limits of its own.

Our choices: the usual rules of that problem (such a value always exists), at most 20000 values from -10^9 to 10^9, and new examples. The judge checks only the returned value, not the memory used.

The judged task matches the report at about 90%.

In the array nums, one value appears in more than half of the positions. Return that value.

Try to use O(n) time and only O(1) extra space.

Function

majorityValue(nums: int[]) → int

Examples

Example 1

nums = [7,1,7,7,4]return = 7

7 appears 3 times out of 5, more than half.

Example 2

nums = [-2,-2,9,9,-2,9,-2]return = -2

-2 appears 4 times out of 7 and 9 only 3, so -2 is the answer.

Example 3

nums = [42]return = 42

A single value is more than half of a one-value array.

Constraints

  • 1 <= nums.length <= 20000
  • -10^9 <= nums[i] <= 10^9
  • some value appears more than nums.length / 2 times

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int majorityValue(int[] nums) {
    // Write your code here.
}
Checking account…