Skip to main content

Is Object Empty - Solution & Explanation

Easy8 min readAsked at: Google
Practice this problem

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:

  • obj is a valid JSON object or array
  • 2 <= 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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

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).

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Default Approach

Code

TypeScript

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Size/Length Properties

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.

Using Iterator Method

Time Complexity: O(1).
Space Complexity: O(1).

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Using Size/Length PropertiesO(n)O(1)Most readable solution; preferred in interviews and common production code.
Using Iterator MethodO(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 is classified as an Easy problem. It tests familiarity with basic object operations, iteration, and built-in language utilities rather than complex algorithms.
Is Object Empty Python/Java solution
In Python, you can check emptiness using len(obj) == 0 for dictionaries or lists. In Java, checking map.isEmpty() or collection.isEmpty() achieves the same goal. The underlying complexity remains O(n) in the worst case depending on implementation details.
How to solve Is Object Empty in O(n)?
Iterate over the object's keys and determine whether any element exists. Using Object.keys(obj).length === 0 or a for...in loop both work. The algorithm scans the properties once, resulting in O(n) time and O(1) extra space.
What is the best approach for Is Object Empty?
The most common approach checks the number of keys using a built-in helper such as Object.keys(obj).length in JavaScript. If the length is zero, the object is empty. This runs in O(n) time because all keys may be scanned, with O(1) additional space. It is concise and widely expected in interviews.
Is Is Object Empty asked at Google/Amazon/Meta?
Object and dictionary inspection problems appear frequently in interviews at companies like Google, Amazon, and Meta, especially in JavaScript-focused roles. While this exact problem may vary, checking whether a structure contains elements is a common fundamental task.
What data structure is used in Is Object Empty?
The problem operates on objects or associative containers, similar to hash maps or dictionaries. These structures store key-value pairs and allow iteration over keys to determine whether any entries exist.
What is the time complexity of Is Object Empty?
The time complexity is O(n), where n is the number of keys or elements in the object or array. Methods like Object.keys() or iteration loops may need to inspect every property in the worst case. Space complexity is typically O(1) excluding temporary structures created by built-in helpers.

Ready to solve this problem?

Practice Is Object Empty with our built-in code editor and test cases.

Practice on FleetCode