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.
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules:
P=[1,2,3,...,m].i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].Return an array containing the result for the given queries.
Example 1:
Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1].
Example 2:
Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0]
Example 3:
Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]
Constraints:
1 <= m <= 10^31 <= queries.length <= m1 <= queries[i] <= mProblem summary: Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Segment Tree
[3,1,2,1] 5
[4,1,2,2] 4
[7,5,5,8,3] 8
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1409: Queries on a Permutation With Key
class Solution {
public int[] processQueries(int[] queries, int m) {
List<Integer> p = new LinkedList<>();
for (int i = 1; i <= m; ++i) {
p.add(i);
}
int[] ans = new int[queries.length];
int i = 0;
for (int v : queries) {
int j = p.indexOf(v);
ans[i++] = j;
p.remove(j);
p.add(0, v);
}
return ans;
}
}
// Accepted solution for LeetCode #1409: Queries on a Permutation With Key
func processQueries(queries []int, m int) []int {
p := make([]int, m)
for i := range p {
p[i] = i + 1
}
ans := []int{}
for _, v := range queries {
j := 0
for i := range p {
if p[i] == v {
j = i
break
}
}
ans = append(ans, j)
p = append(p[:j], p[j+1:]...)
p = append([]int{v}, p...)
}
return ans
}
# Accepted solution for LeetCode #1409: Queries on a Permutation With Key
class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
p = list(range(1, m + 1))
ans = []
for v in queries:
j = p.index(v)
ans.append(j)
p.pop(j)
p.insert(0, v)
return ans
// Accepted solution for LeetCode #1409: Queries on a Permutation With Key
struct Solution;
impl Solution {
fn process_queries(queries: Vec<i32>, m: i32) -> Vec<i32> {
let mut v: Vec<i32> = (1..=m).collect();
let mut res = vec![];
for q in queries {
let p = v.iter().position(|&x| x == q).unwrap();
v.remove(p);
v.insert(0, q);
res.push(p as i32);
}
res
}
}
#[test]
fn test() {
let queries = vec![3, 1, 2, 1];
let m = 5;
let res = vec![2, 1, 2, 1];
assert_eq!(Solution::process_queries(queries, m), res);
let queries = vec![4, 1, 2, 2];
let m = 4;
let res = vec![3, 1, 2, 0];
assert_eq!(Solution::process_queries(queries, m), res);
let queries = vec![7, 5, 5, 8, 3];
let m = 8;
let res = vec![6, 5, 0, 7, 5];
assert_eq!(Solution::process_queries(queries, m), res);
}
// Accepted solution for LeetCode #1409: Queries on a Permutation With Key
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1409: Queries on a Permutation With Key
// class Solution {
// public int[] processQueries(int[] queries, int m) {
// List<Integer> p = new LinkedList<>();
// for (int i = 1; i <= m; ++i) {
// p.add(i);
// }
// int[] ans = new int[queries.length];
// int i = 0;
// for (int v : queries) {
// int j = p.indexOf(v);
// ans[i++] = j;
// p.remove(j);
// p.add(0, v);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
For each of q queries, scan the entire range to compute the aggregate (sum, min, max). Each range scan takes up to O(n) for a full-array query. With q queries: O(n × q) total. Point updates are O(1) but queries dominate.
Building the tree is O(n). Each query or update traverses O(log n) nodes (tree height). For q queries: O(n + q log n) total. Space is O(4n) ≈ O(n) for the tree array. Lazy propagation adds O(1) per node for deferred updates.
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.