Question 281
A smart building uses a grid of temperature sensors installed in the ceiling of a large warehouse floor. The sensor readings are stored in a 2D integer array where each element represents the temperature in degrees at that sensor's location. The facilities team needs analysis tools to monitor heating patterns and identify problem areas.
PART A
Write the method public int[] rowMaximums(int[][] temps) that returns a new 1D array where each element is the maximum temperature value found in the corresponding row of temps.
For example, if temps is {{72, 75, 68}, {80, 77, 82}, {69, 71, 70}}, the method returns {75, 82, 71}.
public int[] rowMaximums(int[][] temps) {
}PART B
Write the method public boolean hasHotspot(int[][] temps, int threshold) that returns true if there exists any \(2\times2\) sub-grid (four adjacent cells forming a square) where all four values are strictly greater than threshold. The method returns false otherwise.
For example, if temps is {{70, 85, 90}, {60, 88, 92}, {75, 80, 78}} and threshold is 80, the method returns true because the \(2\times2\) sub-grid at rows 0–1 and columns 1–2 contains {85, 90, 88, 92}, all greater than 80.
public boolean hasHotspot(int[][] temps, int threshold) {
}Main Solution
PART A — rowMaximums
The method must iterate over every row of the 2D array, find the maximum value in that row, and store it in a result array at the corresponding index.
public int[] rowMaximums(int[][] temps) {
int[] result = new int[temps.length];
for (int r = 0; r < temps.length; r++) {
int max = temps[r][0]; // start with first element of the row
for (int c = 1; c < temps[r].length; c++) {
if (temps[r][c] > max) {
max = temps[r][c];
}
}
result[r] = max;
}
return result;
}Trace with the given example:
- Row 0:
{72, 75, 68}→ max = 75 - Row 1:
{80, 77, 82}→ max = 82 - Row 2:
{69, 71, 70}→ max = 71
Returns {75, 82, 71} ✓
PART B — hasHotspot
The method must check every possible 2×2 sub-grid. A 2×2 sub-grid is defined by its top-left corner at (r, c); its four cells are (r,c), (r,c+1), (r+1,c), and (r+1,c+1). The top-left corner can range from row 0 to temps.length - 2 and column 0 to temps[r].length - 2.
public boolean hasHotspot(int[][] temps, int threshold) {
for (int r = 0; r < temps.length - 1; r++) {
for (int c = 0; c < temps[r].length - 1; c++) {
if (temps[r][c] > threshold &&
temps[r][c+1] > threshold &&
temps[r+1][c] > threshold &&
temps[r+1][c+1] > threshold) {
return true;
}
}
}
return false;
}Trace with the given example (temps = {{70,85,90},{60,88,92},{75,80,78}}, threshold = 80):
- Sub-grid at (0,0): 70, 85, 60, 88 — 70 is not > 80 → skip
- Sub-grid at (0,1): 85, 90, 88, 92 — all > 80 → return
true✓
Teacher Explanation
Part A tests the ability to traverse a 2D array row by row and apply a running-maximum algorithm, then store results in a 1D array. Key pitfalls: initializing max to Integer.MIN_VALUE or 0 instead of the first element of the row (both work, but using the first element is cleaner and avoids edge cases with all-negative temperatures).
Part B tests nested-loop boundary control. The outer loop must stop at temps.length - 1 (not temps.length) and the inner loop at temps[r].length - 1 to avoid an ArrayIndexOutOfBoundsException. The condition must use strict greater-than (>), not >=, as specified by "strictly greater than."
Education Coach Advice
AP Exam Strategy:
- For 2D array traversal problems, always write out the loop bounds explicitly and double-check them against the array dimensions before moving on.
- For "find the maximum" problems, initialize your running max to the first element of the collection, not to 0 or a magic number.
- For sub-grid problems, remember the top-left corner of an \(n \times n\) sub-grid can only go up to
length - nin each dimension. - Trace through the provided example by hand after writing your code — the AP graders reward correct logic even if syntax is slightly off, but a wrong trace means wrong logic.
- Use
return trueas soon as you find a hotspot (early exit); onlyreturn falseafter all sub-grids have been checked.
Traps
Common errors and traps:
- Off-by-one in Part B: Using
r < temps.lengthinstead ofr < temps.length - 1causes anArrayIndexOutOfBoundsExceptionwhen accessingtemps[r+1]. - Wrong comparison operator in Part B: Using
>=instead of>violates the "strictly greater than" requirement and will fail test cases where a cell equals the threshold exactly. - Initializing max to 0 in Part A: If all temperatures in a row are negative, initializing
max = 0would return an incorrect result. Always initialize to the first element of the row or toInteger.MIN_VALUE. - Returning the wrong array length in Part A: The result array must have length
temps.length(number of rows), nottemps[0].length(number of columns).

