Create Hello World Function
Problem Statement​
Problem Description​
Write a function createHelloWorld
. It should return a new function that always returns "Hello World"
.
Examples​
Example 1:
Input: args = []
Output: "Hello World"
Explanation:
const f = createHelloWorld();
f(); // "Hello World"
The function returned by createHelloWorld should always return "Hello World".
Example 2:
Input: args = [{},null,42]
Output: "Hello World"
Explanation:
const f = createHelloWorld();
f({}, null, 42); // "Hello World"
Any arguments could be passed to the function but it should still always return "Hello World".
Constraints​
0 <= args.length <= 10
Solution of Given Problem​
Intuition and Approach​
The function createHelloWorld
should return a new function that always returns the string "Hello World"
. The returned function can accept any number of arguments, but it should ignore them and always return "Hello World"
.
Approaches​
The approach involves defining the function createHelloWorld
which returns another function that ignores its arguments and returns "Hello World"
.
Codes in Different Languages​
- JavaScript
- TypeScript
- Python
- Java
- C++
/**
* @return {Function}
*/
var createHelloWorld = function() {
return function(...args) {
return "Hello World";
}
};
function createHelloWorld(): (...args: any[]) => string {
return function(...args: any[]) {
return "Hello World";
}
}
def create_hello_world():
def hello_world(*args):
return "Hello World"
return hello_world
import java.util.concurrent.Callable;
public class HelloWorldFactory {
public static Callable<String> createHelloWorld() {
return () -> "Hello World";
}
}
#include <functional>
#include <string>
std::function<std::string()> create_hello_world() {
return []() { return "Hello World"; };
}
Complexity Analysis​
-
Time Complexity: O(1), which means it runs in constant time. This is because the function simply returns a new function, without performing any significant computations or iterations.
-
Space Complexity: O(1), which means it uses a constant amount of memory. This is because the function only returns a small, fixed-size function, without allocating any additional memory that scales with the input size.