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.
You are given a 0-indexed array heights of positive integers, where heights[i] represents the height of the ith building.
If a person is in building i, they can move to any other building j if and only if i < j and heights[i] < heights[j].
You are also given another array queries where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi.
Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the ith query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1.
Example 1:
Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] Output: [2,5,-1,5,2] Explanation: In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2]. In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. In the third query, Alice cannot meet Bob since Alice cannot move to any other building. In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5]. In the fifth query, Alice and Bob are already in the same building. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Example 2:
Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] Output: [7,6,-1,4,6] Explanation: In the first query, Alice can directly move to Bob's building since heights[0] < heights[7]. In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. In the third query, Alice cannot meet Bob since Bob cannot move to any other building. In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4]. In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Constraints:
1 <= heights.length <= 5 * 1041 <= heights[i] <= 1091 <= queries.length <= 5 * 104queries[i] = [ai, bi]0 <= ai, bi <= heights.length - 1Problem summary: You are given a 0-indexed array heights of positive integers, where heights[i] represents the height of the ith building. If a person is in building i, they can move to any other building j if and only if i < j and heights[i] < heights[j]. You are also given another array queries where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi. Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the ith query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Stack · Segment Tree
[6,4,8,5,2,7] [[0,1],[0,3],[2,4],[3,4],[2,2]]
[5,3,8,2,6,1,4,6] [[0,7],[3,5],[5,2],[3,0],[1,6]]
number-of-visible-people-in-a-queue)furthest-building-you-can-reach)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2940: Find Building Where Alice and Bob Can Meet
class BinaryIndexedTree {
private final int inf = 1 << 30;
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
Arrays.fill(c, inf);
}
public void update(int x, int v) {
while (x <= n) {
c[x] = Math.min(c[x], v);
x += x & -x;
}
}
public int query(int x) {
int mi = inf;
while (x > 0) {
mi = Math.min(mi, c[x]);
x -= x & -x;
}
return mi == inf ? -1 : mi;
}
}
class Solution {
public int[] leftmostBuildingQueries(int[] heights, int[][] queries) {
int n = heights.length;
int m = queries.length;
for (int i = 0; i < m; ++i) {
if (queries[i][0] > queries[i][1]) {
queries[i] = new int[] {queries[i][1], queries[i][0]};
}
}
Integer[] idx = new Integer[m];
for (int i = 0; i < m; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i, j) -> queries[j][1] - queries[i][1]);
int[] s = heights.clone();
Arrays.sort(s);
int[] ans = new int[m];
int j = n - 1;
BinaryIndexedTree tree = new BinaryIndexedTree(n);
for (int i : idx) {
int l = queries[i][0], r = queries[i][1];
while (j > r) {
int k = n - Arrays.binarySearch(s, heights[j]) + 1;
tree.update(k, j);
--j;
}
if (l == r || heights[l] < heights[r]) {
ans[i] = r;
} else {
int k = n - Arrays.binarySearch(s, heights[l]);
ans[i] = tree.query(k);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2940: Find Building Where Alice and Bob Can Meet
const inf int = 1 << 30
type BinaryIndexedTree struct {
n int
c []int
}
func NewBinaryIndexedTree(n int) BinaryIndexedTree {
c := make([]int, n+1)
for i := range c {
c[i] = inf
}
return BinaryIndexedTree{n: n, c: c}
}
func (bit *BinaryIndexedTree) update(x, v int) {
for x <= bit.n {
bit.c[x] = min(bit.c[x], v)
x += x & -x
}
}
func (bit *BinaryIndexedTree) query(x int) int {
mi := inf
for x > 0 {
mi = min(mi, bit.c[x])
x -= x & -x
}
if mi == inf {
return -1
}
return mi
}
func leftmostBuildingQueries(heights []int, queries [][]int) []int {
n, m := len(heights), len(queries)
for _, q := range queries {
if q[0] > q[1] {
q[0], q[1] = q[1], q[0]
}
}
idx := make([]int, m)
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return queries[idx[j]][1] < queries[idx[i]][1] })
s := make([]int, n)
copy(s, heights)
sort.Ints(s)
ans := make([]int, m)
tree := NewBinaryIndexedTree(n)
j := n - 1
for _, i := range idx {
l, r := queries[i][0], queries[i][1]
for ; j > r; j-- {
k := n - sort.SearchInts(s, heights[j]) + 1
tree.update(k, j)
}
if l == r || heights[l] < heights[r] {
ans[i] = r
} else {
k := n - sort.SearchInts(s, heights[l])
ans[i] = tree.query(k)
}
}
return ans
}
# Accepted solution for LeetCode #2940: Find Building Where Alice and Bob Can Meet
class BinaryIndexedTree:
__slots__ = ["n", "c"]
def __init__(self, n: int):
self.n = n
self.c = [inf] * (n + 1)
def update(self, x: int, v: int):
while x <= self.n:
self.c[x] = min(self.c[x], v)
x += x & -x
def query(self, x: int) -> int:
mi = inf
while x:
mi = min(mi, self.c[x])
x -= x & -x
return -1 if mi == inf else mi
class Solution:
def leftmostBuildingQueries(
self, heights: List[int], queries: List[List[int]]
) -> List[int]:
n, m = len(heights), len(queries)
for i in range(m):
queries[i] = [min(queries[i]), max(queries[i])]
j = n - 1
s = sorted(set(heights))
ans = [-1] * m
tree = BinaryIndexedTree(n)
for i in sorted(range(m), key=lambda i: -queries[i][1]):
l, r = queries[i]
while j > r:
k = n - bisect_left(s, heights[j]) + 1
tree.update(k, j)
j -= 1
if l == r or heights[l] < heights[r]:
ans[i] = r
else:
k = n - bisect_left(s, heights[l])
ans[i] = tree.query(k)
return ans
// Accepted solution for LeetCode #2940: Find Building Where Alice and Bob Can Meet
// 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 #2940: Find Building Where Alice and Bob Can Meet
// class BinaryIndexedTree {
// private final int inf = 1 << 30;
// private int n;
// private int[] c;
//
// public BinaryIndexedTree(int n) {
// this.n = n;
// c = new int[n + 1];
// Arrays.fill(c, inf);
// }
//
// public void update(int x, int v) {
// while (x <= n) {
// c[x] = Math.min(c[x], v);
// x += x & -x;
// }
// }
//
// public int query(int x) {
// int mi = inf;
// while (x > 0) {
// mi = Math.min(mi, c[x]);
// x -= x & -x;
// }
// return mi == inf ? -1 : mi;
// }
// }
//
// class Solution {
// public int[] leftmostBuildingQueries(int[] heights, int[][] queries) {
// int n = heights.length;
// int m = queries.length;
// for (int i = 0; i < m; ++i) {
// if (queries[i][0] > queries[i][1]) {
// queries[i] = new int[] {queries[i][1], queries[i][0]};
// }
// }
// Integer[] idx = new Integer[m];
// for (int i = 0; i < m; ++i) {
// idx[i] = i;
// }
// Arrays.sort(idx, (i, j) -> queries[j][1] - queries[i][1]);
// int[] s = heights.clone();
// Arrays.sort(s);
// int[] ans = new int[m];
// int j = n - 1;
// BinaryIndexedTree tree = new BinaryIndexedTree(n);
// for (int i : idx) {
// int l = queries[i][0], r = queries[i][1];
// while (j > r) {
// int k = n - Arrays.binarySearch(s, heights[j]) + 1;
// tree.update(k, j);
// --j;
// }
// if (l == r || heights[l] < heights[r]) {
// ans[i] = r;
// } else {
// int k = n - Arrays.binarySearch(s, heights[l]);
// ans[i] = tree.query(k);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2940: Find Building Where Alice and Bob Can Meet
class BinaryIndexedTree {
private n: number;
private c: number[];
private inf: number = 1 << 30;
constructor(n: number) {
this.n = n;
this.c = Array(n + 1).fill(this.inf);
}
update(x: number, v: number): void {
while (x <= this.n) {
this.c[x] = Math.min(this.c[x], v);
x += x & -x;
}
}
query(x: number): number {
let mi = this.inf;
while (x > 0) {
mi = Math.min(mi, this.c[x]);
x -= x & -x;
}
return mi === this.inf ? -1 : mi;
}
}
function leftmostBuildingQueries(heights: number[], queries: number[][]): number[] {
const n = heights.length;
const m = queries.length;
for (const q of queries) {
if (q[0] > q[1]) {
[q[0], q[1]] = [q[1], q[0]];
}
}
const idx: number[] = Array(m)
.fill(0)
.map((_, i) => i);
idx.sort((i, j) => queries[j][1] - queries[i][1]);
const tree = new BinaryIndexedTree(n);
const ans: number[] = Array(m).fill(-1);
const s = [...heights];
s.sort((a, b) => a - b);
const search = (x: number) => {
let [l, r] = [0, n];
while (l < r) {
const mid = (l + r) >> 1;
if (s[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
let j = n - 1;
for (const i of idx) {
const [l, r] = queries[i];
while (j > r) {
const k = n - search(heights[j]) + 1;
tree.update(k, j);
--j;
}
if (l === r || heights[l] < heights[r]) {
ans[i] = r;
} else {
const k = n - search(heights[l]);
ans[i] = tree.query(k);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
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.