Skip to content
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6d1817b
Finished timesTable time complexity
Oct 17, 2024
49d02c4
Finished printLetters time complexity
Oct 17, 2024
15abfb0
Finished isBanned time complexity and changed comments a bit
Oct 17, 2024
548b2b8
Finished computeProduct time complexity
Oct 17, 2024
4833efd
Finished describeProduct time complexity
Oct 17, 2024
b0ad609
Finished computeFactorial time complexity
Oct 17, 2024
7cb4d65
Finished computeAllFactorials time complexity
Oct 17, 2024
89b6d76
Finished checkIfContainedArrayList time complexity
Oct 17, 2024
d091562
Finished containsOverlap time complexity
Oct 17, 2024
e6ff94a
Finished containsOverlap2 time complexity
Oct 17, 2024
e5949a8
Finished printCharacters time complexity and added clarification to t…
Oct 17, 2024
91ddaf7
Finished computeAverage time complexity
Oct 17, 2024
79a729a
Finished checkIfContainedHashSet time complexity
Oct 17, 2024
b2dc305
Looked at class notes again, and added a more descriptive explanation…
Oct 18, 2024
1c23ceb
Finished emailLookup time complexity
Oct 18, 2024
1a4847e
Finished the method emailLookupEfficient and figured out the time com…
Oct 18, 2024
2a1c15d
Finished hasCommon time complexity
Oct 18, 2024
e1c684a
Added a for loop to iterate through
Oct 18, 2024
da6af3a
Added an if statement to hasCommonEfficient method
Oct 18, 2024
a6baf71
Finished up the method hasCommonEfficient and flipped the order of Ar…
Oct 18, 2024
bc00c15
Finished first question for data structure choice and chose a HashMap
Oct 18, 2024
533bb7d
Finished second question for data structure choice and chose an Array…
Oct 18, 2024
ab1708f
Finished last question for data structure choice and chose a LinkedList
Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class Main {

// The time complexity is:
// YOUR ANSWER HERE
// O(N^2) -> n*n = n^2
public static void timesTable(int x) {
for(int i = 1; i <= x; i++) {
for(int j = 1; j <= x; j++) {
Expand All @@ -17,7 +17,7 @@ public static void timesTable(int x) {
}

// The time complexity is:
// YOUR ANSWER HERE
// O(N) -> n = length of the word
public static void printLetters(String word) {
char[] letters = word.toCharArray();

Expand All @@ -27,7 +27,7 @@ public static void printLetters(String word) {
}

// The time complexity is:
// YOUR ANSWER HERE
// O(1) -> n = a fixed number of banned passwords
public static boolean isBanned(String password) {
String[] bannedPasswords = {"password", "hello", "qwerty"};
boolean banned = false;
Expand All @@ -41,7 +41,7 @@ public static boolean isBanned(String password) {


// The time complexity is:
// YOUR ANSWER HERE
// O(N) -> n = the length of the int[] array
public static int computeProduct(int[] nums) {
int total = 1;
for(int num : nums) {
Expand All @@ -51,7 +51,7 @@ public static int computeProduct(int[] nums) {
}

// The time complexity is:
// YOUR ANSWER HERE
// O(N) -> n = the length of the int[] array
public static void describeProduct(int[] nums) {
System.out.println("About to compute the product of the array...");
int product = computeProduct(nums);
Expand All @@ -60,7 +60,7 @@ public static void describeProduct(int[] nums) {


// The time complexity is:
// YOUR ANSWER HERE
// O(N) -> simplified to n due to for loop complexity
public static int computeFactorial(int n) {
int result = 1;
for(int i = 1; i <= n; i++) {
Expand All @@ -71,6 +71,7 @@ public static int computeFactorial(int n) {

// Assume that the largest number is no bigger than the length
// of the array
// O(N^2) -> n*n = n^2, double loops
public static void computeAllFactorials(int[] nums) {
for(int num : nums) {
int result = computeFactorial(num);
Expand All @@ -80,7 +81,7 @@ public static void computeAllFactorials(int[] nums) {


// The time complexity is:
// YOUR ANSWER HERE
// O(N) -> n = size of the ArrayList
public static void checkIfContainedArrayList(ArrayList<String> arr, String target) {
if (arr.contains(target)) {
System.out.println(target + " is present in the list");
Expand All @@ -92,7 +93,7 @@ public static void checkIfContainedArrayList(ArrayList<String> arr, String targe

// assume n = wordsA.length = wordsB.length
// The time complexity is:
// YOUR ANSWER HERE
// O(N^2) -> n*n = n^2
public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
for(String wordA : wordsA) {
for(String wordB : wordsB) {
Expand All @@ -105,7 +106,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
}

// The time complexity is:
// YOUR ANSWER HERE
// O(N) -> n = length of wordsA
public static boolean containsOverlap2(String[] wordsA, String[] wordsB) {
Set<String> wordsSet = new HashSet<>();
for(String word : wordsA) {
Expand All @@ -122,20 +123,20 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) {
}

// The time complexity is:
// YOUR ANSWER HERE
// O(N) -> n = the length of the chars[] array
public static void printCharacters(char[] chars) {
for (int i = 0; i < chars.length; i++) {
char character = chars[i];
System.out.println("The character at index " + i + " is " + character);
}
}
// The time complexity is:
// YOUR ANSWER HERE
// O(1) -> constant
public static double computeAverage(double a, double b) {
return (a + b) / 2.0;
}
// The time complexity is:
// YOUR ANSWER HERE
// O(1) -> simplifies and an average case for HashSet
public static void checkIfContainedHashSet(HashSet<String> set, String target)
{
if (set.contains(target)) {
Expand All @@ -150,7 +151,7 @@ public static void checkIfContainedHashSet(HashSet<String> set, String target)
// A queryName is given, and this method returns the corresponding email if it is found
// Otherwise, it returns "Person not found"
// What is the time complexity of this method?
// YOUR ANSWER HERE
// O(N) -> n = length of the names array
public static String emailLookup(String[] names, String[] emails, String queryName) {
for(int i = 0; i < names.length; i++) {
if (names[i].equals(queryName)) {
Expand All @@ -165,14 +166,14 @@ public static String emailLookup(String[] names, String[] emails, String queryNa
// keys are names and the values are emails.
// Write this method to efficiently return the corresponding email or "Person not found" if appropriate
// What is the time complexity of your solution?
// YOUR ANSWER HERE
// O(1) -> average case for HashMap
public static String emailLookupEfficient(HashMap<String, String> namesToEmails, String queryName) {
return null;
return namesToEmails.getOrDefault(queryName, "Person not found");
}

// What is the time complexity of this method?
// (assume the set and list have the same number of elements)
// YOUR ANSWER HERE
// O(N^2) -> n*n = n^2
public static boolean hasCommon(HashSet<String> wordSet, ArrayList<String> wordList) {
for(String word : wordSet) {
if(wordList.contains(word)) {
Expand All @@ -184,8 +185,13 @@ public static boolean hasCommon(HashSet<String> wordSet, ArrayList<String> wordL
// Rewrite hasCommon so it does the same thing as hasCommon, but with a better time complexity.
// Do not change the datatype of wordSet or wordList.
// What is the time complexity of your new solution?
// YOUR ANSWER HERE
// O(N) -> flipped the order of the ArrayList and HashSet to improve time complexity
public static boolean hasCommonEfficient(HashSet<String> wordSet, ArrayList<String> wordList) {
for (String word: wordList) {
if(wordSet.contains(word)) {
return true;
}
}
return false;
}

Expand All @@ -194,20 +200,23 @@ public static boolean hasCommonEfficient(HashSet<String> wordSet, ArrayList<Stri
// The prices will be updated frequently throughout the day, and you need to efficiently update
// and access the current price for each stock. The order of the ticker symbols is not important.
// What would be a good choice of data structure?
// YOUR ANSWER HERE
// I would use a HashMap since it's efficient for frequent updates and provides an easy lookups using stock's ticket symbol as the key.
// The average time complexity for HashMap is O(1), so it's fast.

// Suppose you are building a music player application where users can create playlists.
// Songs can be added to the end of the playlist in the order the user chooses, and the user can
// skip to the next or previous song. Most operations involve adding songs and accessing them by
// their position in the playlist.
// What would be a good choice of data structure?
// YOUR ANSWER HERE
// For this scenario, I would use an ArrayList because we need to be able to access the items by an index and maintain the order.
// The average time complexity for ArrayList is O(1) or O(N) which is not slow.

// Suppose you are developing a search feature that keeps track of the user's
// recent search queries. You want to store the queries in the order they were made,
// so you can display them to the user for quick access. The number of recent searches is
// relatively small, and it is more important to preserve the order of the searches than
// to optimize for fast lookups or deletions.
// What would be a good choice of data structure?
// YOUR ANSWER HERE
// I would use a LinkedList because it has an order of insertion, which is significant in this situation.
// It allows to insert new queries at the end of the list, while maintaining the order.
}