Skip to main content

Create Hello World Function - Solution & Explanation

Easy7 min readAsked at: Amazon, Microsoft, Meta +6
Practice this problem

Problem Statement

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

 

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

Approach Overview

Problem Overview: You need to implement a function that returns another function. When the returned function is executed, it must return the string "Hello World". The task focuses on understanding basic function construction and how returned functions behave.

Approach 1: Closure Approach (O(1) time, O(1) space)

This method returns an inner function that produces the string "Hello World" when called. The outer function creates the inner function and returns it as a closure. Closures allow a function to capture variables from its surrounding scope, although in this case no external variable is required. The key operation is returning a function reference rather than executing it immediately. This pattern appears frequently in JavaScript and functional programming tasks involving callbacks, factories, and higher‑order functions. Related concepts appear in closures and functions.

Approach 2: Direct Return Approach (O(1) time, O(1) space)

The direct return method simply returns a function whose body returns "Hello World". Instead of explicitly constructing additional logic, the function immediately defines and returns the inner function. The implementation is minimal: define a function literal and return the constant string from it. This approach works in most languages that support returning functions or lambdas. It demonstrates how higher‑order functions operate in languages like JavaScript and Python.

Recommended for interviews: Interviewers expect the direct return approach because it clearly demonstrates that you understand higher‑order functions and returning callable objects. The closure approach still shows useful knowledge about function scope and closures, which often appear in callback patterns and functional design. Both solutions run in constant time and space, but writing the minimal version shows clean thinking and familiarity with function syntax.

Approach 1: Closure Approach

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.

In C, we use a function createHelloWorld that simply returns the string "Hello World". We then define a function pointer type hello_function to point to such functions and return it through another function getHelloWorldFunction.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Direct Return Approach

This approach directly returns a function without using intermediate variables like closures. The returned function will always output 'Hello World', ignoring any provided arguments.

In C, we create a simple function returnHelloWorld that directly returns the string "Hello World". This can be used in place of closures as C lacks native closure support.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1). The function executes a single return instruction.
Space Complexity: O(1) as there are no dynamic allocations.

Try this approach in the editor →

Approach 3: Default Approach

Code

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Closure Approach

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.

Direct Return Approach

Time Complexity: O(1). The function executes a single return instruction.
Space Complexity: O(1) as there are no dynamic allocations.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Closure ApproachO(1)O(1)When demonstrating closures or higher‑order function behavior
Direct Return ApproachO(1)O(1)Preferred minimal solution when a simple function return is required

Video Solution

Create Hello World Function (Closure - Day1) - Leetcode 2667 - JavaScript 30-Day ChallengeNeetCodeIO130,461 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Create Hello World Function easy or hard?
Create Hello World Function is categorized as an Easy problem with a high acceptance rate around 80%+. It mainly tests familiarity with returning functions and understanding basic closure behavior.
Create Hello World Function Python/Java solution
In Python, return a nested function or lambda that returns "Hello World". In Java, C++, or C, you typically return a function pointer or implement a function that directly returns the string. JavaScript commonly uses closures to return the function.
How to solve Create Hello World Function in O(1)?
Return a function that directly returns the string "Hello World". No loops, data structures, or extra computation are needed. Creating and returning the function takes constant time and constant space.
What is the best approach for Create Hello World Function?
The direct return approach is the most straightforward solution. Define a function that returns another function whose body returns the string "Hello World". This solution runs in O(1) time and O(1) space and clearly demonstrates understanding of higher‑order functions.
Is Create Hello World Function asked at Google/Amazon/Meta?
This problem is more of a warm‑up question used on platforms like LeetCode to introduce closures and higher‑order functions. It helps candidates become comfortable with returning functions before tackling more complex functional programming problems.
What data structure is used in Create Hello World Function?
No specific data structure is required. The problem focuses on function behavior, particularly higher‑order functions and closures, rather than arrays, hash maps, or trees.
What is the time complexity of Create Hello World Function?
Both common implementations run in O(1) time because the function simply returns another function without iterating over data. Space complexity is also O(1) since only a constant amount of memory is used to store the returned function.

Ready to solve this problem?

Practice Create Hello World Function with our built-in code editor and test cases.

Practice on FleetCode