Is Object Empty - Problem

Given an object or an array, return true if it is empty, false otherwise.

Definition of 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.

Input & Output

Example 1 — Empty Object
$ Input: obj = {}
Output: true
💡 Note: The object has no key-value pairs, so it's empty and we return true
Example 2 — Non-empty Array
$ Input: obj = [null, false, 0]
Output: false
💡 Note: The array contains elements (even though they're falsy values), so it's not empty
Example 3 — Empty Array
$ Input: obj = []
Output: true
💡 Note: The array contains no elements, so it's empty and we return true

Constraints

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

Visualization

Tap to expand
Is Object Empty: Check Container ContentsInput: {}Empty objectInput: []Empty arrayInput: {"x": 1}Non-empty objectInput: [1, 2]Non-empty arrayOutput: trueOutput: trueOutput: falseOutput: falseCheck if container has zero itemsEmpty = true, Non-empty = false
Understanding the Visualization
1
Input
Object or array from JSON.parse
2
Check
Determine if container has any items
3
Output
Return boolean true/false
Key Takeaway
🎯 Key Insight: Use built-in size methods to efficiently check if containers are empty
Asked in
Google 15 Microsoft 12 Amazon 8
35.7K Views
Medium Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen