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 an object or array obj, return a compact object.
A compact object is the same as the original object, except with keys containing falsy values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered falsy when Boolean(value) returns false.
You may assume the obj is the output of JSON.parse. In other words, it is valid JSON.
Example 1:
Input: obj = [null, 0, false, 1] Output: [1] Explanation: All falsy values have been removed from the array.
Example 2:
Input: obj = {"a": null, "b": [false, 1]}
Output: {"b": [1]}
Explanation: obj["a"] and obj["b"][0] had falsy values and were removed.
Example 3:
Input: obj = [null, 0, 5, [0], [false, 16]] Output: [5, [], [16]] Explanation: obj[0], obj[1], obj[3][0], and obj[4][0] were falsy and removed.
Constraints:
obj is a valid JSON object2 <= JSON.stringify(obj).length <= 106Problem summary: Given an object or array obj, return a compact object. A compact object is the same as the original object, except with keys containing falsy values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered falsy when Boolean(value) returns false. You may assume the obj is the output of JSON.parse. In other words, it is valid JSON.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[null, 0, false, 1]
{"a": null, "b": [false, 1]}[null, 0, 5, [0], [false, 16]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2705: Compact Object
// Auto-generated Java example from ts.
class Solution {
public void exampleSolution() {
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2705: Compact Object
// type Obj = Record<any, any>;
//
// function compactObject(obj: Obj): Obj {
// if (!obj || typeof obj !== 'object') {
// return obj;
// }
// if (Array.isArray(obj)) {
// return obj.filter(Boolean).map(compactObject);
// }
// return Object.entries(obj).reduce((acc, [key, value]) => {
// if (value) {
// acc[key] = compactObject(value);
// }
// return acc;
// }, {} as Obj);
// }
// Accepted solution for LeetCode #2705: Compact Object
// Auto-generated Go example from ts.
func exampleSolution() {
}
// Reference (ts):
// // Accepted solution for LeetCode #2705: Compact Object
// type Obj = Record<any, any>;
//
// function compactObject(obj: Obj): Obj {
// if (!obj || typeof obj !== 'object') {
// return obj;
// }
// if (Array.isArray(obj)) {
// return obj.filter(Boolean).map(compactObject);
// }
// return Object.entries(obj).reduce((acc, [key, value]) => {
// if (value) {
// acc[key] = compactObject(value);
// }
// return acc;
// }, {} as Obj);
// }
# Accepted solution for LeetCode #2705: Compact Object
# Auto-generated Python example from ts.
def example_solution() -> None:
return
# Reference (ts):
# // Accepted solution for LeetCode #2705: Compact Object
# type Obj = Record<any, any>;
#
# function compactObject(obj: Obj): Obj {
# if (!obj || typeof obj !== 'object') {
# return obj;
# }
# if (Array.isArray(obj)) {
# return obj.filter(Boolean).map(compactObject);
# }
# return Object.entries(obj).reduce((acc, [key, value]) => {
# if (value) {
# acc[key] = compactObject(value);
# }
# return acc;
# }, {} as Obj);
# }
// Accepted solution for LeetCode #2705: Compact Object
// 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 #2705: Compact Object
// type Obj = Record<any, any>;
//
// function compactObject(obj: Obj): Obj {
// if (!obj || typeof obj !== 'object') {
// return obj;
// }
// if (Array.isArray(obj)) {
// return obj.filter(Boolean).map(compactObject);
// }
// return Object.entries(obj).reduce((acc, [key, value]) => {
// if (value) {
// acc[key] = compactObject(value);
// }
// return acc;
// }, {} as Obj);
// }
// Accepted solution for LeetCode #2705: Compact Object
type Obj = Record<any, any>;
function compactObject(obj: Obj): Obj {
if (!obj || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj.filter(Boolean).map(compactObject);
}
return Object.entries(obj).reduce((acc, [key, value]) => {
if (value) {
acc[key] = compactObject(value);
}
return acc;
}, {} as Obj);
}
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.