Skip to main content

Array Wrapper - Solution & Explanation

Easy10 min read
Practice this problem

Problem Statement

Create a class ArrayWrapper that accepts an array of integers in its constructor. This class should have two features:

  • When two instances of this class are added together with the + operator, the resulting value is the sum of all the elements in both arrays.
  • When the 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 <= 1000
  • 0 <= nums[i] <= 1000
  • Note: nums is the array passed to the constructor

Approach Overview

Problem 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 1: Operator Overloading to Implement Addition and String Conversion

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:

  • Addition: We'll implement the addition operation for the '+' operator to sum the elements in two ArrayWrapper instances.
  • String Conversion: We'll override the method responsible for converting an object to a string, to provide the correct format.

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.

Code

Python

JavaScript

C#

Java

C++

C

Complexity

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.

Try this approach in the editor →

Approach 2: Interface Implementation for Addition and String Conversion

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.

Code

Python

Java

Complexity

Time Complexity: O(n + m), for summing elements of both arrays.
Space Complexity: O(1), no additional space required.

Try this approach in the editor →

Approach 3: Default Approach

Code

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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.
Space Complexity: O(1), as we do not use any additional space.

Interface Implementation for Addition and String Conversion

Time Complexity: O(n + m), for summing elements of both arrays.
Space Complexity: O(1), no additional space required.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Operator Overloading with valueOf / toStringO(n)O(1)Best for JavaScript-style implementations where operator behavior can be customized directly.
Interface or Magic Method ImplementationO(n)O(1)Useful in Python, Java, or similar languages that define arithmetic and string behavior through special methods.

Video Solution

Array Wrapper - Leetcode 2695 - JavaScript 30-Day Challenge • NeetCodeIO • 5,310 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Array Wrapper easy or hard?
Array Wrapper is categorized as an Easy problem. The challenge lies in understanding how programming languages allow objects to override numeric and string operations rather than designing complex algorithms.
Array Wrapper Python/Java solution
In Python, implement __add__ to control addition and __str__ for printing the array format. In Java, similar behavior can be implemented through custom methods or interfaces that compute sums and return formatted strings. Both approaches iterate through the array, resulting in O(n) time complexity.
How to solve Array Wrapper in O(n)?
Store the array inside a wrapper class and override numeric conversion behavior. When two objects are added, iterate through the stored elements to compute their sums and return the combined value. Implement a string conversion method that joins array elements into the format [a,b,c].
What is the best approach for Array Wrapper?
The operator overloading approach is the most common solution. Implement methods such as valueOf (JavaScript) or __add__ (Python) to define how wrapper objects behave when added, and toString or __str__ for formatting. This keeps the implementation concise and matches the problem's design goal of customizing operator behavior.
Is Array Wrapper asked at Google/Amazon/Meta?
Array Wrapper is mainly a concept-check problem focused on operator overloading and object behavior. While not commonly reported as a direct Google or Meta interview question, the underlying concepts appear in object-oriented design and language feature questions.
What data structure is used in Array Wrapper?
The primary data structure is a simple integer array stored inside a wrapper class. The problem focuses less on advanced data structures and more on object-oriented techniques such as method overriding and operator customization.
What is the time complexity of Array Wrapper?
The time complexity is O(n) because computing the sum of the wrapped array requires iterating through its elements. String conversion also takes O(n) since each element must be included in the formatted output. Space complexity is O(1) beyond storing the original array.

Ready to solve this problem?

Practice Array Wrapper with our built-in code editor and test cases.

Practice on FleetCode