Leetcode #1538: Guess the Majority in a Hidden Array
In this guide, we solve Leetcode #1538 Guess the Majority in a Hidden Array in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
We have an integer array nums, where all the integers in nums are 0 or 1. You will not be given direct access to the array, instead, you will have an API ArrayReader which have the following functions: int query(int a, int b, int c, int d): where 0 <= a < b < c < d < ArrayReader.length().
Quick Facts
- Difficulty: Medium
- Premium: Yes
- Tags: Array, Math, Interactive
Intuition
There is a mathematical invariant or formula that directly leads to the result.
Using math avoids unnecessary loops and reduces complexity.
Approach
Derive the formula or update rule, then compute the answer directly.
Handle edge cases like overflow or zero carefully.
Steps:
- Identify the math relationship.
- Compute the result with a loop or formula.
- Handle edge cases.
Example
Input: nums = [0,0,1,0,1,1,1,1]
Output: 5
Explanation: The following calls to the API
reader.length() // returns 8 because there are 8 elements in the hidden array.
reader.query(0,1,2,3) // returns 2 this is a query that compares the elements nums[0], nums[1], nums[2], nums[3]
// Three elements have a value equal to 0 and one element has value equal to 1 or viceversa.
reader.query(4,5,6,7) // returns 4 because nums[4], nums[5], nums[6], nums[7] have the same value.
we can infer that the most frequent value is found in the last 4 elements.
Index 2, 4, 6, 7 is also a correct answer.
Python Solution
# """
# This is the ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
# class ArrayReader(object):
# # Compares 4 different elements in the array
# # return 4 if the values of the 4 elements are the same (0 or 1).
# # return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa.
# # return 0 : if two element have a value equal to 0 and two elements have a value equal to 1.
# def query(self, a: int, b: int, c: int, d: int) -> int:
#
# # Returns the length of the array
# def length(self) -> int:
#
class Solution:
def guessMajority(self, reader: "ArrayReader") -> int:
n = reader.length()
x = reader.query(0, 1, 2, 3)
a, b = 1, 0
k = 0
for i in range(4, n):
if reader.query(0, 1, 2, i) == x:
a += 1
else:
b += 1
k = i
y = reader.query(0, 1, 2, 4)
if reader.query(1, 2, 3, 4) == y:
a += 1
else:
b += 1
k = 0
if reader.query(0, 2, 3, 4) == y:
a += 1
else:
b += 1
k = 1
if reader.query(0, 1, 3, 4) == y:
a += 1
else:
b += 1
k = 2
if a == b:
return -1
return 3 if a > b else k
Complexity
The time complexity is , where is the length of the array. The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.