Locate Any Strict Peak in a Matrix

Quick Overview

Locate Any Strict Peak in a Matrix. No side-adjacent entries of rectangular mat are equal. A peak exceeds every existing up, down, left, and right neighbor; outside neighbors have value -1.

Locate Any Strict Peak in a Matrix

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem No side-adjacent entries of rectangular `mat` are equal. A peak exceeds every existing up, down, left, and right neighbor; outside neighbors have value -1. Return coordinates `[row,column]` of any peak. Use `O(m log n)` or `O(n log m)` time for m rows and n columns. ```cpp class Solution { public: vector<int> findPeakGrid(vector<vector<int>>& mat); }; ``` ### Examples **Example 1:** ```text mat = [[2,8],[5,3]] One valid output: [0,1] Explanation: 8 is a peak, and [1,0] at value 5 is another permitted answer. ``` **Example 2:** ```text mat = [[4,7,2],[3,9,5]] One valid output: [1,1] Explanation: 9 exceeds all existing side neighbors and the outside value -1. ``` ### Constraints - `m == mat.length`, `n == mat[i].length`. - `1 <= m,n <= 500`. - `1 <= mat[i][j] <= 10^5`. - No side-adjacent cells have equal values. - Required time: `O(m log n)` or `O(n log m)`. ### Clarifying Questions Any qualifying peak is accepted; there can be several. Diagonal neighbors do not affect the condition. ```hint Inspect the structure Which neighboring cells matter, what boundary values apply, and may different valid coordinates be accepted? ``` ### What a Strong Answer Covers - Show that the returned coordinates exceed every relevant side-adjacent value. - Justify the required time bound while accepting any qualifying peak and handling one-row or one-column inputs. ### Follow-up Questions When rows greatly outnumber columns, how could choosing the other split direction reduce work?

Overview: Locate Any Strict Peak in a Matrix. No side-adjacent entries of rectangular mat are equal. A peak exceeds every existing up, down, left, and right neighbor; outside neighbors have value -1.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

No side-adjacent entries of rectangular mat are equal. A peak exceeds every existing up, down, left, and right neighbor; outside neighbors have value -1. Return coordinates [row,column] of any peak. Use O(m log n) or O(n log m) time for m rows and n columns.

class Solution {
public:
    vector<int> findPeakGrid(vector<vector<int>>& mat);
};

Examples

Example 1:

mat = [[2,8],[5,3]]
One valid output: [0,1]
Explanation: 8 is a peak, and [1,0] at value 5 is another permitted answer.

Example 2:

mat = [[4,7,2],[3,9,5]]
One valid output: [1,1]
Explanation: 9 exceeds all existing side neighbors and the outside value -1.

Constraints

  • m == mat.length , n == mat[i].length .
  • 1 <= m,n <= 500 .
  • 1 <= mat[i][j] <= 10^5 .
  • No side-adjacent cells have equal values.
  • Required time: O(m log n) or O(n log m) .

Clarifying Questions Guidance

Any qualifying peak is accepted; there can be several. Diagonal neighbors do not affect the condition.

What a Strong Answer Covers Guidance

  • Show that the returned coordinates exceed every relevant side-adjacent value.
  • Justify the required time bound while accepting any qualifying peak and handling one-row or one-column inputs.

Follow-up Questions Guidance

When rows greatly outnumber columns, how could choosing the other split direction reduce work?