LeetCode 2776: Convert Callback Based Function to Promise Based Function Solution

Master LeetCode problem 2776 (Convert Callback Based Function to Promise Based Function), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2776. Convert Callback Based Function to Promise Based Function

Medium

Problem Explanation

Explanation:

To convert a callback-based function to a promise-based function, we need to create a mechanism to handle the asynchronous nature of callbacks using promises. We can achieve this by wrapping the existing callback-based function inside a new function that returns a promise. The promise will resolve or reject based on the success or failure of the original callback.

Algorithmic Idea:

  1. Create a new function that takes the same parameters as the original callback-based function and returns a promise.
  2. Inside the new function, call the original callback-based function with the provided parameters and handle the callback's success or failure.
  3. Resolve the promise if the callback succeeds and pass the result to the promise.
  4. Reject the promise if the callback fails and pass the error to the promise.

Time Complexity:

The time complexity of this approach depends on the original callback-based function and its execution time.

Space Complexity:

The space complexity of this approach is O(1) as we are not using any additional data structures that grow with the input size.

:

Solution Code

import java.util.concurrent.CompletableFuture;

public class CallbackToPromiseConverter {

    public CompletableFuture<String> convertCallbackToPromise(String input, Callback callback) {
        CompletableFuture<String> future = new CompletableFuture<>();

        callback.execute(input, new CallbackHandler() {
            @Override
            public void onSuccess(String result) {
                future.complete(result);
            }

            @Override
            public void onFailure(Exception e) {
                future.completeExceptionally(e);
            }
        });

        return future;
    }
}

Try It Yourself

Loading code editor...

Frequently Asked Questions

How to solve LeetCode 2776 (Convert Callback Based Function to Promise Based Function)?

This page provides optimized solutions for LeetCode problem 2776 (Convert Callback Based Function to Promise Based Function) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2776 (Convert Callback Based Function to Promise Based Function)?

The time complexity for LeetCode 2776 (Convert Callback Based Function to Promise Based Function) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2776 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2776 in Java, C++, or Python.

Back to LeetCode Solutions