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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array arr of positive integers. You are also given the array queries where queries[i] = [lefti, righti].
For each query i compute the XOR of elements from lefti to righti (that is, arr[lefti] XOR arr[lefti + 1] XOR ... XOR arr[righti] ).
Return an array answer where answer[i] is the answer to the ith query.
Example 1:
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] Output: [2,7,14,8] Explanation: The binary representation of the elements in the array are: 1 = 0001 3 = 0011 4 = 0100 8 = 1000 The XOR values for queries are: [0,1] = 1 xor 3 = 2 [1,2] = 3 xor 4 = 7 [0,3] = 1 xor 3 xor 4 xor 8 = 14 [3,3] = 8
Example 2:
Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]] Output: [8,0,4,4]
Constraints:
1 <= arr.length, queries.length <= 3 * 1041 <= arr[i] <= 109queries[i].length == 20 <= lefti <= righti < arr.lengthProblem summary: You are given an array arr of positive integers. You are also given the array queries where queries[i] = [lefti, righti]. For each query i compute the XOR of elements from lefti to righti (that is, arr[lefti] XOR arr[lefti + 1] XOR ... XOR arr[righti] ). Return an array answer where answer[i] is the answer to the ith query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[1,3,4,8] [[0,1],[1,2],[0,3],[3,3]]
[4,8,2,10] [[2,3],[1,3],[0,0],[0,3]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1310: XOR Queries of a Subarray
class Solution {
public int[] xorQueries(int[] arr, int[][] queries) {
int n = arr.length;
int[] s = new int[n + 1];
for (int i = 1; i <= n; ++i) {
s[i] = s[i - 1] ^ arr[i - 1];
}
int m = queries.length;
int[] ans = new int[m];
for (int i = 0; i < m; ++i) {
int l = queries[i][0], r = queries[i][1];
ans[i] = s[r + 1] ^ s[l];
}
return ans;
}
}
// Accepted solution for LeetCode #1310: XOR Queries of a Subarray
func xorQueries(arr []int, queries [][]int) (ans []int) {
n := len(arr)
s := make([]int, n+1)
for i, x := range arr {
s[i+1] = s[i] ^ x
}
for _, q := range queries {
l, r := q[0], q[1]
ans = append(ans, s[r+1]^s[l])
}
return
}
# Accepted solution for LeetCode #1310: XOR Queries of a Subarray
class Solution:
def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
s = list(accumulate(arr, xor, initial=0))
return [s[r + 1] ^ s[l] for l, r in queries]
// Accepted solution for LeetCode #1310: XOR Queries of a Subarray
struct Solution;
impl Solution {
fn xor_queries(mut arr: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
let n = arr.len();
for i in 1..n {
arr[i] ^= arr[i - 1];
}
let mut res = vec![];
for query in queries {
let l = query[0] as usize;
let r = query[1] as usize;
let x = if l > 0 { arr[r] ^ arr[l - 1] } else { arr[r] };
res.push(x);
}
res
}
}
#[test]
fn test() {
let arr = vec![1, 3, 4, 8];
let queries = vec_vec_i32![[0, 1], [1, 2], [0, 3], [3, 3]];
let res = vec![2, 7, 14, 8];
assert_eq!(Solution::xor_queries(arr, queries), res);
let arr = vec![4, 8, 2, 10];
let queries = vec_vec_i32![[2, 3], [1, 3], [0, 0], [0, 3]];
let res = vec![8, 0, 4, 4];
assert_eq!(Solution::xor_queries(arr, queries), res);
}
// Accepted solution for LeetCode #1310: XOR Queries of a Subarray
function xorQueries(arr: number[], queries: number[][]): number[] {
const n = arr.length;
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] ^ arr[i];
}
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
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.