Skip to content
Open
Changes from all commits
Commits
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
45 changes: 32 additions & 13 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class Main {

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

// The time complexity is:
// The time complexity is: n
// YOUR ANSWER HERE
public static void printLetters(String word) {
char[] letters = word.toCharArray();
Expand All @@ -26,7 +26,7 @@ public static void printLetters(String word) {
}
}

// The time complexity is:
// The time complexity is: n
// YOUR ANSWER HERE
public static boolean isBanned(String password) {
String[] bannedPasswords = {"password", "hello", "qwerty"};
Expand All @@ -40,7 +40,7 @@ public static boolean isBanned(String password) {
}


// The time complexity is:
// The time complexity is: n
// YOUR ANSWER HERE
public static int computeProduct(int[] nums) {
int total = 1;
Expand All @@ -50,7 +50,7 @@ public static int computeProduct(int[] nums) {
return total;
}

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


// The time complexity is:
// The time complexity is: n
// YOUR ANSWER HERE
public static int computeFactorial(int n) {
int result = 1;
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
// The time complexity is: n^2
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) {


// assume that each String is bounded by a constant length
// The time complexity is:
// The time complexity is: n
// YOUR ANSWER HERE
public static void checkIfContainedArrayList(ArrayList<String> arr, String target) {
if (arr.contains(target)) {
Expand All @@ -93,7 +94,7 @@ public static void checkIfContainedArrayList(ArrayList<String> arr, String targe

// assume n = wordsA.length = wordsB.length
// assume that each String is bounded by a constant length
// The time complexity is:
// The time complexity is: n^2
// YOUR ANSWER HERE
public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
for(String wordA : wordsA) {
Expand All @@ -107,7 +108,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
}

// assume that each String is bounded by a constant length
// The time complexity is:
// The time complexity is: n
// YOUR ANSWER HERE
public static boolean containsOverlap2(String[] wordsA, String[] wordsB) {
Set<String> wordsSet = new HashSet<>();
Expand All @@ -124,22 +125,22 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) {
return false;
}

// The time complexity is:
// The time complexity is: n
// YOUR ANSWER HERE
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:
// The time complexity is: 1
// YOUR ANSWER HERE
public static double computeAverage(double a, double b) {
return (a + b) / 2.0;
}

// assume that each String is bounded by a constant length
// The time complexity is:
// The time complexity is: 1
// YOUR ANSWER HERE
public static void checkIfContainedHashSet(HashSet<String> set, String target)
{
Expand All @@ -157,6 +158,7 @@ public static void checkIfContainedHashSet(HashSet<String> set, String target)
// assume that each String is bounded by a constant length
// What is the time complexity of this method?
// YOUR ANSWER HERE
// The time complexity is: n
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 @@ -173,14 +175,19 @@ public static String emailLookup(String[] names, String[] emails, String queryNa
// assume that each String is bounded by a constant length
// What is the time complexity of your solution?
// YOUR ANSWER HERE
// The time complexity is: O(1)
public static String emailLookupEfficient(HashMap<String, String> namesToEmails, String queryName) {
return null;
if(namesToEmails.containsKey(queryName)){
return namesToEmails.get(queryName);
}
return "Person not found";
}

// What is the time complexity of this method?
// assume that each String is bounded by a constant length
// (assume the set and list have the same number of elements)
// YOUR ANSWER HERE
// The time complexity is: n^2
public static boolean hasCommon(HashSet<String> wordSet, ArrayList<String> wordList) {
for(String word : wordSet) {
if(wordList.contains(word)) {
Expand All @@ -194,7 +201,13 @@ public static boolean hasCommon(HashSet<String> wordSet, ArrayList<String> wordL
// assume that each String is bounded by a constant length
// What is the time complexity of your new solution?
// YOUR ANSWER HERE
// The time complexity is: O(n)
public static boolean hasCommonEfficient(HashSet<String> wordSet, ArrayList<String> wordList) {
for (String word : wordList) {
if (wordSet.contains(word)) {
return true;
}
}
return false;
}

Expand All @@ -205,18 +218,24 @@ public static boolean hasCommonEfficient(HashSet<String> wordSet, ArrayList<Stri
// What would be a good choice of data structure?
// YOUR ANSWER HERE

//HashMap

// 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

//List, since we are indexing the position

// 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

//List, assuming doing a loop using length to get index last added
}