Watch 10 video solutions for Return Length of Arguments Passed, a easy level problem. This walkthrough by Learn With Chirag has 2,324 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |