




Sponsored
Sponsored
This approach uses the concept of closures to return a function that ignores its arguments and always returns the string 'Hello World'. In this method, createHelloWorld returns a new function that consistently returns the string when invoked.
Time Complexity: O(1) as the function performs a constant-time operation.
Space Complexity: O(1) since it doesn't use any extra space dependent on input size.
1
In Java, we utilize the Supplier functional interface to return a lambda expression () -> "Hello World". The lambda does not capture any variables and simply returns the static string.
This approach directly returns a function without using intermediate variables like closures. The returned function will always output 'Hello World', ignoring any provided arguments.
Time Complexity: O(1). The function executes a single return instruction.
Space Complexity: O(1) as there are no dynamic allocations.
1def hello_world_function():
2    return "Hello World"
3
4def create_hello_world():
5    return hello_world_functionIn Python, we define hello_world_function that returns "Hello World". This function is returned directly by create_hello_world, thereby leveraging Python's first-class functions.