Regional Maximum Finder
Updated
Problem statement
You are given a 2D array of non-negative integers. A cell can be a regional maximum. Return the positions [i, j] of all regional maxima as a 2D array with 2 columns.
Regional maximum. A cell (i, j) is a regional maximum if both are true:
array[i][j] != 0array[i][j]is the maximum value in its region. A cell that ties with other cells in its region still counts as a maximum.
Region. Let v = array[i][j]. The region of (i, j) is the square of rows i - v to i + v and columns j - v to j + v, with these changes:
- Leave out the four corner cells of the square:
(i - v, j - v),(i - v, j + v),(i + v, j - v)and(i + v, j + v). - If part of the square is outside the array, ignore those cells.
List the positions in row order: smaller i first, and for the same i, smaller j first. If there is no regional maximum, return an empty array.
Complete the function findRegionalMaxima in the editor.
findRegionalMaxima has the following parameter:
int[][] array: a 2D array of integers
The function returns an int[][]: one row [i, j] for each regional maximum.
Function
findRegionalMaxima(array: int[][]) → int[][]Examples
Example 1
array = [[3, 0, 1], [2, 0, 0], [0, 0, 0]]return = [[0, 0], [0, 2]]The array is:
[
[3, 0, 1],
[2, 0, 0],
[0, 0, 0]
]The cell [0, 0] has value 3, so its square reaches 3 cells in every direction. Cut to the array, that is the whole array. Its corners are outside the array. No cell is bigger than 3, so [0, 0] is a regional maximum.
The cell [0, 2] has value 1, so its square is rows -1 to 1 and columns 1 to 3. Cut to the array, the cells left are [0, 1], [0, 2] and [1, 2], because [1, 1] is a corner and is left out. The values are 0, 1 and 0, so [0, 2] is a regional maximum.
The cell [1, 0] has value 2, but [0, 0] is in its region and has value 3, so it is not a maximum. The other cells are 0.
The output is [[0, 0], [0, 2]].
Constraints
1 ≤ R ≤ 50, whereR = array.length1 ≤ C ≤ 50, whereC = array[i].lengthfor every row0 ≤ array[i][j] ≤ 1000
More Uber interview questions
- Random Bingo Card GeneratorONSITE INTERVIEW · Seen Dec 2026
- Travelers per Consecutive City TripletONSITE INTERVIEW · Seen Dec 2026
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Simulate Orchard Rot SpreadAirbnb · OAAirbnb interview questions
- Simulate Orchard Rot SpreadCapital One · OACapital One interview questions
- Rotate Matrix Regions Around the DiagonalsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Rotate Matrix Regions Around the DiagonalsHudson River Trading · OAHudson River Trading interview questions
- Robot Navigation Around LasersRoblox · OARoblox interview questions
- Leaves Remaining After Wind GustsOpenAI · OAOpenAI interview questions
- Apply a Reversi MoveAmazon · OA
- Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
- Count Straight-Line Word Occurrences in a GridOptiver · OAOptiver interview questions
- First Completed Mingo LineEpic · OAEpic interview questions