Watch 7 video solutions for Check if Object Instance of Class, a medium level problem. This walkthrough by NeetCodeIO has 4,189 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Write a function that checks if a given value is an instance of a given class or superclass. For this problem, an object is considered an instance of a given class if that object has access to that class's methods.
There are no constraints on the data types that can be passed to the function. For example, the value or the class could be undefined.
Example 1:
Input: func = () => checkIfInstanceOf(new Date(), Date) Output: true Explanation: The object returned by the Date constructor is, by definition, an instance of Date.
Example 2:
Input: func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }
Output: true
Explanation:
class Animal {};
class Dog extends Animal {};
checkIfInstanceOf(new Dog(), Animal); // true
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.
Example 3:
Input: func = () => checkIfInstanceOf(Date, Date) Output: false Explanation: A date constructor cannot logically be an instance of itself.
Example 4:
Input: func = () => checkIfInstanceOf(5, Number) Output: true Explanation: 5 is a Number. Note that the "instanceof" keyword would return false. However, it is still considered an instance of Number because it accesses the Number methods. For example "toFixed()".
Problem Overview: You are given an object and a class (constructor). The task is to determine whether the object is an instance of that class. In other words, check whether the object's inheritance chain contains the provided class constructor.
Approach 1: Use Language-Specific Instance Checking (Time: O(1), Space: O(1))
Most languages provide a built‑in operator or function for checking class membership. In JavaScript you use instanceof, Python provides isinstance(), Java uses instanceof, and C# provides the is operator. These utilities internally inspect the object's type metadata or prototype chain and return a boolean result. Because the runtime handles the check natively, the operation is effectively constant time for typical use cases and requires no additional memory.
This approach is the most reliable and concise. It correctly handles inheritance, meaning subclasses also count as instances of the parent class. When writing production code or solving interview problems, this is almost always the preferred solution because it uses the language’s built‑in type system instead of re‑implementing it manually.
Approach 2: Manual Prototype Chain Walking (Time: O(h), Space: O(1))
In JavaScript, objects inherit from other objects through a prototype chain. Each object has an internal [[Prototype]] reference accessible through Object.getPrototypeOf() or __proto__. To determine if an object is an instance of a constructor, you can repeatedly move up this chain and compare each prototype with constructor.prototype.
The algorithm is straightforward: start with the object's prototype, iterate upward using Object.getPrototypeOf(), and stop if you either find the target prototype or reach null. If the target appears anywhere in the chain, the object is an instance of that class. The runtime cost depends on the depth of the inheritance chain (h), which is usually small but technically linear in the chain length.
This approach demonstrates a deeper understanding of how JavaScript implements object-oriented programming through prototypes rather than classical classes. It’s useful when implementing custom type utilities or when interviewers want to see knowledge of the prototype chain itself.
Recommended for interviews: Use the language’s built‑in instance check. Interviewers expect you to recognize the native operator (instanceof, isinstance, etc.) because it’s concise and idiomatic. The manual prototype traversal is still valuable to discuss because it shows you understand how instance checking works under the hood.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Language-Specific Instance Checking | O(1) | O(1) | Default choice. Uses built-in operators like instanceof or isinstance for fast and reliable type checks. |
| Manual Prototype Chain Walking | O(h) | O(1) | When implementing custom instance logic or demonstrating understanding of JavaScript prototype inheritance. |