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
116 changes: 38 additions & 78 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class Main {

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

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

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

// The time complexity is:
// YOUR ANSWER HERE
// The time complexity is O(m), where m is the number of banned passwords
public static boolean isBanned(String password) {
String[] bannedPasswords = {"password", "hello", "qwerty"};
boolean banned = false;
Expand All @@ -39,48 +35,40 @@ public static boolean isBanned(String password) {
return banned;
}


// The time complexity is:
// YOUR ANSWER HERE
// The time complexity is O(n), where n is the length of the nums array
public static int computeProduct(int[] nums) {
int total = 1;
for(int num : nums) {
total *= num;
}
return total;
}

// The time complexity is:
// YOUR ANSWER HERE

// The time complexity isO(n), where n is the length of the nums array
public static void describeProduct(int[] nums) {
System.out.println("About to compute the product of the array...");
int product = computeProduct(nums);
System.out.println("The product I found was " + product);
}


// The time complexity is:
// YOUR ANSWER HERE
// The time complexity is O(n), where n is the input number 'n'
public static int computeFactorial(int n) {
int result = 1;
for(int i = 1; i <= n; i++) {
result *= n;
result *= i;
}
return result;
}

// Assume that the largest number is no bigger than the length
// of the array
// The time complexity is O(n)
public static void computeAllFactorials(int[] nums) {
for(int num : nums) {
int result = computeFactorial(num);
System.out.println("The factorial of " + num + " is " + result);
}
}


// The time complexity is:
// YOUR ANSWER HERE
// The time complexity is O(n), where n is the length 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 @@ -89,10 +77,7 @@ public static void checkIfContainedArrayList(ArrayList<String> arr, String targe
}
}


// assume n = wordsA.length = wordsB.length
// The time complexity is:
// YOUR ANSWER HERE
// The time complexity is: O(n^2), where n is the length of the arrays
public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
for(String wordA : wordsA) {
for(String wordB : wordsB) {
Expand All @@ -104,8 +89,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
return false;
}

// The time complexity is:
// YOUR ANSWER HERE
// The time complexity is: O(n), where n is the total length of the two arrays
public static boolean containsOverlap2(String[] wordsA, String[] wordsB) {
Set<String> wordsSet = new HashSet<>();
for(String word : wordsA) {
Expand All @@ -121,36 +105,29 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) {
return false;
}

// The time complexity is:
// YOUR ANSWER HERE
// The time complexity is O(n), where n is 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

// The time complexity is O(1)
public static double computeAverage(double a, double b) {
return (a + b) / 2.0;
}
// The time complexity is:
// YOUR ANSWER HERE
public static void checkIfContainedHashSet(HashSet<String> set, String target)
{

// The time complexity is O(1), assuming the set's `contains` method is O(1)
public static void checkIfContainedHashSet(HashSet<String> set, String target) {
if (set.contains(target)) {
System.out.println(target + " is present in the set");
} else {
System.out.println(target + " is not present in the set");
}
}

// emailLookup attempts to find the email associated with a name.
// The name at index i of names corresponds to the email at index i of emails
// 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
// The time complexity is O(n), where n is the 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 @@ -160,19 +137,12 @@ public static String emailLookup(String[] names, String[] emails, String queryNa
return "Person not found";
}

// Suppose that emailLookupEfficient performs the same task as emailLookup
// However, instead of two arrays it is passed a map where the
// 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
// The time complexity is O(1), since a HashMap lookup is O(1) on average
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
// The time complexity is O(n^2), where n is the length of the set/list
public static boolean hasCommon(HashSet<String> wordSet, ArrayList<String> wordList) {
for(String word : wordSet) {
if(wordList.contains(word)) {
Expand All @@ -181,33 +151,23 @@ public static boolean hasCommon(HashSet<String> wordSet, ArrayList<String> wordL
}
return false;
}
// 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

// The time complexity is O(n), where n is the total length of the set and list
public static boolean hasCommonEfficient(HashSet<String> wordSet, ArrayList<String> wordList) {
for(String word : wordList) {
if(wordSet.contains(word)) {
return true;
}
}
return false;
}

// Suppose you are building a dashboard that displays real-time stock prices.
// You want to keep track of the current price of each stock, with the stock's ticker symbol as the key.
// 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

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

// 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
}
// The best choice of data structure is: HashMap<String, Double>
// A HashMap is ideal for keeping track of stock prices, providing efficient O(1) time complexity for both updates and lookups.

// The best choice of data structure is: ArrayList<String>
// An ArrayList allows efficient adding of songs to the end and accessing songs by their position, making it a good fit for this scenario.

// The best choice of data structure is: LinkedList<String>
// A LinkedList preserves the order of recent search queries, which is ideal when displaying the user's recent searches.
}