Watch 7 video solutions for Is Object Empty, a easy level problem. This walkthrough by Learn With Chirag has 1,657 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an object or an array, return if it is empty.
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:
obj is a valid JSON object or array2 <= JSON.stringify(obj).length <= 105Can you solve it in O(1) time?
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 | 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. |