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
89 changes: 54 additions & 35 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,52 @@
public class Main {

// The time complexity is:
// YOUR ANSWER HERE
// O(n^2) (Oh n-squared)
public static void timesTable(int x) {
for(int i = 1; i <= x; i++) {
for(int j = 1; j <= x; j++) {
System.out.print(i*j + " ");
for(int i = 1; i <= x; i++) { //O(n)
for(int j = 1; j <= x; j++) { //*O(n)
System.out.print(i*j + " ");//O(1)
}
System.out.println();
System.out.println(); //O(n)
}
}

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

for (char letter : letters) {
System.out.println(letter);
for (char letter : letters) { //O(n)
System.out.println(letter); //O(1)
}
}

// The time complexity is:
// YOUR ANSWER HERE
// O(1)
public static boolean isBanned(String password) {
String[] bannedPasswords = {"password", "hello", "qwerty"};
boolean banned = false;
for(String bannedPassword : bannedPasswords) {
String[] bannedPasswords = {"password", "hello", "qwerty"}; //O(1)
boolean banned = false; //O(1)
for(String bannedPassword : bannedPasswords) { //O(1) - fixed-length list (would be O(n) if variable)
if(password.equals(bannedPassword)) {
banned = true;
banned = true; //O(1)
}
}
return banned;
}


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

// The time complexity is:
// YOUR ANSWER HERE
// O(n) (calling computeProduct(nums) which is O(n))
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)
public static int computeFactorial(int n) {
int result = 1;
for(int i = 1; i <= n; i++) {
Expand All @@ -71,6 +71,8 @@ public static int computeFactorial(int n) {

// Assume that the largest number is no bigger than the length
// of the array

//Time complexity is O(n^2)
public static void computeAllFactorials(int[] nums) {
for(int num : nums) {
int result = computeFactorial(num);
Expand All @@ -80,7 +82,7 @@ public static void computeAllFactorials(int[] nums) {


// The time complexity is:
// YOUR ANSWER HERE
// O(n) - contains() performs linear search
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 +94,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)
public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
for(String wordA : wordsA) {
for(String wordB : wordsB) {
Expand All @@ -105,7 +107,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) {
}

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

// The time complexity is:
// YOUR ANSWER HERE
// O(n)
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)
public static double computeAverage(double a, double b) {
return (a + b) / 2.0;
}
// The time complexity is:
// YOUR ANSWER HERE
// O(1)
public static void checkIfContainedHashSet(HashSet<String> set, String target)
{
if (set.contains(target)) {
Expand All @@ -150,7 +152,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)
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 +167,22 @@ 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)
public static String emailLookupEfficient(HashMap<String, String> namesToEmails, String queryName) {
return null;
//use HashMap get method to find email associated with queryName
String email = namesToEmails.get(queryName);

//if email null
if (email == null) {
return "Person not found";
}

return email;
}

// 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) (this method is inefficient/backwards)
public static boolean hasCommon(HashSet<String> wordSet, ArrayList<String> wordList) {
for(String word : wordSet) {
if(wordList.contains(word)) {
Expand All @@ -184,30 +194,39 @@ 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)
public static boolean hasCommonEfficient(HashSet<String> wordSet, ArrayList<String> wordList) {
return false;
//iterate over the wordList (instead of over wordSet)
for (String word : wordList) {
//check if word present in wordSet
if (wordSet.contains(word)) {
return true;
}
}
return false; //return false if no common word found
}

// 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
// ** HashMap (efficient lookups O(1); efficient updates
// O(1); order of ticker symbols not important)

// 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
// ** LinkedList (efficient adding of songs especially at end of list; users usually listen to songs in sequential order, LinkedList is good for this; random access would be O(n) however)

// 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 think a LinkedList would again be the
//best choice here (a LinkedList implemented as a queue actually); this would preserve order for a small number of queries and allow for efficient additions
}