Skip to main content

Return Length of Arguments Passed - Solution & Explanation

Easy8 min readAsked at: Amazon, Google, TCS +1
Practice this problem

Problem Statement

Write a function argumentsLength that returns the count of arguments passed to it.

 

Example 1:

Input: args = [5]
Output: 1
Explanation:
argumentsLength(5); // 1

One value was passed to the function so it should return 1.

Example 2:

Input: args = [{}, null, "3"]
Output: 3
Explanation: 
argumentsLength({}, null, "3"); // 3

Three values were passed to the function so it should return 3.

 

Constraints:

  • args is a valid JSON array
  • 0 <= args.length <= 100

Approach Overview

Problem Overview: You receive a function that accepts a variable number of arguments. The task is simple: return how many arguments were passed during the function call. If the function receives (1, 2, 3), the result should be 3. The challenge mainly tests your understanding of how programming languages represent function arguments.

Approach 1: Using Built-in Methods (Time: O(1), Space: O(1))

The most direct solution uses the built-in property that stores the number of arguments. Many languages expose this through constructs like args.length, len(args), or equivalent mechanisms. The key insight is that argument collections are typically implemented as arrays or array-like structures, which already track their size internally. Accessing this size is a constant-time operation because the length value is stored alongside the data structure rather than computed dynamically. This approach is concise, readable, and the most idiomatic way to solve the problem in languages such as JavaScript, Python, Java, and C#.

Under the hood, the function gathers all arguments into a container (often a rest parameter or variadic array). Returning the stored size avoids iteration entirely. Because of this, the runtime remains O(1) and memory usage stays O(1). If you're working with concepts like functions or variable arguments, this pattern appears frequently.

Approach 2: Manual Count (Time: O(n), Space: O(1))

A more explicit solution iterates through the argument collection and counts each element manually. Initialize a counter at zero, loop through every argument, and increment the counter for each element encountered. This approach mirrors how you would determine the size of a container if no length property existed.

The algorithm performs a single traversal across the argument list, giving it a time complexity of O(n), where n is the number of arguments passed to the function. Space usage remains O(1) because only a single counter variable is maintained. While this method is less efficient than using a built-in length property, it demonstrates fundamental iteration logic and works in environments where argument metadata is not directly accessible. The technique resembles standard counting patterns used with arrays or simple data collections.

Recommended for interviews: Interviewers expect the built-in length approach because it reflects familiarity with language features and produces the optimal O(1) solution. Showing the manual counting method first can demonstrate understanding of basic iteration, but the built-in property is the cleanest and most efficient implementation for real-world code.

Approach 1: Using Built-in Methods

This approach leverages built-in properties or methods provided by programming languages to determine the number of arguments passed to a function. These features make it simple to count the number of arguments dynamically.

In C, we use the variadic function capability from the standard library. We define a function that takes at least one parameter, which will be the count of the arguments. The function simply returns this count.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) - Consistently returns a given number.
Space Complexity: O(1) - No additional space needed beyond the function's internal variables.

Try this approach in the editor →

Approach 2: Manual Count

This approach involves counting the arguments using a loop or other constructs to manually track the number of parameters. Although less commonly needed in modern languages due to built-in functionality, it's a potential way to understand argument storage deeply.

In C, leveraging variadic functions allows us to define a manual counting approach, but here we focus on providing the number directly or hardware constraints might require manual counting.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) - Due to usage constraints.
Space Complexity: O(1) - No additional space is used.

Try this approach in the editor →

Approach 3: Default Approach

Code

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Built-in Methods

Time Complexity: O(1) - Consistently returns a given number.
Space Complexity: O(1) - No additional space needed beyond the function's internal variables.

Manual Count

Time Complexity: O(1) - Due to usage constraints.
Space Complexity: O(1) - No additional space is used.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Using Built-in Length PropertyO(1)O(1)Best approach in modern languages where argument collections expose a length or size property
Manual Counting via IterationO(n)O(1)Useful when demonstrating iteration logic or when a container does not provide a built-in size

Video Solution

Return Length of Arguments Passed | Leetcode 2703 | 30 Days of JavaScript #leetcode #javascript • Learn With Chirag • 2,440 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Return Length of Arguments Passed easy or hard?
Return Length of Arguments Passed is classified as an Easy problem. It focuses on understanding variadic arguments and accessing the size of the argument list rather than applying complex algorithms or data structures.
Return Length of Arguments Passed Python/Java solution
In Python, the function receives *args and returns len(args). In Java, variadic parameters like int... args are stored in an array, so returning args.length provides the answer. Both implementations run in O(1) time and O(1) space.
How to solve Return Length of Arguments Passed in O(1)?
Use the built-in property that stores the number of arguments passed to the function. In many implementations this is accessed as args.length or an equivalent size method. Because the value is stored internally, retrieving it does not require iterating through the arguments.
What is the best approach for Return Length of Arguments Passed?
The best approach uses the built-in length property of the argument collection, such as args.length or len(args). This retrieves the stored size of the argument container in constant time. The solution runs in O(1) time and O(1) space and is the most idiomatic implementation across languages.
Is Return Length of Arguments Passed asked at Google/Amazon/Meta?
Problems like this appear more often in beginner coding assessments and language fundamentals exercises rather than advanced big-tech interviews. The goal is verifying understanding of function arguments, variadic parameters, and basic data structures used to store them.
What data structure is used in Return Length of Arguments Passed?
The arguments are typically stored in an array or array-like structure created by variadic or rest parameters. This structure maintains a length or size field, allowing constant-time access to the number of elements.
What is the time complexity of Return Length of Arguments Passed?
Using the built-in length or size property gives O(1) time complexity because the count is stored with the argument container. A manual counting approach that iterates through each argument takes O(n) time where n is the number of arguments. Both approaches use O(1) additional space.

Ready to solve this problem?

Practice Return Length of Arguments Passed with our built-in code editor and test cases.

Practice on FleetCode