Make Object Immutable - Problem
Create an Immutable Object
Your task is to implement a function that transforms any JavaScript object or array into an immutable version. An immutable object is one that cannot be modified after creation - any attempt to alter it should throw a specific error message.
The Challenge:
โข Transform the input object/array to prevent any modifications
โข Handle three types of operations that should throw errors:
- Modifying object properties:
- Modifying array indices:
- Calling mutating array methods:
Mutating Array Methods:
Important: Throw string literals, not Error objects. The input is guaranteed to be a valid JSON object/array.
Your task is to implement a function that transforms any JavaScript object or array into an immutable version. An immutable object is one that cannot be modified after creation - any attempt to alter it should throw a specific error message.
The Challenge:
โข Transform the input object/array to prevent any modifications
โข Handle three types of operations that should throw errors:
- Modifying object properties:
"Error Modifying: ${key}"- Modifying array indices:
"Error Modifying Index: ${index}"- Calling mutating array methods:
"Error Calling Method: ${methodName}"Mutating Array Methods:
['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse']Important: Throw string literals, not Error objects. The input is guaranteed to be a valid JSON object/array.
Input & Output
example_1.js โ Basic Object
$
Input:
{"name": "John", "age": 30}
โบ
Output:
{"name": "John", "age": 30}
๐ก Note:
Creates an immutable version of the object. Reading properties works normally, but any attempt to modify (like obj.name = 'Jane') would throw 'Error Modifying: name'
example_2.js โ Array Operations
$
Input:
[1, 2, 3, 4, 5]
โบ
Output:
[1, 2, 3, 4, 5]
๐ก Note:
Array access works normally (arr[0] returns 1), but arr[0] = 10 throws 'Error Modifying Index: 0' and arr.push(6) throws 'Error Calling Method: push'
example_3.js โ Nested Structure
$
Input:
{"users": [{"id": 1, "name": "Alice"}], "count": 1}
โบ
Output:
{"users": [{"id": 1, "name": "Alice"}], "count": 1}
๐ก Note:
Nested objects and arrays are also immutable. obj.users[0].name = 'Bob' would throw 'Error Modifying: name', and obj.users.pop() would throw 'Error Calling Method: pop'
Visualization
Tap to expand
Understanding the Visualization
1
Wrap with Protection
Original object gets wrapped with a Proxy 'security system'
2
Monitor Access
All interactions are monitored by the Proxy handlers
3
Allow Observations
Reading properties and accessing data is permitted
4
Block Modifications
Any attempt to modify triggers specific error messages
Key Takeaway
๐ฏ Key Insight: JavaScript Proxies act as a transparent security layer that intercepts all operations, making it possible to create truly immutable objects with custom error handling while maintaining the original API.
Time & Space Complexity
Time Complexity
O(n)
Where n is the total number of properties/elements, as we traverse once to create proxies
โ Linear Growth
Space Complexity
O(n)
Creates proxy wrappers for each object/array in the structure
โก Linearithmic Space
Constraints
- The input is guaranteed to be a valid JSON object or array
- Nested depth can be up to 100 levels
- Object keys are strings following JSON specification
- Array indices are non-negative integers
- Must throw string literals, not Error objects
-
Only these array methods should be blocked:
['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse']
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code