Magicsheet logo

Guess the Majority in a Hidden Array

Medium
25%
Updated 8/1/2025

Asked by 1 Company

Guess the Majority in a Hidden Array

What is this problem about?

The Guess the Majority in a Hidden Array coding problem involves a hidden array of 0s and 1s. You have an API query(a, b, c, d) that takes four distinct indices and returns the number of elements among those four indices that have the same value. Your task is to find the index of an element that belongs to the majority class (0 or 1). If there is a tie, return -1.

Why is this asked in interviews?

Google uses this Interactive and Math interview pattern to evaluate a candidate's logical deduction and ability to minimize API calls. It tests if you can uncover the relative relationships between elements without knowing their absolute values. It’s an advanced brainteaser that requires structured grouping and counting based on differential responses.

Algorithmic pattern used

This problem uses Differential Logic / Grouping.

  1. Establish a Baseline: Query (0, 1, 2, 3). Then query (1, 2, 3, 4).
  2. If the results are the same, array[0] MUST equal array[4]. If different, they are opposite. You can find the relationship between index 0 and all other indices i4i \ge 4 using this logic.
  3. Find relationships for 1, 2, 3: Use similar differential queries (e.g., query (0, 2, 3, 4) to find the relationship between 1 and 4, which relates back to 0).
  4. Count: Since you know whether every element is the "same as 0" or "different from 0", count the two groups.
  5. If the "same as 0" group is larger, return index 0. If smaller, return an index from the other group. If equal, return -1.

Example explanation

Hidden Array: [1, 1, 0, 1, 1] (Majority is 1).

  • Let's relate everything to index 0.
  • q1 = query(0,1,2,3) -> returns 2 (two 1s, two 0s? Wait, indices 0,1,3 are 1, index 2 is 0. So 3 are same, 1 is diff. Returns 3? Actually, the API usually returns 4, 2, or 0 representing pairs. Let's assume it returns 2 for a 3-1 split, or 4 for a 4-0 split).
  • Regardless of exact API return, query(1,2,3,4) compares index 4 against 1,2,3. By comparing q1 and q2, we know if 0 and 4 are the same.
  • We build groups: Group A (same as 0) = {0, 1, 3, 4}. Group B (diff from 0) = {2}.
  • Group A has 4 elements, B has 1. Group A is majority. Return index 0.

Common mistakes candidates make

  • Too Many Queries: Trying to query every combination, exceeding the allowed API call limit.
  • Logic Gaps: Successfully relating indices 4 to N with index 0, but failing to find the relationship between 0 and indices 1, 2, 3.
  • Absolute Values: Trying to figure out if index 0 is actually a 1 or a 0. It doesn't matter. You only need relative groups.

Interview preparation tip

In interactive array problems, always look for the "swap one element" trick. If f(A, B, C, D) and f(B, C, D, E) give different results, the difference is entirely caused by A and E being different. This forms the basis of logical deduction.

Similar Questions