We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Create Hello World Function

Difficulty: Easy


Problem Description

Write a function createHelloWorld. It should return a new function that always returns "Hello World".


Key Insights

  • The function should return another function.
  • The returned function should not depend on any input arguments.
  • Regardless of the input, the output should always be "Hello World".

Space and Time Complexity

Time Complexity: O(1) - The operation of returning a string is constant time. Space Complexity: O(1) - Only a fixed amount of space is used to store the string.


Solution

The solution involves creating a function that, when called, returns another function. The inner function, when invoked, will simply return the string "Hello World". This approach does not require any parameters or additional data structures, as the output is constant.


Code Solutions

def createHelloWorld():
    def hello_world():
        return "Hello World"
    return hello_world
← Back to All Questions