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 <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to check if an object is empty involves examining every single part of the object to see if it contains anything. We're going to manually check each part until we find something, or until we've checked everything and found nothing.
Here's how the algorithm would work step-by-step:
def is_object_empty_brute_force(input_object):
# Iterate through all keys in the object
for key in input_object:
# If any key exists, the object is not empty
return False
# If no keys were found, the object is empty
return TrueThe best way to determine if an object is empty is to check if it has any properties at all. We want to avoid looking at specific properties, as that would be inefficient. The fastest approach checks for the existence of any property, and returns right away if it finds one.
Here's how the algorithm would work step-by-step:
def is_object_empty(input_object):
# Attempt to retrieve the first key to check for emptiness
try:
first_key = next(iter(input_object))
# If the object has at least one key, it's not empty
return False
except StopIteration:
# If there are no keys, the object is empty
return True| Case | How to Handle |
|---|---|
| Null or undefined object input | Return true immediately as null/undefined objects are considered empty |
| Object with inherited properties only | Use hasOwnProperty() to ensure only own properties are considered. |
| Object with a very large number of properties | The solution iterates over properties, and should perform reasonably well even with a large number of them although very large objects could lead to performance degradation, but it will still provide a valid result |
| Object with symbolic properties | The `for...in` loop (when combined with `hasOwnProperty()`) and `Object.keys()` do not enumerate Symbol properties; ensure testing includes these if the requirement includes symbols |
| Object with properties that have getter/setter methods | Accessing a getter should not throw an error or have unintended side effects within the emptiness check. |
| Frozen object | The solution should not attempt to modify the object; therefore freezing will not impact the solution. |
| Object created with Object.create(null) | Object.create(null) creates an object without a prototype, so it should not cause issues if combined with `hasOwnProperty()` to avoid checking inherited properties. |
| Empty object {} passed as input | Return true since it has no properties |