Big O Notation for Interviews

Photo by Veri Ivanova on Unsplash

Why not just measure the runtime of the programs?

public static void print1000() {
Instant start = Instant.now();
for(int i = 0; i < 1000; i++) {
System.out.println("Hello, World");
}
Instant finish = Instant.now();
long timeElapsed = Duration.between(start, finish).toMillis();
System.out.println("Milliseconds: " + timeElapsed);
}

Big O notation measurement is based on input

public void printFirstElement(String[] str) {
System.out.println(str[0]);
}
public void iterateOver(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
public boolean isSevenThere(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == 7) {
return true;
}
}
return false;
}
public void sayHello(int num) {
for (int i = 0; i < num; i++) {
System.out.println("Hello");
}
for (int i = 0; i < num; i++) {
System.out.println("Hi");
}
}
public void sayHello(int num) {
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
for (int i = 0; i < num; i++) {
System.out.println("Hello");
}
}
public void printStr(String str) {
for (int i = 0; i < 100; i++) {
System.out.println(str);
}
}
public boolean isSumTarget(int numArr[], int target) {
for(int i = 0; i < numArr.length; i++) {
for(int j = i + 1; j < numArr.length; j++) {
if(numArr[i] + numArr[j] == target) {
return true;
}
}
}

return false;
}

Software Developer, Java Instructor https://www.techleadacademy.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store