Skip to main content

Check if Object Instance of Class - Solution & Explanation

Medium7 min readAsked at: Microsoft, Google
Practice this problem

Problem Statement

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()".

Approach Overview

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 1: Approach 1: Use Language-Specific Instance Checking

In this approach, we make use of direct language features to determine if an object is an instance of a given class. Most programming languages have built-in operators or functions to check instanceof relationships, such as 'instanceof' in JavaScript, or 'isinstance' in Python.

In JavaScript, the 'instanceof' operator checks if an object is an instance of a class. It walks up the prototype chain to see if the constructor appears anywhere in the object's prototype chain. This method is suitable for object instances and covers cases such as subclass inheritance.

Code

JavaScript

Python

Java

C#

C

Complexity

Time Complexity: O(n), where n is the depth of prototype chain.
Space Complexity: O(1), no extra space is used.

Try this approach in the editor →

Approach 2: Approach 2: Manual Prototype Chain Walking (JavaScript Specific)

This approach is specifically for JavaScript where we manually traverse the prototype chain of the object to check if the class prototype is found in the chain.

This solution manually walks up the prototype chain by using 'Object.getPrototypeOf'. For each step in the chain, it verifies if the current prototype matches the prototype of the class, which ensures that subclass checks are also performed. This method provides finer control and understanding of the checking process.

Code

JavaScript

C++

C

Complexity

Time Complexity: O(n), where n is the depth of the prototype chain.
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Default Approach

Code

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Use Language-Specific Instance Checking

Time Complexity: O(n), where n is the depth of prototype chain.
Space Complexity: O(1), no extra space is used.

Approach 2: Manual Prototype Chain Walking (JavaScript Specific)

Time Complexity: O(n), where n is the depth of the prototype chain.
Space Complexity: O(1)

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Language-Specific Instance CheckingO(1)O(1)Default choice. Uses built-in operators like instanceof or isinstance for fast and reliable type checks.
Manual Prototype Chain WalkingO(h)O(1)When implementing custom instance logic or demonstrating understanding of JavaScript prototype inheritance.

Video Solution

Check if Object Instance of Class - Leetcode 2618 - JavaScript 30-Day ChallengeNeetCodeIO4,299 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Check if Object Instance of Class easy or hard?
The problem is rated Medium because the concept appears simple but understanding how instance checks work internally can be tricky. The optimal solution is short using built-in operators, but explaining prototype traversal or inheritance mechanics requires deeper knowledge.
Check if Object Instance of Class Python/Java solution
Python solves the problem using isinstance(obj, ClassName), which checks the object's type and its parent classes. Java uses the instanceof operator, which returns true if the object belongs to the class or any subclass in its inheritance hierarchy.
How to solve Check if Object Instance of Class in O(1)?
Use the language's native instance checking operator. For example, JavaScript uses obj instanceof ClassName, Python uses isinstance(obj, ClassName), and Java also supports instanceof. These operations directly verify type relationships and run in constant time with no extra space.
What is the best approach for Check if Object Instance of Class?
The best approach is to use the language’s built-in instance checking feature such as JavaScript's instanceof, Python's isinstance(), Java's instanceof, or C#'s is operator. These are optimized runtime checks that verify whether the object's inheritance chain contains the given class. They run in O(1) time and require no additional memory.
Is Check if Object Instance of Class asked at Google/Amazon/Meta?
Problems involving type checking and object inheritance appear in interviews at companies that emphasize JavaScript or object-oriented fundamentals. Variants of prototype chain questions and instanceof behavior have been discussed in interviews at companies like Meta and frontend-heavy roles at large tech firms.
What data structure is used in Check if Object Instance of Class?
The core concept relies on the class inheritance hierarchy or prototype chain. In JavaScript this is implemented through linked prototype references between objects, forming a chain that can be traversed until the root object is reached.
What is the time complexity of Check if Object Instance of Class?
Using built-in instance checking typically runs in O(1) time with O(1) space because the runtime directly inspects the object's type information. A manual prototype-chain traversal solution in JavaScript takes O(h) time where h is the height of the prototype chain.

Ready to solve this problem?

Practice Check if Object Instance of Class with our built-in code editor and test cases.

Practice on FleetCode