I've Never Considered Time Complexity When I Try To Implement Printing Starts In Java.
#Forword
* This is my log while stduying the core concepts of Coding Test, I've attached code snippet and result snapshot below, and I've set the range to '5' for convenience.
As a beginner in Java backend development, I am feeling more challenging while doing a Coding test. Therefore, I am organizing things I've learned and trying to make a document my thoughts. I am not sure how effective this approach will be, but I'm committed to using it to organize my thoughts and sharpen my understanding of new concepts over time.
#Reasons We Need to Know Time Complexity
When resolving coding Test, the objective is to assess the problem solving skills of the interviewee. Therefore, we need to invest time in analyzing the problem's requirements. This involves considering which algorithms are suitable for the given task. Once we've decided on the appropriate algorithm, it's crucial to calculate the time complexity of the chosen algorithm before implementation—if not, you are likely to be wrong.
#Code Snippet of PrintingStars
public class PrintingStars {
public static void main(String[] args) {
for(int i = 0; i < 5; i++){ // when i = 0, i = 1, i = 2, i = 3, i = 4
for(int j = 0; j <= i; j++){ // this condition is "j <= i" , J must be less than or equal to 4
System.out.print("*");
}
System.out.println();
}
/* watch out while you do coding, if you add "System.out.println();", which we can call "Line Changer"
==> the result will be different, because it will print an additional empty line after each row of asterisks(*). */
}
}
#Result
#Time Complexity Calculation
when summing the number from 1 to n-1, we use the fomula for the sum of an arithmetic series.
1+2+3+…+N = N(N+1)/2 (==> see below.)
This can also be expressed as
N^2 1/2 + N * 1/2 (==> see below.)
when calculating Big O Notation, we need to consider the dominating operation, which in this case is N^2