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;
}
This article is part of the series of articles to learn Java programming language from Tech Lead Academy:1. Introduction to programming 
2. OS, File, and File System
3. Working with terminal
4. Welcome to Java Programming Language
5. Variables and Primitives in Java
6. Methods with Java
7. Java Math Operators and special operators
8. Conditional branching in Java
9. Switch statement in Java
10. Ternary operator in Java
11. Enum in Java
12. String class and its methods in Java
13. Loops in Java
14. Access modifiers in Java
15. Static keyword in Java
16. The final keyword in Java
17. Class and Object in Java
18. Object Oriented Programming in Java
19. OOP: Encapsulation in Java
20. Inheritance in Java
21. Abstraction in Java
22. Polymorphism in Java
23. Overriding vs Overloading in Java
24. OOP Design Principles in Java
25. Array in Java
26. Data Structures with Java
27. Collection framework in Java
28. ArrayList in Java
29. Set in Java
30. Map in Java
31. LocalDate in Java
32. Exception in Java
33. IO in Java
34. Design Patterns
35. Generics in Java
36. Multithreading in java
37. JUnit
38. Big O Notation for coding interviews
39. Top 17 Java coding interview questions for SDET

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