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.
A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.
You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).
Return the number of indices where heights[i] != expected[i].
Example 1:
Input: heights = [1,1,4,2,1,3] Output: 3 Explanation: heights: [1,1,4,2,1,3] expected: [1,1,1,2,3,4] Indices 2, 4, and 5 do not match.
Example 2:
Input: heights = [5,1,2,3,4] Output: 5 Explanation: heights: [5,1,2,3,4] expected: [1,2,3,4,5] All indices do not match.
Example 3:
Input: heights = [1,2,3,4,5] Output: 0 Explanation: heights: [1,2,3,4,5] expected: [1,2,3,4,5] All indices match.
Constraints:
1 <= heights.length <= 1001 <= heights[i] <= 100Problem summary: A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line. You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed). Return the number of indices where heights[i] != expected[i].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,1,4,2,1,3]
[5,1,2,3,4]
[1,2,3,4,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1051: Height Checker
class Solution {
public int heightChecker(int[] heights) {
int[] expected = heights.clone();
Arrays.sort(expected);
int ans = 0;
for (int i = 0; i < heights.length; ++i) {
if (heights[i] != expected[i]) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1051: Height Checker
func heightChecker(heights []int) (ans int) {
expected := slices.Clone(heights)
sort.Ints(expected)
for i, v := range heights {
if v != expected[i] {
ans++
}
}
return
}
# Accepted solution for LeetCode #1051: Height Checker
class Solution:
def heightChecker(self, heights: List[int]) -> int:
expected = sorted(heights)
return sum(a != b for a, b in zip(heights, expected))
// Accepted solution for LeetCode #1051: Height Checker
struct Solution;
impl Solution {
fn height_checker(heights: Vec<i32>) -> i32 {
let mut sorted = heights.to_vec();
sorted.sort_unstable();
heights
.iter()
.zip(sorted.iter())
.fold(0, |sum, (a, b)| if a != b { sum + 1 } else { sum })
}
}
#[test]
fn test() {
let heights = vec![1, 1, 4, 2, 1, 3];
assert_eq!(Solution::height_checker(heights), 3);
}
// Accepted solution for LeetCode #1051: Height Checker
function heightChecker(heights: number[]): number {
const expected = [...heights].sort((a, b) => a - b);
return heights.reduce((acc, cur, i) => acc + (cur !== expected[i] ? 1 : 0), 0);
}
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.