Watch 10 video solutions for Array Wrapper, a easy level problem. This walkthrough by NeetCodeIO has 5,245 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |