Find a Bounded Subarray and Largest Square
UberSoftware Engineer · OnsiteLast updated
This question evaluates array manipulation and algorithmic problem-solving skills across two tasks: enforcing bounded value ranges within contiguous subarrays and identifying largest uniform square regions in binary matrices, exercising concepts such as range-tracking, efficient data structures, and dynamic programming.
Implement the following two independent coding tasks.
Task A: Longest bounded-range subarray
Given an integer array nums and an integer limit, return the length of the longest contiguous subarray such that the difference between the maximum and minimum values inside that subarray is at most limit.
Example:
- Input:
nums = [8, 2, 4, 7],limit = 4 - Output:
2 - Explanation: The longest valid subarray is
[2, 4]or[4, 7].
Constraints:
1 <= nums.length <= 100000-1000000000 <= nums[i] <= 10000000000 <= limit <= 1000000000
Task B: Largest square area in a binary grid
Given an m x n binary matrix grid containing only 0s and 1s, return the area of the largest square containing only 1s.
Example:
- Input:
grid = [[1,0,1,0,0],[1,0,1,1,1],[1,1,1,1,1],[1,0,0,1,0]] - Output:
4 - Explanation: The largest all-
1square has side length2, so its area is4.
Constraints:
1 <= m, n <= 300grid[i][j]is either0or1.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.