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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
There are n people standing in a queue, and they numbered from 0 to n - 1 in left to right order. You are given an array heights of distinct integers where heights[i] represents the height of the ith person.
A person can see another person to their right in the queue if everybody in between is shorter than both of them. More formally, the ith person can see the jth person if i < j and min(heights[i], heights[j]) > max(heights[i+1], heights[i+2], ..., heights[j-1]).
Return an array answer of length n where answer[i] is the number of people the ith person can see to their right in the queue.
Example 1:
Input: heights = [10,6,8,5,11,9] Output: [3,1,2,1,1,0] Explanation: Person 0 can see person 1, 2, and 4. Person 1 can see person 2. Person 2 can see person 3 and 4. Person 3 can see person 4. Person 4 can see person 5. Person 5 can see no one since nobody is to the right of them.
Example 2:
Input: heights = [5,1,2,3,10] Output: [4,1,1,1,0]
Constraints:
n == heights.length1 <= n <= 1051 <= heights[i] <= 105heights are unique.Problem summary: There are n people standing in a queue, and they numbered from 0 to n - 1 in left to right order. You are given an array heights of distinct integers where heights[i] represents the height of the ith person. A person can see another person to their right in the queue if everybody in between is shorter than both of them. More formally, the ith person can see the jth person if i < j and min(heights[i], heights[j]) > max(heights[i+1], heights[i+2], ..., heights[j-1]). Return an array answer of length n where answer[i] is the number of people the ith person can see to their right in the queue.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Stack
[10,6,8,5,11,9]
[5,1,2,3,10]
buildings-with-an-ocean-view)sum-of-subarray-ranges)sum-of-total-strength-of-wizards)number-of-people-that-can-be-seen-in-a-grid)find-building-where-alice-and-bob-can-meet)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1944: Number of Visible People in a Queue
class Solution {
public int[] canSeePersonsCount(int[] heights) {
int n = heights.length;
int[] ans = new int[n];
Deque<Integer> stk = new ArrayDeque<>();
for (int i = n - 1; i >= 0; --i) {
while (!stk.isEmpty() && stk.peek() < heights[i]) {
stk.pop();
++ans[i];
}
if (!stk.isEmpty()) {
++ans[i];
}
stk.push(heights[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #1944: Number of Visible People in a Queue
func canSeePersonsCount(heights []int) []int {
n := len(heights)
ans := make([]int, n)
stk := []int{}
for i := n - 1; i >= 0; i-- {
for len(stk) > 0 && stk[len(stk)-1] < heights[i] {
ans[i]++
stk = stk[:len(stk)-1]
}
if len(stk) > 0 {
ans[i]++
}
stk = append(stk, heights[i])
}
return ans
}
# Accepted solution for LeetCode #1944: Number of Visible People in a Queue
class Solution:
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
n = len(heights)
ans = [0] * n
stk = []
for i in range(n - 1, -1, -1):
while stk and stk[-1] < heights[i]:
ans[i] += 1
stk.pop()
if stk:
ans[i] += 1
stk.append(heights[i])
return ans
// Accepted solution for LeetCode #1944: Number of Visible People in a Queue
impl Solution {
pub fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32> {
let n = heights.len();
let mut ans = vec![0; n];
let mut stack = Vec::new();
for i in (0..n).rev() {
while !stack.is_empty() {
ans[i] += 1;
if heights[i] <= heights[*stack.last().unwrap()] {
break;
}
stack.pop();
}
stack.push(i);
}
ans
}
}
// Accepted solution for LeetCode #1944: Number of Visible People in a Queue
function canSeePersonsCount(heights: number[]): number[] {
const n = heights.length;
const ans: number[] = new Array(n).fill(0);
const stk: number[] = [];
for (let i = n - 1; ~i; --i) {
while (stk.length && stk.at(-1) < heights[i]) {
++ans[i];
stk.pop();
}
if (stk.length) {
++ans[i];
}
stk.push(heights[i]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan left (or right) to find the next greater/smaller element. The inner scan can visit up to n elements per outer iteration, giving O(n²) total comparisons. No extra space needed beyond loop variables.
Each element is pushed onto the stack at most once and popped at most once, giving 2n total operations = O(n). The stack itself holds at most n elements in the worst case. The key insight: amortized O(1) per element despite the inner while-loop.
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.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.