LeetCode Problem Workspace

Is Object Empty

Determine if a given object or array is empty by checking its keys or length efficiently in interviews.

category

0

Topics

code_blocks

2

Code langs

hub

0

Related

Practice Focus

Easy · Is Object Empty core interview pattern

bolt

Answer-first summary

Determine if a given object or array is empty by checking its keys or length efficiently in interviews.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Is Object Empty core interview pattern

Try AiBox Copilotarrow_forward

This problem requires quickly identifying whether an object or array contains elements or key-value pairs. You can return true if it is empty and false otherwise, using standard language operations. Mastering this helps avoid subtle errors with empty structures and ensures your solution is concise and readable.

Problem Statement

Given a JSON-parsed object or array, determine whether it is empty. Return true if it contains no keys or elements, otherwise return false. The input is guaranteed to be a valid object or array.

For example, an object with key-value pairs is not empty, while an object with no keys is empty. Similarly, an array with elements is not empty, while an empty array returns true. You may assume the input comes directly from JSON.parse and respects typical JSON structure.

Examples

Example 1

Input: obj = {"x": 5, "y": 42}

Output: false

The object has 2 key-value pairs so it is not empty.

Example 2

Input: obj = {}

Output: true

The object doesn't have any key-value pairs so it is empty.

Example 3

Input: obj = [null, false, 0]

Output: false

The array has 3 elements so it is not empty.

Constraints

  • obj is a valid JSON object or array
  • 2 <= JSON.stringify(obj).length <= 105

Solution Approach

Check Object Keys

If the input is an object, use Object.keys(obj).length to determine emptiness. Return true if the length is 0, false otherwise. This avoids manual iteration and ensures O(1) checks for small objects.

Check Array Length

If the input is an array, directly check obj.length. Return true if the length is 0, otherwise false. This approach handles arrays efficiently and matches the core pattern of validating emptiness.

Unified Type Handling

First determine if the input is an array or object. Apply the corresponding emptiness check. This prevents type errors and covers both cases of the problem pattern without redundant code.

Complexity Analysis

Metric Value
Time **
Space **

Time complexity is O(1) for checking array length and O(n) for object keys with n properties. Space complexity is O(n) if Object.keys creates a new array of keys.

What Interviewers Usually Probe

  • Expect candidates to differentiate between objects and arrays.
  • Watch for solutions that manually iterate instead of using built-in properties.
  • Check that candidates handle empty and non-empty cases correctly.

Common Pitfalls or Variants

Common pitfalls

  • Assuming empty objects and arrays are the same without type check.
  • Iterating unnecessarily over object keys instead of checking length.
  • Returning incorrect values for falsy elements in arrays like 0 or null.

Follow-up variants

  • Check nested objects for emptiness recursively.
  • Determine emptiness for objects containing only empty arrays or objects.
  • Return a count of non-empty elements instead of a boolean.

FAQ

What does Is Object Empty mean in this context?

It means returning true if the object or array has no elements or keys, and false otherwise.

Can I assume the input is always a valid object or array?

Yes, the problem guarantees the input is JSON-parsed and valid.

How do I handle arrays with falsy elements like 0 or null?

You count the presence of elements regardless of their value; only the length matters for emptiness.

Is checking Object.keys(obj).length efficient?

Yes, for small objects it is efficient and standard for this interview pattern.

Are nested empty objects considered empty here?

Only the top-level object or array is checked; nested contents do not affect the top-level emptiness.

terminal

Solution

Solution 1

#### TypeScript

1
2
3
4
5
6
7
8
9
10
/**
 * @param {Object | Array} obj
 * @return {boolean}
 */
var isEmpty = function (obj) {
    for (const x in obj) {
        return false;
    }
    return true;
};

Solution 2

#### TypeScript

1
2
3
4
5
6
7
8
9
10
/**
 * @param {Object | Array} obj
 * @return {boolean}
 */
var isEmpty = function (obj) {
    for (const x in obj) {
        return false;
    }
    return true;
};
Is Object Empty Solution: Is Object Empty core interview pattern | LeetCode #2727 Easy