Watch 9 video solutions for Calculator with Method Chaining, a easy level problem. This walkthrough by Learn With Chirag has 1,238 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Design a Calculator class. The class should provide the mathematical operations of addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining. The Calculator class constructor should accept a number which serves as the initial value of result.
Your Calculator class should have the following methods:
add - This method adds the given number value to the result and returns the updated Calculator.subtract - This method subtracts the given number value from the result and returns the updated Calculator.multiply - This method multiplies the result by the given number value and returns the updated Calculator.divide - This method divides the result by the given number value and returns the updated Calculator. If the passed value is 0, an error "Division by zero is not allowed" should be thrown.power - This method raises the result to the power of the given number value and returns the updated Calculator.getResult - This method returns the result.Solutions within 10-5 of the actual result are considered correct.
Example 1:
Input: actions = ["Calculator", "add", "subtract", "getResult"], values = [10, 5, 7] Output: 8 Explanation: new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
Example 2:
Input: actions = ["Calculator", "multiply", "power", "getResult"], values = [2, 5, 2] Output: 100 Explanation: new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
Example 3:
Input: actions = ["Calculator", "divide", "getResult"], values = [20, 0] Output: "Division by zero is not allowed" Explanation: new Calculator(20).divide(0).getResult() // 20 / 0 The error should be thrown because we cannot divide by zero.
Constraints:
actions is a valid JSON array of stringsvalues is a valid JSON array of numbers2 <= actions.length <= 2 * 1041 <= values.length <= 2 * 104 - 1actions[i] is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"Problem Overview: Design a calculator object that supports chained arithmetic operations such as add(), subtract(), multiply(), and divide(). Each method updates an internal value and returns the calculator instance so the next operation can be called in the same expression.
Approach 1: Class Design with Method Chaining (O(1) time per operation, O(1) space)
This approach uses a simple class that stores the current numeric value as internal state. Each arithmetic method modifies that value and then returns self (Python) or this (Java). Returning the same object enables chaining: calc.add(5).multiply(2).subtract(3). Internally, each operation performs a constant-time arithmetic update, so the time complexity per call is O(1) and the class only stores one number, giving O(1) space. This pattern is common in object-oriented design and relies on the concept of returning the current instance from methods.
The key insight is that the calculator never creates new objects for each step. Instead, it mutates the stored value and returns the same instance. This keeps the implementation small and efficient while still providing a clean API. Division requires one extra check: if the divisor is zero, throw an exception to prevent invalid state.
Approach 2: Fluent Interface with Error Handling (O(1) time per operation, O(1) space)
A more structured variation follows the fluent interface design pattern. The class still stores the current value, but each method explicitly validates inputs and handles error cases. For example, divide() checks for zero and throws an exception, while other operations simply update the stored value. After validation and calculation, the method returns this so the chain continues.
This approach is common in JavaScript and C# APIs where fluent method calls improve readability and developer ergonomics. The implementation still performs constant-time arithmetic operations and uses constant memory. The difference lies in defensive checks and clearer API design, which is important when building reusable libraries or interview-quality class structures. The design also demonstrates understanding of class design and object-oriented programming patterns.
Recommended for interviews: The standard class with method chaining is what interviewers expect. It shows you understand how returning the current object enables chained calls and how to maintain internal state cleanly. Mentioning the fluent interface pattern and adding divide-by-zero validation demonstrates stronger API design thinking. The brute implementation proves you understand chaining mechanics, while the polished version shows production-level design awareness.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Class Design with Method Chaining | O(1) per operation | O(1) | Standard interview solution demonstrating method chaining using self/this |
| Fluent Interface with Error Handling | O(1) per operation | O(1) | When building production-style APIs with validation such as divide-by-zero checks |