Create a class ArrayWrapper that accepts an array of integers in its constructor. This class should have two features:
+ operator, the resulting value is the sum of all the elements in both arrays.String() function is called on the instance, it will return a comma separated string surrounded by brackets. For example, [1,2,3].
Example 1:
Input: nums = [[1,2],[3,4]], operation = "Add" Output: 10 Explanation: const obj1 = new ArrayWrapper([1,2]); const obj2 = new ArrayWrapper([3,4]); obj1 + obj2; // 10
Example 2:
Input: nums = [[23,98,42,70]], operation = "String" Output: "[23,98,42,70]" Explanation: const obj = new ArrayWrapper([23,98,42,70]); String(obj); // "[23,98,42,70]"
Example 3:
Input: nums = [[],[]], operation = "Add" Output: 0 Explanation: const obj1 = new ArrayWrapper([]); const obj2 = new ArrayWrapper([]); obj1 + obj2; // 0
Constraints:
0 <= nums.length <= 10000 <= nums[i] <= 1000Note: nums is the array passed to the constructorProblem Overview: You design a wrapper class around an integer array. When two wrapper objects are added using +, the result should be the sum of all elements from both arrays. Converting the object to a string should produce the array in bracket format like [1,2,3].
Approach 1: Operator Overloading to Implement Addition and String Conversion (O(n) time, O(1) space)
The wrapper stores the input array internally and relies on language features that customize how objects behave in arithmetic and string contexts. In JavaScript, implementing valueOf() allows the runtime to convert the object into a primitive number when the + operator is used. The method simply iterates through the stored array and computes the sum using a loop or reduce. For printing, toString() returns the bracketed representation by joining elements with commas.
Each addition triggers a linear scan of the array, so the time complexity is O(n). The string conversion also requires iterating through the array to build the formatted output, again O(n). No extra data structures are needed beyond the stored array, giving O(1) auxiliary space. This approach directly leverages language-level operator behavior, making the implementation concise and idiomatic for problems involving object-oriented programming and operator overloading.
Approach 2: Interface / Method Implementation for Addition and String Conversion (O(n) time, O(1) space)
Languages like Python and Java expose explicit hooks for customizing arithmetic and string behavior. In Python, implementing __add__ defines how two objects combine when the + operator is used. The method iterates through both internal arrays (or sums their values) and returns the total. The __str__ method formats the array into the required bracket string. Java can achieve the same behavior by implementing equivalent methods or interfaces and defining custom logic for summation and printing.
The core idea remains the same: store the array once and compute its total when addition occurs. The summation requires iterating through elements, giving O(n) time, while formatting the array string also takes O(n). Memory usage stays O(1) beyond the input array. This approach is language-agnostic and highlights how operator behavior can be customized through method overrides in object-oriented programming environments.
Recommended for interviews: The operator overloading approach is the expected solution because the problem explicitly tests understanding of how objects interact with operators and string conversion. Demonstrating both numeric conversion (valueOf or __add__) and string formatting (toString or __str__) shows solid knowledge of language internals. A brute-force mindset—iterating to compute the sum—is acceptable here since the operation is inherently linear.
This approach leverages operator overloading (or language-specific methods) to define how instances of ArrayWrapper behave when used with the '+' operator and the String conversion function. We'll override/add appropriate methods to customize these operations:
ArrayWrapper instances.This Python solution defines an ArrayWrapper class. It uses the magic methods __add__ for adding two instances together and __str__ for converting an instance to its string representation. __add__ method returns the sum of the numbers in both arrays, while __str__ method returns the string representation.
Time Complexity: O(n + m), where n and m are the lengths of the two arrays in the instances being added.
Space Complexity: O(1), as we do not use any additional space.
This approach focuses on languages that support the implementation of interfaces to achieve the desired functionality. We utilize relevant interfaces or methods required to override the behavior of arithmetic operations and object-to-string conversion.
This Python approach assumes an interface-like setup using methods, providing add and to_string methods to implement addition and string conversion respectively.
Time Complexity: O(n + m), for summing elements of both arrays.
Space Complexity: O(1), no additional space required.
TypeScript
| Approach | Complexity |
|---|---|
| Operator Overloading to Implement Addition and String Conversion | Time Complexity: O(n + m), where n and m are the lengths of the two arrays in the instances being added. |
| Interface Implementation for Addition and String Conversion | Time Complexity: O(n + m), for summing elements of both arrays. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Operator Overloading with valueOf / toString | O(n) | O(1) | Best for JavaScript-style implementations where operator behavior can be customized directly. |
| Interface or Magic Method Implementation | O(n) | O(1) | Useful in Python, Java, or similar languages that define arithmetic and string behavior through special methods. |
Array Wrapper - Leetcode 2695 - JavaScript 30-Day Challenge • NeetCodeIO • 5,245 views views
Watch 9 more video solutions →Practice Array Wrapper with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor