Practice
n x n matrix representing an image. The task is to rotate the image by 90 degrees clockwise in-place, without using extra space. Which of the following approaches guarantees an optimal in-place solution with O(1) extra space and O(n²) time complexity?Solution
Step 1: Understand the problem constraints
The problem requires rotating the matrix in-place with O(1) extra space and O(n²) time.Step 2: Evaluate approaches
Brute force uses extra space, DP is unrelated, and transposing then reversing columns rotates counterclockwise, not clockwise. Layer-by-layer four-way swaps correctly rotate clockwise in-place.Final Answer:
Option A -> Option AQuick Check:
Layer-by-layer swaps rotate in-place with correct direction and complexity [OK]
- Confusing transpose+reverse columns with clockwise rotation
- Using extra space when in-place is required
n x n matrix with numbers from 1 to n² in a pattern that starts at the top-left corner and proceeds in a clockwise spiral moving outward. Which approach guarantees filling all cells correctly without revisiting or skipping any cell?Solution
Step 1: Understand the problem constraints
The matrix must be filled in a spiral order, visiting each cell exactly once in a clockwise manner.Step 2: Identify the approach that tracks boundaries and direction
Directional simulation with boundary shrinking ensures that after filling one side, boundaries are updated to avoid revisiting cells, and directions cycle through right, down, left, and up correctly.Final Answer:
Option C -> Option CQuick Check:
Boundary shrinking prevents overwriting and ensures full coverage [OK]
- Assuming greedy row/column fills without boundary checks works
- Confusing DP with spiral traversal
- Ignoring direction cycling leads to incomplete fills
def isValidSudoku(board):
rows = [0] * 9
cols = [0] * 9
boxes = [0] * 9
for i in range(9):
for j in range(9):
if board[i][j] == '.':
continue
val = ord(board[i][j]) - ord('1')
mask = 1 << val
box_index = (i // 3) * 3 + (j // 3)
if (rows[i] & mask) or (cols[j] & mask) or (boxes[box_index] & mask):
return false
rows[i] |= mask
cols[j] |= mask
boxes[box_index] |= mask
return true
board = [
['5','3','.','.','7','.','.','.','.'],
['6','.','.','1','9','5','.','.','.'],
['.','9','8','.','.','.','.','6','.'],
['8','.','.','.','6','.','.','.','3'],
['4','.','.','8','.','3','.','.','1'],
['7','.','.','.','2','.','.','.','6'],
['.','6','.','.','.','.','2','8','.'],
['.','.','.','4','1','9','.','.','5'],
['.','.','.','.','8','.','.','7','9']
]
print(isValidSudoku(board))
What is the output of this code?Solution
Step 1: Trace first few iterations
All digits are unique in their rows, columns, and boxes; no mask conflicts occur.Step 2: Confirm no duplicates found
Since no duplicates are detected, the function completes and returns true.Final Answer:
Option A -> Option AQuick Check:
Standard valid Sudoku board returns true [OK]
- Misreading box index calculation
- Assuming '.' causes errors
- Confusing return values
Solution
Step 1: Identify loops and operations
Outer loops iterate over all m*n cells. For each cell, 8 neighbors are checked in constant time.Step 2: Calculate total operations
Total operations = m*n*8 = O(m*n), since 8 is constant.Final Answer:
Option D -> Option DQuick Check:
Neighbor checks are constant per cell -> linear in board size [OK]
- Mistaking neighbor checks as nested loops over entire board
- Counting neighbor checks twice incorrectly
- Confusing linear scan with quadratic complexity
def isValidSudoku(board):
rows = [0] * 9
cols = [0] * 9
boxes = [0] * 9
for i in range(9):
for j in range(9):
if board[i][j] == '.':
continue
val = ord(board[i][j]) - ord('1')
mask = 1 << val
box_index = i // 3 + (j // 3) * 3
if (rows[i] & mask) or (cols[j] & mask) or (boxes[box_index] & mask):
return false
rows[i] |= mask
cols[j] |= mask
boxes[box_index] |= mask
return true
What is the bug in this code?Solution
Step 1: Analyze box_index calculation
The code uses box_index = i // 3 + (j // 3) * 3, which swaps row and column grouping.Step 2: Correct formula for box_index
The correct formula is (i // 3) * 3 + (j // 3) to map 3x3 boxes properly.Final Answer:
Option D -> Option DQuick Check:
Incorrect box_index causes missed duplicates in boxes [OK]
- Swapping row and column in box index
- Misusing bitmask shifts
- Including '.' in checks
