Infinite Method Object - Solution & Explanation
Problem Statement
Write a function that returns an infinite-method object.
An infinite-method object is defined as an object that allows you to call any method and it will always return the name of the method.
For example, if you execute obj.abc123(), it will return "abc123".
Example 1:
Input: method = "abc123" Output: "abc123" Explanation: const obj = createInfiniteObject(); obj['abc123'](); // "abc123" The returned string should always match the method name.
Example 2:
Input: method = ".-qw73n|^2It" Output: ".-qw73n|^2It" Explanation: The returned string should always match the method name.
Constraints:
0 <= method.length <= 1000
Approach Overview
Problem Overview: Design a function createInfiniteObject() that returns an object capable of handling any method call. No matter which method name you access, calling it should return the method’s name as a string. The object must behave as if it has infinitely many methods defined.
Approach 1: Static Method Definitions (Naive Idea) (Time: O(1) per call, Space: O(n))
A straightforward but impractical idea is to explicitly define every method on the returned object. Each property would store a function that returns its own name. This works for a small, known set of methods, but the requirement here is that the object should support any method name. Predefining methods cannot scale to an infinite interface and defeats the purpose of the problem. While the invocation cost stays constant O(1), memory usage grows with the number of defined methods O(n). This approach demonstrates the expected behavior but fails the “infinite” requirement.
Approach 2: JavaScript Proxy for Dynamic Method Resolution (Time: O(1), Space: O(1))
The intended solution relies on the Proxy object in JavaScript. A proxy intercepts property access on an object using the get trap. Whenever a property is accessed (for example obj.hello), the trap receives the property name. Instead of returning a stored value, the proxy dynamically returns a function that closes over the property name and returns it when invoked.
This design makes the object effectively support infinite methods. Accessing obj.test(), obj.anything(), or obj.randomName() all trigger the same get handler. The handler generates a new function that returns the property name string. Since no methods are stored ahead of time, the object remains lightweight and flexible.
The key insight is that property access and method invocation are separate steps. First, the runtime looks up the property; second, it executes the returned function. The proxy intercepts the lookup step and injects a function that knows the method name. This pattern is common in dynamic APIs, fluent interfaces, and meta‑programming tasks involving objects and proxies.
Each lookup simply returns a small function closure, so the operation runs in constant time O(1) and requires constant extra space O(1). No precomputation or storage of method names is necessary.
Recommended for interviews: The Proxy-based solution is the expected answer. Interviewers want to see that you understand JavaScript’s meta‑programming capabilities and how property interception works. Mentioning the naive “define every method” idea shows you understand the requirement, but implementing the dynamic proxy demonstrates real JavaScript fluency.
Solution
Code
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Static Method Definitions | O(1) | O(n) | Only when the set of method names is small and known beforehand |
| Proxy-Based Dynamic Method Resolution | O(1) | O(1) | Best approach for handling unlimited method names dynamically |
Video Solution
Most Asked Coding Interview Question (Don't Skip !!😮) #shorts • Programiz • 720,696 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Infinite Method Object easy or hard?
Infinite Method Object Python/Java solution
How to solve Infinite Method Object in O(1)?
What is the best approach for Infinite Method Object?
Is Infinite Method Object asked at Google/Amazon/Meta?
What data structure is used in Infinite Method Object?
What is the time complexity of Infinite Method Object?
Ready to solve this problem?
Practice Infinite Method Object with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor