Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
() => checkIfInstanceOf(new Date(), Date)
() => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }() => checkIfInstanceOf(Date, Date)
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2618: Check if Object Instance of Class
// Auto-generated Java example from ts.
class Solution {
public void exampleSolution() {
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2618: Check if Object Instance of Class
// function checkIfInstanceOf(obj: any, classFunction: any): boolean {
// if (classFunction === null || classFunction === undefined) {
// return false;
// }
// while (obj !== null && obj !== undefined) {
// const proto = Object.getPrototypeOf(obj);
// if (proto === classFunction.prototype) {
// return true;
// }
// obj = proto;
// }
// return false;
// }
//
// /**
// * checkIfInstanceOf(new Date(), Date); // true
// */
// Accepted solution for LeetCode #2618: Check if Object Instance of Class
// Auto-generated Go example from ts.
func exampleSolution() {
}
// Reference (ts):
// // Accepted solution for LeetCode #2618: Check if Object Instance of Class
// function checkIfInstanceOf(obj: any, classFunction: any): boolean {
// if (classFunction === null || classFunction === undefined) {
// return false;
// }
// while (obj !== null && obj !== undefined) {
// const proto = Object.getPrototypeOf(obj);
// if (proto === classFunction.prototype) {
// return true;
// }
// obj = proto;
// }
// return false;
// }
//
// /**
// * checkIfInstanceOf(new Date(), Date); // true
// */
# Accepted solution for LeetCode #2618: Check if Object Instance of Class
# Auto-generated Python example from ts.
def example_solution() -> None:
return
# Reference (ts):
# // Accepted solution for LeetCode #2618: Check if Object Instance of Class
# function checkIfInstanceOf(obj: any, classFunction: any): boolean {
# if (classFunction === null || classFunction === undefined) {
# return false;
# }
# while (obj !== null && obj !== undefined) {
# const proto = Object.getPrototypeOf(obj);
# if (proto === classFunction.prototype) {
# return true;
# }
# obj = proto;
# }
# return false;
# }
#
# /**
# * checkIfInstanceOf(new Date(), Date); // true
# */
// Accepted solution for LeetCode #2618: Check if Object Instance of Class
// Rust example auto-generated from ts reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2618: Check if Object Instance of Class
// function checkIfInstanceOf(obj: any, classFunction: any): boolean {
// if (classFunction === null || classFunction === undefined) {
// return false;
// }
// while (obj !== null && obj !== undefined) {
// const proto = Object.getPrototypeOf(obj);
// if (proto === classFunction.prototype) {
// return true;
// }
// obj = proto;
// }
// return false;
// }
//
// /**
// * checkIfInstanceOf(new Date(), Date); // true
// */
// Accepted solution for LeetCode #2618: Check if Object Instance of Class
function checkIfInstanceOf(obj: any, classFunction: any): boolean {
if (classFunction === null || classFunction === undefined) {
return false;
}
while (obj !== null && obj !== undefined) {
const proto = Object.getPrototypeOf(obj);
if (proto === classFunction.prototype) {
return true;
}
obj = proto;
}
return false;
}
/**
* checkIfInstanceOf(new Date(), Date); // true
*/
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.