Practice
def countNegatives(grid):
m, n = len(grid), len(grid[0])
count = 0
row, col = 0, n - 1
while row < m and col >= 0:
if grid[row][col] < 0:
count += m - row
col -= 1
else:
row += 1
return count
Solution
Step 1: Trace first iteration
Start at (0,2): grid[0][2] = -1 < 0, count += 3 - 0 = 3, col = 1Step 2: Trace subsequent iterations
At (0,1): 1 >= 0, row = 1; At (1,1): 0 >= 0, row = 2; At (2,1): -1 < 0, count += 3 - 2 = 1, col = 0; At (2,0): 1 >= 0, row = 3 (end)Final Answer:
Option A -> Option AQuick Check:
Counted negatives: 3 + 1 + 2 (from last col) = 6 [OK]
- Off-by-one in counting rows below
- Incrementing row or col incorrectly
- Misreading matrix indices
from typing import List
def luckyNumbers(matrix: List[List[int]]) -> List[int]:
m, n = len(matrix), len(matrix[0])
row_mins = [min(row) for row in matrix]
col_maxs = [max(matrix[i][j] for i in range(m)) for j in range(n)]
return [matrix[i][j] for i in range(m) for j in range(n) if matrix[i][j] == row_mins[i] == col_maxs[j]]
mat = [[3,7,8],[9,11,13],[15,16,17]]
print(luckyNumbers(mat))
What is the output of this code?Solution
Step 1: Compute row minimums
Row 0 min = 3, Row 1 min = 9, Row 2 min = 15.Step 2: Compute column maximums
Col 0 max = max(3,9,15) = 15, Col 1 max = max(7,11,16) = 16, Col 2 max = max(8,13,17) = 17.Step 3: Find elements equal to both row min and column max
Check each element: - (2,0) = 15 equals row 2 min and col 0 max -> lucky number. No other elements satisfy both conditions.Final Answer:
Option A -> Option AQuick Check:
Only 15 matches both conditions -> output [15] [OK]
- Confusing min and max
- Off-by-one indexing
- Returning all row mins or col maxs
Solution
Step 1: Understand the hourglass pattern
The hourglass consists of 7 fixed positions in a 3x3 block, not the entire 3x3 matrix.Step 2: Recognize the sliding window optimization
Sliding window updates the sum by removing left column elements and adding right column elements, achieving O(n*m) time and O(1) space.Final Answer:
Option D -> Option DQuick Check:
Sliding window updates sums efficiently without recomputing full sums [OK]
- Confusing hourglass with full 3x3 submatrix sum
- Thinking DP is needed
- Assuming greedy works for max sum pattern
spiralOrder([[1,2],[3,4]])?Solution
Step 1: Trace first iteration
Top=0, Bottom=1, Left=0, Right=1. Append top row: [1, 2]. Increment top to 1.Step 2: Trace right column and subsequent steps
Append right column from row 1 to 1: [4]. Decrement right to 0. Since top <= bottom, append bottom row reversed: [3]. Decrement bottom to 0. Left <= right, but now left=0, right=0, top=1, bottom=0 so loop ends.Final Answer:
Option B -> Option BQuick Check:
Output matches clockwise spiral traversal [OK]
- Appending rows or columns in wrong order
- Off-by-one errors in loops
- Not updating boundaries correctly
Solution
Step 1: Identify missing boundary check
The code traverses the bottom row without verifying if top <= bottom, which can cause duplicates or index errors when boundaries cross.Step 2: Confirm other lines are correct
Top increment, right decrement, and left column traversal have proper boundary checks or are logically correct.Final Answer:
Option A -> Option AQuick Check:
Missing boundary check before bottom row traversal causes subtle bugs [OK]
- Forgetting boundary checks before bottom or left traversal
- Incorrectly updating boundaries causing infinite loops
- Misplaced increments/decrements
