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.
There are n teams numbered from 0 to n - 1 in a tournament.
Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n - 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i.
Team a will be the champion of the tournament if there is no team b that is stronger than team a.
Return the team that will be the champion of the tournament.
Example 1:
Input: grid = [[0,1],[0,0]] Output: 0 Explanation: There are two teams in this tournament. grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.
Example 2:
Input: grid = [[0,0,1],[1,0,1],[0,0,0]] Output: 1 Explanation: There are three teams in this tournament. grid[1][0] == 1 means that team 1 is stronger than team 0. grid[1][2] == 1 means that team 1 is stronger than team 2. So team 1 will be the champion.
Constraints:
n == grid.lengthn == grid[i].length2 <= n <= 100grid[i][j] is either 0 or 1.i grid[i][i] is 0.i, j that i != j, grid[i][j] != grid[j][i].a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.Problem summary: There are n teams numbered from 0 to n - 1 in a tournament. Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n - 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i. Team a will be the champion of the tournament if there is no team b that is stronger than team a. Return the team that will be the champion of the tournament.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[0,1],[0,0]]
[[0,0,1],[1,0,1],[0,0,0]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2923: Find Champion I
class Solution {
public int findChampion(int[][] grid) {
int n = grid.length;
for (int i = 0;; ++i) {
int cnt = 0;
for (int j = 0; j < n; ++j) {
if (i != j && grid[i][j] == 1) {
++cnt;
}
}
if (cnt == n - 1) {
return i;
}
}
}
}
// Accepted solution for LeetCode #2923: Find Champion I
func findChampion(grid [][]int) int {
n := len(grid)
for i := 0; ; i++ {
cnt := 0
for j, x := range grid[i] {
if i != j && x == 1 {
cnt++
}
}
if cnt == n-1 {
return i
}
}
}
# Accepted solution for LeetCode #2923: Find Champion I
class Solution:
def findChampion(self, grid: List[List[int]]) -> int:
for i, row in enumerate(grid):
if all(x == 1 for j, x in enumerate(row) if i != j):
return i
// Accepted solution for LeetCode #2923: Find Champion I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2923: Find Champion I
// class Solution {
// public int findChampion(int[][] grid) {
// int n = grid.length;
// for (int i = 0;; ++i) {
// int cnt = 0;
// for (int j = 0; j < n; ++j) {
// if (i != j && grid[i][j] == 1) {
// ++cnt;
// }
// }
// if (cnt == n - 1) {
// return i;
// }
// }
// }
// }
// Accepted solution for LeetCode #2923: Find Champion I
function findChampion(grid: number[][]): number {
for (let i = 0, n = grid.length; ; ++i) {
let cnt = 0;
for (let j = 0; j < n; ++j) {
if (i !== j && grid[i][j] === 1) {
++cnt;
}
}
if (cnt === n - 1) {
return i;
}
}
}
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.