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 array0 <= args.length <= 100Problem 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.
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.
Time Complexity: O(1) - Consistently returns a given number.
Space Complexity: O(1) - No additional space needed beyond the function's internal variables.
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.
Time Complexity: O(1) - Due to usage constraints.
Space Complexity: O(1) - No additional space is used.
| Approach | Complexity |
|---|---|
| Using Built-in Methods | Time Complexity: O(1) - Consistently returns a given number. |
| Manual Count | Time Complexity: O(1) - Due to usage constraints. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Built-in Length Property | O(1) | O(1) | Best approach in modern languages where argument collections expose a length or size property |
| Manual Counting via Iteration | O(n) | O(1) | Useful when demonstrating iteration logic or when a container does not provide a built-in size |
Return Length of Arguments Passed | Leetcode 2703 | 30 Days of JavaScript #leetcode #javascript • Learn With Chirag • 2,324 views views
Watch 9 more video solutions →Practice Return Length of Arguments Passed with our built-in code editor and test cases.
Practice on FleetCode