Skip to the solution
MediumFrontend6 min read

How do you implement a deepFreeze function recursively?

935 views

The problem

Question presented to candidate: "You call Object.freeze() on a config object that has a nested object property. Can you still mutate that nested object? Write a deepFreeze() function that actually fixes this β€” and make sure it does not infinite-loop on a circular reference."

What a strong answer should cover:

  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: yes β€” Object.freeze() is genuinely shallow. Freezing an object prevents changes to that object's OWN properties, but a NESTED object referenced by one of those properties is genuinely NOT itself frozen and remains fully mutable.
  • πŸ“Œ Interview term: deepFreeze() β€” a real, recursive utility that calls Object.freeze() on the top-level object, then recurses into every property value that is itself an object or function, freezing each one too β€” verified directly to correctly prevent mutation at every nested level, including inside arrays.
  • πŸ“Œ Interview term: the real cycle-safety requirement β€” a precise answer names that a naive recursive deepFreeze() would genuinely infinite-loop on a real, self-referential (circular) object β€” the fix is a real Object.isFrozen(obj) guard at the top of the recursive call: if an object is ALREADY frozen, return immediately rather than recursing into it again β€” verified directly to correctly terminate on a real cyclic structure.
  • πŸ“Œ Interview term: Reflect.ownKeys() for full coverage β€” a precise answer names that iterating with Object.keys() alone would silently skip non-enumerable properties and Symbol-keyed properties; Reflect.ownKeys() (covered in more depth in this bank's own dedicated Proxy/Reflect question) correctly walks every own key, enumerable or not, string or Symbol.
  • A precise answer names the real, practical use case: freezing a shared configuration object or a Redux-style initial state tree so that an accidental deep mutation anywhere in the object graph genuinely throws (in strict mode) rather than silently corrupting shared state.

Clarifying questions expected:

  • None β€” this is an implementation question; directly writing and demonstrating the recursive fix (including the cycle-safety case) is the strong signal.

Code / implementation expected: Yes β€” a full, real, recursive deepFreeze() implementation, executed against a nested object, an array, and a genuinely circular structure.

  • objects
  • recursion
  • immutability

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: Object.freeze() is genuinely shallow, a recursive deepFreeze() fixes every nested level, and a real cyclic object is handled safely β€” verified directly

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 59 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is a Promise and what are its states?

Continue