Create Hello World Function
Asked at Microsoft
Difficulty
Easy
Time
O(1)
Space
O(1)
Pattern
—
Companies
1
Problem
Write a function createHelloWorld. It does not return the greeting itself. Instead, it returns a new function, and that returned function always gives back the string "Hello World".
The returned function must accept any number of arguments of any type. It simply ignores them and returns the same string every time. So calling the returned function with no arguments, or with three arguments, or with an object and a number, all produce "Hello World".
The point of the task is to show that functions are values: you can create one inside another function and hand it back to the caller.
Two separate calls
Constraints
- ●
0 <= args.length <= 10 - ●
The returned function must accept any argument types - ●
The returned string must be exactly "Hello World" (capital H, capital W, one space)
Examples
args = []"Hello World"args = [{},null,42]"Hello World"Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Microsoft | Easy | View all Microsoft questions → |
How to Think About It
The simplest approach: define a function that returns the string "Hello World". No parameters, no state. Every call returns the same value. O(1) time and space.
In JavaScript, use a closure: function createHelloWorld() { return function(...args) { return "Hello World"; }; }. The inner function closes over nothing but still works because the string is a literal.
In Python: def createHelloWorld(): return lambda *args: "Hello World". The lambda is a closure that captures nothing from the outer scope - it just returns a constant.
Why this question exists: interviewers want to see if you understand first-class functions, closures, and the difference between defining a function and calling it. Returning a function vs returning the result of a function.
Edge cases: ensure the function works regardless of the number or type of arguments passed. The function should always return the exact string "Hello World" (capital H, capital W, space in between).
The trick is that the inner function accepts arguments (via *args or ...args) but ignores them. The signature requires it to be callable with any arguments, but the return value is always "Hello World".
createHelloWorld()() → "Hello World"
createHelloWorld()(1, 2, 3) → "Hello World"
createHelloWorld()("anything") → "Hello World"
Optimal Approach
Step 1: Define an outer function createHelloWorld.
Step 2: Inside it, define and return an inner function that takes any number of arguments.
Step 3: The inner function simply returns the string "Hello World".
The key concept is closures: the inner function is a first-class value that can be returned and later called. The returned function ignores all arguments.
Walkthrough:fn = createHelloWorld() - creates the closure, returns the inner functionfn() → "Hello World"fn(1, 2, 3) → "Hello World"fn("any", "args") → "Hello World"
Time: O(1). Space: O(1).
Outer function returns inner function
What Trips People Up in Real Interviews
Returning "Hello World" directly instead of returning a function. The question asks you to create a function that returns another function. createHelloWorld() should return a callable, not a string.
Not handling extra arguments. The inner function must accept any number of arguments (even if it ignores them). Use *args in Python or ...args in JavaScript.
Overcomplicating with a class or closure state. This question is intentionally simple - just return a function that returns a constant string.
Forgetting the exact string format. It must be "Hello World" with capital H and W. Not "hello world" or "Hello world".
Confusing this with a class-based design pattern. While you could use a class with __call__, the simplest solution is a nested function or lambda.
Solution Code
O(1) timedef createHelloWorld():
return lambda *args: "Hello World"Frequently Asked Questions
What is the Create Hello World Function problem?
Create a function that returns "Hello World" every time it is called. This is a fundamental closure and function-scoping question used in early rounds to test your understanding of how functions capture their environment.
How do you solve Create Hello World Function?
The optimal approach runs in O(1) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Create Hello World Function?
Create Hello World Function is asked at Microsoft. It is a easy difficulty problem.
What are common mistakes on Create Hello World Function?
- Returning "Hello World" directly instead of returning a function. The question asks you to create a function that returns another function. `createHelloWorld()` should return a callable, not a string.
- Not handling extra arguments. The inner function must accept any number of arguments (even if it ignores them). Use `*args` in Python or `...args` in JavaScript.
- Overcomplicating with a class or closure state. This question is intentionally simple - just return a function that returns a constant string.
- Forgetting the exact string format. It must be "Hello World" with capital H and W. Not "hello world" or "Hello world".
- Confusing this with a class-based design pattern. While you could use a class with `__call__`, the simplest solution is a nested function or lambda.