Find the Repeated and Missing Values in a Square Grid

Quick Overview

Find the single repeated value and single missing value in a square grid containing the numbers from one through n squared, returning repeated before missing.

Find the Repeated and Missing Values in a Square Grid

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Find the Repeated and Missing Values in a Square Grid You are given a 0-indexed `n * n` integer grid whose values lie in `[1,n^2]`. Exactly one value `a` appears twice, exactly one value `b` is missing, and every other value in that range appears exactly once. Return the two-element array `[a,b]`: the repeated value first and the missing value second. ### Examples **Example 1** ```text Input: grid = [[4,1],[4,3]] Output: [4,2] ``` The range is `1..4`. Value `4` appears twice, value `2` never appears, and `1` and `3` each appear once. **Example 2** ```text Input: grid = [[2,8,1],[6,3,9],[7,1,5]] Output: [1,4] ``` Among the values `1..9`, only `1` is repeated and only `4` is absent. ### Constraints - `2 <= n == grid.length == grid[i].length <= 50` for each row `i`. - `1 <= grid[i][j] <= n * n`. - Exactly one value in `[1,n^2]` occurs zero times. - Exactly one value in `[1,n^2]` occurs exactly twice. - Every other value in `[1,n^2]` occurs at exactly one grid position. ### Clarifying Questions - Are the rows and columns equal in count? Yes; the grid is square. - Are there potentially several different missing or repeated values? No; the stated guarantees give exactly one of each. - Should the answer contain coordinates? No; return the values themselves. - Can the answer order be reversed? No; repeated comes first, missing second. - Is an in-place grid modification required? No such requirement is stated; preserve the given method interface and return the two values. ### C++ Interface ```cpp class Solution { public: vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) { } }; ``` ### Reasoning Checkpoints - Use the full value range from one through n squared, including both endpoints. - Distinguish value frequencies from grid coordinates. - Apply the exact input guarantees and preserve the required answer order. ```hint Count occurrences across the grid After counting every cell, which frequency identifies the repeated value, and which identifies the missing one? ```

Overview: Find the single repeated value and single missing value in a square grid containing the numbers from one through n squared, returning repeated before missing.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Find the Repeated and Missing Values in a Square Grid

You are given a 0-indexed n * n integer grid whose values lie in [1,n^2]. Exactly one value a appears twice, exactly one value b is missing, and every other value in that range appears exactly once.

Return the two-element array [a,b]: the repeated value first and the missing value second.

Examples

Example 1

Input: grid = [[4,1],[4,3]]
Output: [4,2]

The range is 1..4. Value 4 appears twice, value 2 never appears, and 1 and 3 each appear once.

Example 2

Input: grid = [[2,8,1],[6,3,9],[7,1,5]]
Output: [1,4]

Among the values 1..9, only 1 is repeated and only 4 is absent.

Constraints

  • 2 <= n == grid.length == grid[i].length <= 50 for each row i .
  • 1 <= grid[i][j] <= n * n .
  • Exactly one value in [1,n^2] occurs zero times.
  • Exactly one value in [1,n^2] occurs exactly twice.
  • Every other value in [1,n^2] occurs at exactly one grid position.

Clarifying Questions Guidance

  • Are the rows and columns equal in count? Yes; the grid is square.
  • Are there potentially several different missing or repeated values? No; the stated guarantees give exactly one of each.
  • Should the answer contain coordinates? No; return the values themselves.
  • Can the answer order be reversed? No; repeated comes first, missing second.
  • Is an in-place grid modification required? No such requirement is stated; preserve the given method interface and return the two values.

C++ Interface

class Solution {
public:
    vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
        
    }
};

Reasoning Checkpoints

  • Use the full value range from one through n squared, including both endpoints.
  • Distinguish value frequencies from grid coordinates.
  • Apply the exact input guarantees and preserve the required answer order.