Practice
1. What is the space complexity of the optimal in-place layer-by-layer rotation algorithm for an
n x n matrix, and why might some candidates mistakenly think it is higher?medium
Solution
Step 1: Identify space usage
The algorithm uses only a few variables (indices, temp storage) regardless of matrix size.Step 2: Understand common misconceptions
Some think space is O(n) or more due to confusion with extra matrix or recursion, but no extra arrays or recursion are used.Final Answer:
Option D -> Option DQuick Check:
Only constant extra variables used, so space is O(1) [OK]
Hint: In-place means constant extra space regardless of input size [OK]
Common Mistakes:
- Confusing time complexity with space
- Assuming recursion or extra arrays are used
2. The following code attempts to rotate an
n x n matrix by 90 degrees clockwise in-place. Identify the line containing the subtle bug that causes incorrect rotation.medium
Solution
Step 1: Analyze the last assignment
The last line should assign the saved top value to matrix[i][last], but it assigns matrix[i][last] to itself.Step 2: Identify the fix
Replacematrix[i][last] = matrix[i][last]withmatrix[i][last] = topto complete the four-way swap correctly.Final Answer:
Option C -> Option CQuick Check:
Incorrect assignment leaves matrix partially unrotated [OK]
Hint: Last assignment must restore saved top element to complete rotation [OK]
Common Mistakes:
- Forgetting to assign saved top value
- Swapping elements twice causing corruption
3. What is the time complexity of the optimal in-place boundary shrinking approach to generate an
n x n spiral matrix, and why?medium
Solution
Step 1: Identify the number of cells to fill
The matrix has n² cells, each must be assigned a value exactly once.Step 2: Analyze loops and operations
Each iteration fills one side of the current boundary, and boundaries shrink inward. The total number of assignments sums to n², so time complexity is O(n²).Final Answer:
Option A -> Option AQuick Check:
Every cell visited once -> O(n²) time [OK]
Hint: Each cell visited once -> O(n²) time [OK]
Common Mistakes:
- Confusing boundary shrinking with linear time
- Assuming nested loops multiply to cubic time
- Ignoring that direction cycling is constant time
4. What is the time complexity of the in-place transpose algorithm for an n x n matrix that swaps elements only above the main diagonal?
medium
Solution
Step 1: Identify loops and iterations
The outer loop runs n times, inner loop runs approximately n - i - 1 times, summing to about n(n-1)/2 swaps.Step 2: Simplify complexity
Ignoring constants and lower order terms, time complexity is O(n^2).Final Answer:
Option D -> Option DQuick Check:
Quadratic time due to nested loops over matrix elements [OK]
Hint: Nested loops over n x n matrix -> O(n^2) [OK]
Common Mistakes:
- Confusing O(n^2 / 2) as different from O(n^2)
- Mistaking it for O(n^3) due to nested loops
- Assuming linear time due to skipping diagonal
5. Suppose you need to transpose a non-square matrix (m x n) in-place, but the matrix is stored as a flat array in row-major order. Which approach correctly handles this scenario?
hard
Solution
Step 1: Understand matrix shape constraints
In-place transpose by swapping only works for square matrices because indices map symmetrically.Step 2: For non-square matrices, in-place transpose is not possible
Because the shape changes from m x n to n x m, a new matrix must be created to hold the transposed data.Final Answer:
Option C -> Option CQuick Check:
Non-square matrix transpose requires extra space due to dimension change [OK]
Hint: In-place transpose only works for square matrices [OK]
Common Mistakes:
- Trying to swap elements in-place for non-square matrices
- Assuming upper triangle swaps apply to rectangular matrices
- Ignoring dimension changes after transpose
