Return Length of Arguments Passed solution and explanation
9 more videos on this problem
Return Length of Arguments Passed | Leetcode 2703 | 30 Days of JavaScript #leetcode #javascriptLearn With Chirag · 3:37 · 2K views
2703. Return Length of Arguments Passed - LeetCode JavaScriptBecoming A Better Dev (with David) · 5:32 · 891 views
30 days of JavaScript - Return Length of Arguments PassedCode With Bertrand · 3:44 · 676 views
#day9 |2703 Return Length of Arguments Passed|30 Days of Javascript Leetcode-2703| 30 Days ChallengeCoding with Ankita · 3:47 · 557 views
Leetcode - Return Length of Arguments Passed JavaScriptVinhNuwin · 1:37 · 362 views
Leetcode #2703 || Return Length of Arguments Passed || Solution in JavaScriptHitesh · 0:41 · 288 views
Leetcode | Javascript Fundamentals | Rest Parameter | 2703. Return Length of Arguments PassedSoftware Interviews Prep · 5:14 · 204 views
2703. Return Length of Arguments Passed | 30 dias de JavaScript no Leetcode #09Gustavo Caetano · 5:51 · 191 views
Rest Operator | Javascript | Leetcode 2703 -Return Length of Arguments - Kanika VatsKanika Vats · 2:14 · 63 views
Leetcode challange 2703 | Return Length of Arguments PassedThat Lazy Dev · 1:26 · 62 views
The problem
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:
argsis a valid JSON array0 <= 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.
def argumentsLength(*args):
return len(args)
print(argumentsLength(5)) # Output: 1
print(argumentsLength({}, None, "3")) # Output: 3public class Solution {
public static int argumentsLength(Object... args) {
return args.length;
}
public static void main(String[] args) {
System.out.println(argumentsLength(5)); // Output: 1
System.out.println(argumentsLength(new Object(), null, "3")); // Output: 3
}
}#include <iostream>
#include <cstdarg>
int argumentsLength(int count, ...) {
return count;
}
int main() {
std::cout << argumentsLength(1, 5) << std::endl; // Output: 1
std::cout << argumentsLength(3, 0, nullptr, "3") << std::endl; // Output: 3
return 0;
}function argumentsLength(...args) {
return args.length;
}
console.log(argumentsLength(5)); // Output: 1
console.log(argumentsLength({}, null, "3")); // Output: 3;#include <stdio.h>
int argumentsLength(int count, ...) {
return count;
}
int main() {
printf("%d\n", argumentsLength(1, 5)); // Output: 1
printf("%d\n", argumentsLength(3, 0, NULL, "3")); // Output: 3
return 0;
}using System;
class Solution {
static int argumentsLength(params object[] args) {
return args.Length;
}
static void Main() {
Console.WriteLine(argumentsLength(5)); // Output: 1
Console.WriteLine(argumentsLength(new object(), null, "3")); // Output: 3
}
}Python's function definition with "*args" captures all extra arguments as a tuple, and the tuple length gives the number of arguments passed.
Time Complexity: O(1) - Calculating the tuple's length is a constant time operation.
Space Complexity: O(n) - Tied to the amount of data in the arguments tuple.
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.
def argumentsLength(*args):
return sum(1 for _ in args)
print(argumentsLength(5)) # Output: 1
print(argumentsLength({}, None, "3")) # Output: 3import java.util.Collections;
import java.util.List;
class Solution {
public static int argumentsLength(Object... args) {
List<Object> list = List.of(args);
return list.size();
}
public static void main(String[] args) {
System.out.println(argumentsLength(5)); // Output: 1
System.out.println(argumentsLength(new Object(), null, "3")); // Output: 3
}
}#include <iostream>
#include <initializer_list>
int argumentsLength(std::initializer_list<int> list) {
return list.size();
}
int main() {
std::cout << argumentsLength({5}) << std::endl; // Output: 1
std::cout << argumentsLength({{}, NULL, "3"}) << std::endl; // Output: 3
return 0;
}function argumentsLength() {
let count = 0;
for (let i = 0; i < arguments.length; i++) {
count++;
}
return count;
}
console.log(argumentsLength(5)); // Output: 1
console.log(argumentsLength({}, null, "3")); // Output: 3;#include <stdio.h>
#include <stdarg.h>
int argumentsLength(int num, ...) {
int count = num;
return count;
}
int main() {
printf("%d\n", argumentsLength(1, 5)); // Output: 1
printf("%d\n", argumentsLength(3, {}, NULL, "3")); // Output: 3
return 0;
}using System;
using System.Collections.Generic;
class Solution {
static int argumentsLength(params object[] args) {
List<object> list = new List<object>(args);
return list.Count;
}
static void Main() {
Console.WriteLine(argumentsLength(5)); // Output: 1
Console.WriteLine(argumentsLength(new object(), null, "3")); // Output: 3
}
}Here, we manually iterate over the arguments tuple using list comprehension to count each item, mimicking manual counting.
Time Complexity: O(n) - Involves iterating over the entire arguments tuple.
Space Complexity: O(1) - No extra space is required beyond counting.
Approach 3: Default Approach
function argumentsLength(...args: any[]): number {
return args.length;
}
/**
* argumentsLength(1, 2, 3); // 3
*/Complexity comparison
| Approach | Complexity |
|---|---|
| Using Built-in Methods | Time Complexity: O(1) - Calculating the tuple's length is a constant time operation. |
| Manual Count | Time Complexity: O(n) - Involves iterating over the entire arguments tuple. |
| Default Approach | – |
Complexity analysis
| 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 |
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.
