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: "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
Original Object{name: "John", age: 30}๐Ÿ›ก๏ธ Proxy ProtectionRead Access โœ“obj.namereturns "John"Write Blocked โŒobj.name = "Jane""Error Modifying: name"๐Ÿ‘๏ธObserver๐ŸšซModifier
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Creates proxy wrappers for each object/array in the structure

n
2n
โšก 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']
Asked in
Meta 45 Google 38 Amazon 32 Netflix 28 Airbnb 25
42.4K Views
Medium 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