Deep Object Filter - Problem
Deep Object Filter Challenge

Imagine you're building a data cleaning system that needs to intelligently filter complex nested data structures. You're given an object or array obj and a filter function fn, and your task is to create a deep filtering mechanism that not only removes unwanted elements but also cleans up the structure.

The deepFilter function should:
• Apply the filter function fn to each value in the nested structure
• Remove properties/elements where fn returns false
Clean up empty objects and arrays that remain after filtering
• Return undefined if the entire structure becomes empty

This is like having a smart vacuum cleaner that not only removes dirt but also tidies up empty containers afterwards! The challenge lies in handling deeply nested structures while maintaining the original data types and relationships.

Input & Output

basic_filtering.py — Python
$ Input: obj = {"a": 1, "b": {"c": 2, "d": 4}, "e": [3, 5]} fn = lambda x: x > 2
Output: {"b": {"d": 4}, "e": [3, 5]}
💡 Note: Values 1 and 2 are filtered out. The nested object 'b' keeps only 'd': 4, and array 'e' keeps both 3 and 5.
empty_cleanup.py — Python
$ Input: obj = {"a": 1, "b": {"c": 1}, "d": [1, 1]} fn = lambda x: x > 5
Output: undefined
💡 Note: All values fail the filter (x > 5). After removing them, all containers become empty and are cleaned up, resulting in undefined.
nested_arrays.py — Python
$ Input: obj = [1, [2, [3, 4]], {"x": 5}] fn = lambda x: x % 2 == 0
Output: [[2, [4]], {}]
💡 Note: Only even numbers pass the filter. The nested structure is preserved, but the object becomes empty and is cleaned up to {}.

Constraints

  • 1 ≤ total number of elements ≤ 104
  • Maximum nesting depth ≤ 100
  • Values can be primitives (number, string, boolean) or nested objects/arrays
  • Filter function will always return a boolean value
  • Objects can have string keys only

Visualization

Tap to expand
Smart Library Organization AnalogyLibrary BuildingFiction SectionClassic ShelfModern ShelfScience SectionPhysics ShelfHistory SectionAncient ShelfGood Book(Keep)Bad Book(Remove)Empty Shelf(Cleanup)Automatic Cleanup!
Understanding the Visualization
1
Enter Section
Start at the library entrance and systematically visit each section
2
Check Books
At each shelf, examine individual books using your criteria
3
Filter Content
Remove books that don't meet the criteria (outdated, damaged, etc.)
4
Clean Shelves
As you finish each shelf, remove it if it's completely empty
5
Clean Sections
When exiting a section, remove it if all its shelves were removed
6
Final Result
The library is now clean with only relevant books and no empty containers
Key Takeaway
🎯 Key Insight: Recursion naturally provides the backtracking needed to clean up empty containers as we return from deeper levels, making the solution both elegant and efficient.
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
67.2K Views
High Frequency
~25 min Avg. Time
1.8K 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