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.
You are given an integer array order of length n and an integer array friends.
order contains every integer from 1 to n exactly once, representing the IDs of the participants of a race in their finishing order.friends contains the IDs of your friends in the race sorted in strictly increasing order. Each ID in friends is guaranteed to appear in the order array.Return an array containing your friends' IDs in their finishing order.
Example 1:
Input: order = [3,1,2,5,4], friends = [1,3,4]
Output: [3,1,4]
Explanation:
The finishing order is [3, 1, 2, 5, 4]. Therefore, the finishing order of your friends is [3, 1, 4].
Example 2:
Input: order = [1,4,5,3,2], friends = [2,5]
Output: [5,2]
Explanation:
The finishing order is [1, 4, 5, 3, 2]. Therefore, the finishing order of your friends is [5, 2].
Constraints:
1 <= n == order.length <= 100order contains every integer from 1 to n exactly once1 <= friends.length <= min(8, n)1 <= friends[i] <= nfriends is strictly increasingProblem summary: You are given an integer array order of length n and an integer array friends. order contains every integer from 1 to n exactly once, representing the IDs of the participants of a race in their finishing order. friends contains the IDs of your friends in the race sorted in strictly increasing order. Each ID in friends is guaranteed to appear in the order array. Return an array containing your friends' IDs in their finishing order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[3,1,2,5,4] [1,3,4]
[1,4,5,3,2] [2,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3668: Restore Finishing Order
class Solution {
public int[] recoverOrder(int[] order, int[] friends) {
int n = order.length;
int[] d = new int[n + 1];
for (int i = 0; i < n; ++i) {
d[order[i]] = i;
}
return Arrays.stream(friends)
.boxed()
.sorted((a, b) -> d[a] - d[b])
.mapToInt(Integer::intValue)
.toArray();
}
}
// Accepted solution for LeetCode #3668: Restore Finishing Order
func recoverOrder(order []int, friends []int) []int {
n := len(order)
d := make([]int, n+1)
for i, x := range order {
d[x] = i
}
sort.Slice(friends, func(i, j int) bool {
return d[friends[i]] < d[friends[j]]
})
return friends
}
# Accepted solution for LeetCode #3668: Restore Finishing Order
class Solution:
def recoverOrder(self, order: List[int], friends: List[int]) -> List[int]:
d = {x: i for i, x in enumerate(order)}
return sorted(friends, key=lambda x: d[x])
// Accepted solution for LeetCode #3668: Restore Finishing Order
fn recover_order(order: Vec<i32>, friends: Vec<i32>) -> Vec<i32> {
use std::collections::HashSet;
let s: HashSet<_> = friends.into_iter().collect();
order.into_iter().filter(|n| s.contains(n)).collect()
}
fn main() {
let order = vec![3, 1, 2, 5, 4];
let friends = vec![1, 3, 4];
let ret = recover_order(order, friends);
println!("ret={ret:?}");
}
#[test]
fn test() {
{
let order = vec![3, 1, 2, 5, 4];
let friends = vec![1, 3, 4];
let ret = recover_order(order, friends);
assert_eq!(ret, vec![3, 1, 4]);
}
{
let order = vec![1, 4, 5, 3, 2];
let friends = vec![2, 5];
let ret = recover_order(order, friends);
assert_eq!(ret, vec![5, 2]);
}
}
// Accepted solution for LeetCode #3668: Restore Finishing Order
function recoverOrder(order: number[], friends: number[]): number[] {
const n = order.length;
const d: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
d[order[i]] = i;
}
return friends.sort((a, b) => d[a] - d[b]);
}
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.