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 core interview patterns strategy.
Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each of the two inputs arrays will contain an id field that has an integer value.
joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length of joinedArray should be the length of unique values of id. The returned array should be sorted in ascending order based on the id key.
If a given id exists in one array but not the other, the single object with that id should be included in the result array without modification.
If two objects share an id, their properties should be merged into a single object:
arr2 should override the value from arr1.Example 1:
Input:
arr1 = [
{"id": 1, "x": 1},
{"id": 2, "x": 9}
],
arr2 = [
{"id": 3, "x": 5}
]
Output:
[
{"id": 1, "x": 1},
{"id": 2, "x": 9},
{"id": 3, "x": 5}
]
Explanation: There are no duplicate ids so arr1 is simply concatenated with arr2.
Example 2:
Input:
arr1 = [
{"id": 1, "x": 2, "y": 3},
{"id": 2, "x": 3, "y": 6}
],
arr2 = [
{"id": 2, "x": 10, "y": 20},
{"id": 3, "x": 0, "y": 0}
]
Output:
[
{"id": 1, "x": 2, "y": 3},
{"id": 2, "x": 10, "y": 20},
{"id": 3, "x": 0, "y": 0}
]
Explanation: The two objects with id=1 and id=3 are included in the result array without modifiction. The two objects with id=2 are merged together. The keys from arr2 override the values in arr1.
Example 3:
Input:
arr1 = [
{"id": 1, "b": {"b": 94},"v": [4, 3], "y": 48}
]
arr2 = [
{"id": 1, "b": {"c": 84}, "v": [1, 3]}
]
Output: [
{"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}
]
Explanation: The two objects with id=1 are merged together. For the keys "b" and "v" the values from arr2 are used. Since the key "y" only exists in arr1, that value is taken form arr1.
Constraints:
arr1 and arr2 are valid JSON arraysarr1 and arr2 has a unique integer id key2 <= JSON.stringify(arr1).length <= 1062 <= JSON.stringify(arr2).length <= 106Problem summary: Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each of the two inputs arrays will contain an id field that has an integer value. joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length of joinedArray should be the length of unique values of id. The returned array should be sorted in ascending order based on the id key. If a given id exists in one array but not the other, the single object with that id should be included in the result array without modification. If two objects share an id, their properties should be merged into a single object: If a key only exists in one object, that single key-value pair should be included in the object. If a key is included in both objects, the value in the object from arr2 should override the value from arr1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[{"id": 1,"x": 1},{"id": 2,"x": 9}]
[{"id": 3,"x": 5}][{"id": 1,"x": 2,"y": 3},{"id": 2,"x": 3,"y": 6}]
[{"id": 2,"x": 10,"y": 20},{"id": 3,"x": 0,"y": 0}][{"id":1,"b":{"b": 94},"v":[4,3],"y":48}]
[{"id":1,"b":{"c": 84},"v":[1,3]}]Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2722: Join Two Arrays by ID
// Auto-generated Java example from ts.
class Solution {
public void exampleSolution() {
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2722: Join Two Arrays by ID
// function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
// const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc);
// const d = arr1.reduce(r, {});
//
// arr2.forEach(x => {
// if (d[x.id]) {
// Object.assign(d[x.id], x);
// } else {
// d[x.id] = x;
// }
// });
// return Object.values(d);
// }
//
// type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
// type ArrayType = { id: number } & Record<string, JSONValue>;
//
// type Obj = Record<number, ArrayType>;
// Accepted solution for LeetCode #2722: Join Two Arrays by ID
// Auto-generated Go example from ts.
func exampleSolution() {
}
// Reference (ts):
// // Accepted solution for LeetCode #2722: Join Two Arrays by ID
// function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
// const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc);
// const d = arr1.reduce(r, {});
//
// arr2.forEach(x => {
// if (d[x.id]) {
// Object.assign(d[x.id], x);
// } else {
// d[x.id] = x;
// }
// });
// return Object.values(d);
// }
//
// type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
// type ArrayType = { id: number } & Record<string, JSONValue>;
//
// type Obj = Record<number, ArrayType>;
# Accepted solution for LeetCode #2722: Join Two Arrays by ID
# Auto-generated Python example from ts.
def example_solution() -> None:
return
# Reference (ts):
# // Accepted solution for LeetCode #2722: Join Two Arrays by ID
# function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
# const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc);
# const d = arr1.reduce(r, {});
#
# arr2.forEach(x => {
# if (d[x.id]) {
# Object.assign(d[x.id], x);
# } else {
# d[x.id] = x;
# }
# });
# return Object.values(d);
# }
#
# type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
# type ArrayType = { id: number } & Record<string, JSONValue>;
#
# type Obj = Record<number, ArrayType>;
// Accepted solution for LeetCode #2722: Join Two Arrays by ID
// Rust example auto-generated from ts 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 (ts):
// // Accepted solution for LeetCode #2722: Join Two Arrays by ID
// function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
// const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc);
// const d = arr1.reduce(r, {});
//
// arr2.forEach(x => {
// if (d[x.id]) {
// Object.assign(d[x.id], x);
// } else {
// d[x.id] = x;
// }
// });
// return Object.values(d);
// }
//
// type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
// type ArrayType = { id: number } & Record<string, JSONValue>;
//
// type Obj = Record<number, ArrayType>;
// Accepted solution for LeetCode #2722: Join Two Arrays by ID
function join(arr1: ArrayType[], arr2: ArrayType[]): ArrayType[] {
const r = (acc: Obj, x: ArrayType): Obj => ((acc[x.id] = x), acc);
const d = arr1.reduce(r, {});
arr2.forEach(x => {
if (d[x.id]) {
Object.assign(d[x.id], x);
} else {
d[x.id] = x;
}
});
return Object.values(d);
}
type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
type ArrayType = { id: number } & Record<string, JSONValue>;
type Obj = Record<number, ArrayType>;
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.