Constructor methods in MATLAB - Time & Space Complexity
We want to understand how the time it takes to create an object changes as the input size grows.
How does the constructor method's work grow when we make bigger or more complex inputs?
Analyze the time complexity of the following code snippet.
classdef MyClass
properties
Data
end
methods
function obj = MyClass(inputArray)
obj.Data = zeros(size(inputArray));
for i = 1:length(inputArray)
obj.Data(i) = inputArray(i) * 2;
end
end
end
end
This constructor creates an object and fills its Data property by doubling each element of the input array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each element of the input array.
- How many times: Once for each element in the input array (n times).
As the input array gets bigger, the constructor does more work by doubling each element.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times doubling |
| 100 | About 100 times doubling |
| 1000 | About 1000 times doubling |
Pattern observation: The work grows directly with the input size; doubling the input doubles the work.
Time Complexity: O(n)
This means the time to create the object grows in a straight line with the size of the input array.
[X] Wrong: "The constructor runs in constant time no matter the input size."
[OK] Correct: Because the constructor processes each element of the input array, the time depends on how many elements there are.
Understanding how constructors scale helps you explain object creation costs clearly and confidently in real projects or interviews.
"What if the constructor used nested loops to process the input array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of constructors
Constructor methods are special functions that create objects and set their initial state.Step 2: Identify the correct purpose
Among the options, only creating an object with initial values matches the constructor's role.Final Answer:
To create an object with initial values -> Option DQuick Check:
Constructor = create object [OK]
- Confusing constructors with destructors
- Thinking constructors display properties
- Mixing constructors with inheritance
Car?Solution
Step 1: Check constructor name matches class name
The constructor must have the same name as the class, which is 'Car' with uppercase C.Step 2: Verify syntax and parameters
function obj = Car(color) obj.color = color; end correctly defines a constructor with input argument to set the color property.Final Answer:
function obj = Car(color) obj.color = color; end -> Option BQuick Check:
Constructor name = class name [OK]
- Using lowercase class name in constructor
- Swapping function and variable names
- Missing input arguments for initialization
classdef Book
properties
Title
Author
end
methods
function obj = Book(title, author)
obj.Title = title;
obj.Author = author;
end
end
end
b = Book('1984', 'Orwell');
disp(b.Title);What will be the output when this code runs?
Solution
Step 1: Understand object creation with constructor
The constructor sets Title to '1984' and Author to 'Orwell' when creating object b.Step 2: Check the displayed property
The code displays b.Title, which was set to '1984'.Final Answer:
1984 -> Option CQuick Check:
Displayed Title = '1984' [OK]
- Confusing Title and Author properties
- Expecting object display instead of property
- Assuming error due to missing arguments
classdef Person
properties
Name
end
methods
function obj = Person(name)
Name = name;
end
end
endSolution
Step 1: Check property assignment in constructor
The code assigns 'name' to a variable 'Name' but not to obj.Name, so the object property remains empty.Step 2: Identify correct assignment syntax
Correct syntax is obj.Name = name; to assign the input to the object's property.Final Answer:
The property Name is not assigned to the object -> Option AQuick Check:
Assign to obj.Name, not just Name [OK]
- Assigning to local variable instead of object property
- Ignoring case sensitivity in property names
- Forgetting to return obj from constructor
Rectangle with properties Length and Width. Write a constructor that sets these properties and also calculates the Area property automatically. Which constructor method is correct?Solution
Step 1: Assign Length and Width to object properties
function obj = Rectangle(length, width) obj.Length = length; obj.Width = width; obj.Area = length * width; end correctly assigns length and width to obj.Length and obj.Width.Step 2: Calculate Area as product and assign to obj.Area
function obj = Rectangle(length, width) obj.Length = length; obj.Width = width; obj.Area = length * width; end calculates area as length * width and assigns it to obj.Area, which is correct.Final Answer:
function obj = Rectangle(length, width) obj.Length = length; obj.Width = width; obj.Area = length * width; end -> Option AQuick Check:
Assign all properties to obj with correct calculation [OK]
- Assigning Area to local variable instead of obj.Area
- Adding Length and Width instead of multiplying for Area
- Not assigning Length and Width to obj
Ready to prove it?
Pass the test and this pattern moves unknown
