Is Object Empty - Solution & Explanation
Problem Statement
Given an object or an array, return if it is empty.
- An empty object contains no key-value pairs.
- An empty array contains no elements.
You may assume the object or array is the output of JSON.parse.
Example 1:
Input: obj = {"x": 5, "y": 42}
Output: false
Explanation: The object has 2 key-value pairs so it is not empty.
Example 2:
Input: obj = {}
Output: true
Explanation: The object doesn't have any key-value pairs so it is empty.
Example 3:
Input: obj = [null, false, 0] Output: false Explanation: The array has 3 elements so it is not empty.
Constraints:
objis a valid JSON object or array2 <= JSON.stringify(obj).length <= 105
Can you solve it in O(1) time?
Approach Overview
Problem Overview: You receive an object or array and need to determine whether it contains any elements. Return true if it has no enumerable keys or items, otherwise return false.
Approach 1: Using Size/Length Properties (O(n) time, O(1) space)
The most common solution checks how many keys exist in the object. In JavaScript this typically means calling Object.keys(obj) and inspecting the resulting array length. If the length is 0, the object has no properties. Arrays follow the same idea using their length field. The algorithm internally iterates through the object's enumerable keys, giving a time complexity of O(n) where n is the number of properties, while using constant auxiliary space aside from the key list created by the runtime.
This approach is concise and readable, which is why it appears frequently in production code and interviews. It relies on built-in language helpers that already implement efficient iteration over object properties.
Approach 2: Using Iterator Method (O(n) time, O(1) space)
Another approach directly iterates through the object using a loop such as for...in (JavaScript) or language-specific iterators in C++, Java, or Python. The key idea is early termination: the moment you encounter the first property, you know the object is not empty and can immediately return false. If the loop finishes without visiting any property, the object is empty.
This method avoids creating an intermediate list of keys and may exit earlier in practice if the object contains elements. The worst-case complexity is still O(n) because every key might need to be checked. The space complexity remains O(1) since no additional data structures are required. The technique relies on fundamental data structure traversal patterns and is useful when working close to the runtime or when minimizing allocations.
Recommended for interviews: Both solutions are acceptable. The size/length property approach is the most recognizable and communicates the intent immediately, so many interviewers expect it first. The iterator-based approach demonstrates deeper understanding of how property traversal works and can be slightly more memory-efficient. Showing both approaches proves you understand object traversal and basic algorithm complexity tradeoffs.
Approach 1: Using Size/Length Properties
This approach involves using language-specific properties or methods that can quickly determine if an object or array has any elements. For objects, this often involves checking the number of keys, while for arrays it's about checking the length.
In this hypothetical C solution, we are assuming the use of some JSON library where JSON objects and arrays have defined length and size. The isObjectEmpty function checks if the length (or size) is zero to determine emptiness.
Complexity
Time Complexity: O(1) because we are directly accessing built-in properties.
Space Complexity: O(1) since no extra space is used except for few variables.
Approach 2: Using Iterator Method
This method relies on the use of iterators to quickly assess emptiness by attempting to iterate over the first element. If there is no element to begin with, it returns empty.
This is a more experimental C solution assuming some JSON library where iterators can be compared to check for zero elements (i.e., iterators at the beginning equal iterators at the end).
Complexity
Time Complexity: O(1).
Space Complexity: O(1).
Approach 3: Default Approach
Code
TypeScript
JavaScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Using Size/Length Properties | Time Complexity: O(1) because we are directly accessing built-in properties. |
| Using Iterator Method | Time Complexity: O(1). |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Size/Length Properties | O(n) | O(1) | Most readable solution; preferred in interviews and common production code. |
| Using Iterator Method | O(n) | O(1) | Useful when you want early exit without allocating a list of keys. |
Video Solution
Is Object Empty | Leetcode 2727 | JSON | 30 Days of JavaScript #javascript #leetcode • Learn With Chirag • 1,741 views views
Watch 5 more video solutions →Frequently Asked Questions
Is Is Object Empty easy or hard?
Is Object Empty Python/Java solution
How to solve Is Object Empty in O(n)?
What is the best approach for Is Object Empty?
Is Is Object Empty asked at Google/Amazon/Meta?
What data structure is used in Is Object Empty?
What is the time complexity of Is Object Empty?
Ready to solve this problem?
Practice Is Object Empty with our built-in code editor and test cases.
Practice on FleetCode