Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given a 2D integer array matrix, return the transpose of matrix.
The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:
Input: matrix = [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]]
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10001 <= m * n <= 105-109 <= matrix[i][j] <= 109Problem summary: Given a 2D integer array matrix, return the transpose of matrix. The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,2,3],[4,5,6],[7,8,9]]
[[1,2,3],[4,5,6]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #867: Transpose Matrix
class Solution {
public int[][] transpose(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[][] ans = new int[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
}
// Accepted solution for LeetCode #867: Transpose Matrix
func transpose(matrix [][]int) [][]int {
m, n := len(matrix), len(matrix[0])
ans := make([][]int, n)
for i := range ans {
ans[i] = make([]int, m)
for j := range ans[i] {
ans[i][j] = matrix[j][i]
}
}
return ans
}
# Accepted solution for LeetCode #867: Transpose Matrix
class Solution:
def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
return list(zip(*matrix))
// Accepted solution for LeetCode #867: Transpose Matrix
struct Solution;
impl Solution {
fn transpose(a: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = a.len();
let m = a[0].len();
let mut res: Vec<Vec<i32>> = vec![vec![0; n]; m];
for i in 0..n {
for j in 0..m {
res[j][i] = a[i][j];
}
}
res
}
}
#[test]
fn test() {
let a: Vec<Vec<i32>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
.iter()
.map(|x| x.to_vec())
.collect();
let b: Vec<Vec<i32>> = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
.iter()
.map(|x| x.to_vec())
.collect();
assert_eq!(Solution::transpose(a), b);
let a: Vec<Vec<i32>> = [[1, 2, 3], [4, 5, 6]].iter().map(|x| x.to_vec()).collect();
let b: Vec<Vec<i32>> = [[1, 4], [2, 5], [3, 6]]
.iter()
.map(|x| x.to_vec())
.collect();
assert_eq!(Solution::transpose(a), b);
}
// Accepted solution for LeetCode #867: Transpose Matrix
function transpose(matrix: number[][]): number[][] {
const [m, n] = [matrix.length, matrix[0].length];
const ans: number[][] = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = matrix[j][i];
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.