Talentd
FleetCode
Make and follow sheets.Sign in

Infinite Method Object solution and explanation

Easy3 min read92.3% acceptanceUpdated 24 Mar 2026Premium elsewhere, free here
LeetCode 2690. Infinite Method Object. Вирішуємо літкод Українською JSКанал про вирішення ЛітКод Українською · 4:02 · 294 viewsWatch on YouTube
1 more video on this problem
  1. LeetCode 2690. Infinite Method Object. Вирішуємо літкод Українською JSКанал про вирішення ЛітКод Українською · 4:02 · 294 views
  2. 2690. Infinite Method ObjectTech Guides · 5:05 · 28 views

The problem

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

function createInfiniteObject(): Record<string, () => string> {
    return new Proxy(
        {},
        {
            get: (_, prop) => () => prop.toString(),
        },
    );
}

/**
 * const obj = createInfiniteObject();
 * obj['abc123'](); // "abc123"
 */
Try this approach in the editor

Complexity analysis

ApproachTimeSpaceWhen to Use
Static Method DefinitionsO(1)O(n)Only when the set of method names is small and known beforehand
Proxy-Based Dynamic Method ResolutionO(1)O(1)Best approach for handling unlimited method names dynamically

Frequently asked questions

Is Infinite Method Object easy or hard?

Infinite Method Object is classified as Easy with a very high acceptance rate around 92%. The challenge mainly tests familiarity with JavaScript Proxy behavior rather than algorithmic complexity.

Infinite Method Object Python/Java solution

The official problem targets JavaScript or TypeScript because it relies on the Proxy API. Languages like Python or Java do not provide an identical runtime proxy for property access, so the direct solution is specific to JavaScript environments.

How to solve Infinite Method Object in O(1)?

Create a Proxy object and implement the get handler. When any property is accessed, return a function that returns the property name string. Since the handler performs a constant amount of work and stores no additional data, both time and space complexity remain O(1).

What is the best approach for Infinite Method Object?

The optimal approach uses a JavaScript Proxy with a get trap. Every property access dynamically returns a function that captures the method name and returns it when invoked. This satisfies the requirement of supporting unlimited method names while keeping time and space complexity O(1).

Is Infinite Method Object asked at Google/Amazon/Meta?

This problem is part of the JavaScript-focused LeetCode questions commonly used to evaluate understanding of proxies, objects, and meta-programming. Similar dynamic object design questions appear in frontend and full‑stack interviews at large tech companies.

What data structure is used in Infinite Method Object?

The core mechanism is the JavaScript Proxy object. It intercepts property access on an object and dynamically returns a function for any method name, enabling the object to behave as if it has infinitely many methods.

What is the time complexity of Infinite Method Object?

The Proxy-based solution runs in O(1) time for each method access and invocation. Property lookup triggers the proxy handler, which immediately returns a small function without performing iteration or additional computation.

Comments

Now write it yourselfClose this page and solve Infinite Method Object in the editor, without looking.
Practise Infinite Method Object