Array of Objects to Matrix - Problem

Transform an array of nested objects into a structured matrix format! This problem challenges you to flatten complex, deeply nested data structures into a readable tabular format.

Given an array arr containing objects or arrays with potentially deep nesting (including child arrays, objects, numbers, strings, booleans, and null values), your task is to:

  • Extract all possible paths from the nested structures
  • Create column headers using dot notation for nested paths (e.g., "user.address.city")
  • Build a matrix where the first row contains sorted column names
  • Populate data rows with values from each object, using empty strings for missing values

The columns must be arranged in lexicographically ascending order, making the output predictable and organized.

Example: [{"name": "John", "address": {"city": "NYC"}}, {"name": "Jane", "age": 25}] becomes a matrix with headers ["address.city", "age", "name"] and corresponding data rows.

Input & Output

example_1.py — Basic Objects
$ Input: [{"b": 1, "a": 2}, {"c": 3, "a": 4}]
Output: [["a", "b", "c"], ["2", "1", ""], ["4", "", "3"]]
💡 Note: Two objects with different keys. Columns sorted: a, b, c. Missing values filled with empty strings.

Time & Space Complexity

Time Complexity
⏱️
O(n * d + m²)

n*d for processing objects, m² for dynamic column insertions in worst case

n
2n
Linear Growth
Space Complexity
O(n * m)

Result matrix plus temporary path storage

n
2n
Linearithmic Space

Constraints

Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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