From d5898d567d1774292086b56fe54110c8c1626770 Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Wed, 4 Mar 2026 15:38:28 -0500 Subject: [PATCH 1/9] Implemented Iterable problems --- .../Iterable/Examples/ForEachLoopDemo.java | 10 ++++++++- .../java/Iterable/Examples/IteratorDemo.java | 12 ++++++++++ .../Iterable/Practice/IterableWarmups.java | 22 +++++++++++++++---- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..88ef909 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -17,13 +17,18 @@ public static void main(String[] args) { // TODO: // Use a for-each loop to print each student name + for (String student : students) { + System.out.println(student); + } System.out.println("\nPrinting students in uppercase:"); // TODO: // Use a for-each loop to print each name in uppercase - + for (String student : students) { + System.out.println(student.toUpperCase()); + } System.out.println("\nCount the number of students:"); @@ -31,6 +36,9 @@ public static void main(String[] args) { // TODO: // Use a for-each loop to count how many students are in the list + for (String student : students) { + count++; + } System.out.println("Total students: " + count); } diff --git a/src/main/java/Iterable/Examples/IteratorDemo.java b/src/main/java/Iterable/Examples/IteratorDemo.java index b09fca7..635267f 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -27,6 +27,11 @@ public static void main(String[] args) { // Use iterator.hasNext() and iterator.next() // Print each number + while (iterator.hasNext()) { + int num = iterator.next(); + System.out.println(num); + } + System.out.println("\nRemoving odd numbers using Iterator"); @@ -36,6 +41,13 @@ public static void main(String[] args) { // Use iterator to remove odd numbers // Remember: use iterator.remove() + while (iterator.hasNext()) { + int num = iterator.next(); + if (num % 2 != 0) { + iterator.remove(); + } + } + System.out.println("\nUpdated list:"); System.out.println(numbers); diff --git a/src/main/java/Iterable/Practice/IterableWarmups.java b/src/main/java/Iterable/Practice/IterableWarmups.java index 9e9de94..459bc2a 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -30,7 +30,9 @@ public static int sum(Iterable numbers) { // TODO: // Use a for-each loop to calculate the sum - + for (int num : numbers) { + total += num; + } return total; } @@ -46,7 +48,11 @@ public static int countEven(Iterable numbers) { // TODO: // Loop through numbers // Increment count if number is even - + for (int num : numbers) { + if (num % 2 == 0) { + count++; + } + } return count; } @@ -62,7 +68,11 @@ public static int findMax(Iterable numbers) { // TODO: // Loop through numbers // Update max if current number is larger - + for (int num : numbers) { + if (num > max) { + max = num; + } + } return max; } @@ -78,7 +88,11 @@ public static int countMatches(Iterable words, String target) { // TODO: // Loop through words // Compare each word to target - + for (String word : words) { + if (word.equals(target)) { + count++; + } + } return count; } } From b947ad6e822c33ceb86e7c71597a9a26a6fbd43d Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Wed, 4 Mar 2026 15:48:10 -0500 Subject: [PATCH 2/9] Implement CollectionBasics --- .../Practice/CollectionBasics.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..48d6457 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Set; public class CollectionBasics { public static void main(String[] args) { @@ -34,6 +35,9 @@ public static int sum(Collection numbers) { // Loop through the collection // Add each number to total + for (int num : numbers) { + total += num; + } return total; } @@ -49,7 +53,11 @@ public static int countEven(Collection numbers) { // TODO: // Loop through the collection // If the number is even, increase count - + for (int num : numbers) { + if (num % 2 == 0) { + count++; + } + } return count; } @@ -65,7 +73,11 @@ public static int findMax(Collection numbers) { // TODO: // Loop through numbers // Update max if current number is larger - + for (int num : numbers) { + if (num > max) { + max = num; + } + } return max; } @@ -80,7 +92,9 @@ public static boolean hasDuplicates(Collection numbers) { // TODO: // Hint: // Compare the size of a collection with the size of a Set - + if (numbers.size() != Set.copyOf(numbers).size()) { + return true; + } return false; } @@ -96,6 +110,11 @@ public static int countOccurrences(Collection numbers, int target) { // TODO: // Loop through numbers // If number equals target, increase count + for (int num : numbers) { + if (num == target) { + count++; + } + } return count; } @@ -113,6 +132,11 @@ public static Collection filterGreaterThanTwenty(Collection nu // TODO: // Loop through numbers // Add numbers greater than 20 to result + for (int num : numbers) { + if (num > 20) { + result.add(num); + } + } return result; } From cbb1c994b662a15d53af6c0e8e7d4d15e500b8c8 Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Wed, 4 Mar 2026 16:06:39 -0500 Subject: [PATCH 3/9] Implemented ArrayList problems --- .../Lists/ArrayLists/ArrayListProblems.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..0194476 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; public class ArrayListProblems { public static void main(String[] args) { @@ -32,10 +33,13 @@ public static void main(String[] args) { Output: 6 */ public static int sum(List nums) { - // TODO: Implement this method + int sum = 0; + for (int num : nums) { + sum += num; + } - return 0; + return sum; } /* @@ -49,8 +53,14 @@ public static int sum(List nums) { public static int countEvens(List nums) { // TODO: Implement this method - - return 0; + int evens = 0; + for (int num : nums) { + if (num % 2 == 0) { + evens++; + } + } + + return evens; } /* @@ -67,7 +77,9 @@ public static int countEvens(List nums) { public static boolean hasDuplicate(List nums) { // TODO: Implement this method - + if (nums.size() != Set.copyOf(nums).size()) { + return true; + } return false; } @@ -82,8 +94,13 @@ public static boolean hasDuplicate(List nums) { public static int findMax(List nums) { // TODO: Implement this method - - return 0; + int max = Integer.MIN_VALUE; + for (int num : nums) { + if (num > max) { + max = num; + } + } + return max; } /* @@ -99,7 +116,6 @@ public static int findMax(List nums) { public static List reverse(List nums) { // TODO: Implement this method - - return null; + return nums.reversed(); } } From b255f39f0c48b51dc4d1e00c0f641c8966c931bd Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Wed, 4 Mar 2026 16:20:31 -0500 Subject: [PATCH 4/9] Implemented LinkedListProblems --- .../java/Lists/LinkedLists/LinkedListProblems.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..207bd59 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -38,7 +38,7 @@ public static void main(String[] args) { public static void addToFront(LinkedList list, int value) { // TODO: Implement this method - + list.push(value); } /* @@ -52,6 +52,7 @@ public static void addToFront(LinkedList list, int value) { public static void addToEnd(LinkedList list, int value) { // TODO: Implement this method + list.addLast(value); } @@ -66,7 +67,7 @@ public static void addToEnd(LinkedList list, int value) { public static void removeFirstElement(LinkedList list) { // TODO: Implement this method - + list.removeFirst(); } /* @@ -80,6 +81,7 @@ public static void removeFirstElement(LinkedList list) { public static void removeLastElement(LinkedList list) { // TODO: Implement this method + list.removeLast(); } @@ -94,8 +96,7 @@ public static void removeLastElement(LinkedList list) { public static int getFirstElement(LinkedList list) { // TODO: Implement this method - - return 0; + return list.getFirst(); } /* @@ -109,7 +110,6 @@ public static int getFirstElement(LinkedList list) { public static int getLastElement(LinkedList list) { // TODO: Implement this method - - return 0; + return list.getLast(); } } From 13ee1f9fe19b6ba87df41a32121ab4699dbb59c6 Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Wed, 4 Mar 2026 19:11:41 -0500 Subject: [PATCH 5/9] Finished HashSetProblems --- .../java/Sets/HashSet/HashSetProblems.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..ac000e8 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -1,5 +1,7 @@ package Sets.HashSet; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -7,6 +9,14 @@ public class HashSetProblems { public static void main(String[] args) { // You can test your methods here + Set test = new HashSet<>(); + addElement(test, "apple"); + System.out.println(test); + System.out.println(containsValue(test, "apple")); + removeValue(test, "apple"); + System.out.println(test); + System.out.println(getUniqueValues(new ArrayList<>(List.of(1, 2, 2, 3, 3)))); + } @@ -21,6 +31,7 @@ public static void main(String[] args) { public static void addElement(Set set, String value) { // TODO: Implement this method + set.add(value); } @@ -35,8 +46,7 @@ public static void addElement(Set set, String value) { public static boolean containsValue(Set set, String value) { // TODO: Implement this method - - return false; + return set.contains(value); } /* @@ -48,9 +58,8 @@ public static boolean containsValue(Set set, String value) { Output: value removed */ public static void removeValue(Set set, String value) { - // TODO: Implement this method - + set.remove(value); } /* @@ -65,7 +74,7 @@ public static int getUniqueCount(Set set) { // TODO: Implement this method - return 0; + return set.size(); } /* @@ -79,7 +88,6 @@ public static int getUniqueCount(Set set) { public static Set getUniqueValues(List numbers) { // TODO: Implement this method - - return null; + return new HashSet<>(numbers); } } From 8d3031316d3f0d65d57c3fdd9f747d01cdf35e3a Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Thu, 5 Mar 2026 15:37:24 -0500 Subject: [PATCH 6/9] Implemented queue problems --- .../Queues/ArrayDeque/ArrayDequeProblems.java | 19 ++++----- src/main/java/Queues/Deque/DequeProblems.java | 41 +++++++++++-------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..c817d09 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -29,8 +29,8 @@ public static void main(String[] args) { Output: [5] */ public static void addToFront(ArrayDeque deque, int value) { - // TODO: Implement this method + deque.addFirst(value); } @@ -43,8 +43,8 @@ public static void addToFront(ArrayDeque deque, int value) { Output: [5,10] */ public static void addToBack(ArrayDeque deque, int value) { - // TODO: Implement this method + deque.addLast(value); } @@ -57,8 +57,8 @@ public static void addToBack(ArrayDeque deque, int value) { Output: [10,20] */ public static void removeFront(ArrayDeque deque) { - - // TODO: Implement this method + // TODO: Implement this method' + deque.removeFirst(); } @@ -71,9 +71,8 @@ public static void removeFront(ArrayDeque deque) { Output: [5,10] */ public static void removeBack(ArrayDeque deque) { - // TODO: Implement this method - + deque.removeLast(); } /* @@ -85,10 +84,8 @@ public static void removeBack(ArrayDeque deque) { Output: 5 */ public static Integer peekFront(ArrayDeque deque) { - // TODO: Implement this method - - return null; + return deque.peekFirst(); } /* @@ -100,9 +97,7 @@ public static Integer peekFront(ArrayDeque deque) { Output: 20 */ public static Integer peekBack(ArrayDeque deque) { - // TODO: Implement this method - - return null; + return deque.peekLast(); } } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..46d2214 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -1,12 +1,28 @@ package Queues.Deque; +import java.util.ArrayDeque; import java.util.Deque; public class DequeProblems { public static void main(String[] args) { - // You can test your methods here + ArrayDeque numbers = new ArrayDeque<>(); + + addFront(numbers, 10); + addFront(numbers, 5); + addBack(numbers, 20); + + System.out.println(peekFront(numbers)); + System.out.println(peekBack(numbers)); + + System.out.println("Deque: " + numbers); + + removeFront(numbers); + System.out.println("After removing front: " + numbers); + + removeBack(numbers); + System.out.println("After removing back: " + numbers); } /* @@ -18,9 +34,8 @@ public static void main(String[] args) { Output: [5] */ public static void addFront(Deque deque, int value) { - // TODO: Implement this method - + deque.addFirst(value); } /* @@ -32,9 +47,8 @@ public static void addFront(Deque deque, int value) { Output: [5,10] */ public static void addBack(Deque deque, int value) { - // TODO: Implement this method - + deque.addLast(value); } /* @@ -46,10 +60,8 @@ public static void addBack(Deque deque, int value) { Output: 5 */ public static Integer removeFront(Deque deque) { - // TODO: Implement this method - - return null; + return deque.removeFirst(); } /* @@ -61,10 +73,8 @@ public static Integer removeFront(Deque deque) { Output: 15 */ public static Integer removeBack(Deque deque) { - // TODO: Implement this method - - return null; + return deque.removeLast(); } /* @@ -76,10 +86,7 @@ public static Integer removeBack(Deque deque) { Output: 5 */ public static Integer peekFront(Deque deque) { - - // TODO: Implement this method - - return null; + return deque.peekFirst(); } /* @@ -91,10 +98,8 @@ public static Integer peekFront(Deque deque) { Output: 15 */ public static Integer peekBack(Deque deque) { - // TODO: Implement this method - - return null; + return deque.peekLast(); } } From d5338d74813c57b2e5df27e55c51b62c27664b22 Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Thu, 5 Mar 2026 15:57:16 -0500 Subject: [PATCH 7/9] Implemented MapProblems --- .../java/Maps/HashMap/HashMapProblems.java | 26 +++++++++-------- .../LinkedHashMap/LinkedHashMapProblems.java | 28 +++++++++++-------- .../java/Maps/TreeMap/TreeMapProblems.java | 18 ++++-------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index 04b5567..d2bf189 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -33,9 +33,8 @@ public static void main(String[] args) { Output: {"Apples"=10} */ public static void addItem(Map map, String item, int quantity) { - // TODO: Implement this method - + map.put(item, quantity); } /* @@ -47,10 +46,8 @@ public static void addItem(Map map, String item, int quantity) Output: 10 */ public static int getQuantity(Map map, String item) { - // TODO: Implement this method - - return 0; + return map.get(item); } /* @@ -62,9 +59,8 @@ public static int getQuantity(Map map, String item) { Output: {"Bananas"=12} */ public static void updateQuantity(Map map, String item, int newQuantity) { - // TODO: Implement this method - + map.replace(item, newQuantity); } /* @@ -76,9 +72,8 @@ public static void updateQuantity(Map map, String item, int new Output: item removed */ public static void removeItem(Map map, String item) { - // TODO: Implement this method - + map.remove(item); } /* @@ -90,9 +85,16 @@ public static void removeItem(Map map, String item) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - // TODO: Implement this method - - return null; + Map answer = new HashMap<>(); + for (int num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); + } + } + return answer; } } diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..da183e8 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -1,5 +1,6 @@ package Maps.LinkedHashMap; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -31,9 +32,8 @@ public static void main(String[] args) { Output: {"Jordan"=90} */ public static void addStudent(Map map, String name, int grade) { - // TODO: Implement this method - + map.put(name, grade); } /* @@ -45,9 +45,8 @@ public static void addStudent(Map map, String name, int grade) Output: {"Taylor"=92} */ public static void updateGrade(Map map, String name, int newGrade) { - // TODO: Implement this method - + map.replace(name, newGrade); } /* @@ -59,9 +58,8 @@ public static void updateGrade(Map map, String name, int newGra Output: Student removed from map */ public static void removeStudent(Map map, String name) { - // TODO: Implement this method - + map.remove(name); } /* @@ -73,9 +71,10 @@ public static void removeStudent(Map map, String name) { Output: "Jordan" */ public static String getFirstInserted(Map map) { - // TODO: Implement this method - + for (String key : map.keySet()) { + return key; + } return null; } @@ -89,9 +88,16 @@ public static String getFirstInserted(Map map) { Output: {apple=2, banana=1, orange=1} */ public static Map wordFrequency(List words) { - // TODO: Implement this method - - return null; + Map answer = new LinkedHashMap<>(); + for (String word : words) { + if (answer.containsKey(word)) { + int value = answer.get(word); + answer.replace(word, value + 1); + } else { + answer.put(word, 1); + } + } + return answer; } } diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..8fd33fc 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -29,9 +29,8 @@ public static void main(String[] args) { Output: {1="Jordan"} */ public static void addPlayer(TreeMap map, int rank, String name) { - // TODO: Implement this method - + map.put(rank, name); } /* @@ -43,10 +42,8 @@ Return the player with the highest ranking (smallest key). Output: "Jordan" */ public static String getTopPlayer(TreeMap map) { - // TODO: Implement this method - - return null; + return map.firstEntry().getValue(); } /* @@ -58,10 +55,8 @@ Return the player with the lowest ranking (largest key). Output: "Taylor" */ public static String getLowestPlayer(TreeMap map) { - // TODO: Implement this method - - return null; + return map.lastEntry().getValue(); } /* @@ -73,9 +68,8 @@ public static String getLowestPlayer(TreeMap map) { Output: player removed */ public static void removePlayer(TreeMap map, int rank) { - // TODO: Implement this method - + map.remove(rank); } /* @@ -87,9 +81,7 @@ public static void removePlayer(TreeMap map, int rank) { Output: 3 */ public static Integer getNextRank(TreeMap map, int rank) { - // TODO: Implement this method - - return null; + return rank + 1; } } From 2e3299b121a5ad3b7fb565a461d0ae2772189262 Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Fri, 6 Mar 2026 10:04:16 -0500 Subject: [PATCH 8/9] Commented out TODOs --- .../CollectionsHackerrankProblems.java | 144 +++++++++++++----- .../Lists/ArrayLists/ArrayListProblems.java | 12 -- .../Lists/LinkedLists/LinkedListProblems.java | 14 -- .../java/Maps/HashMap/HashMapProblems.java | 6 - .../LinkedHashMap/LinkedHashMapProblems.java | 6 - .../java/Maps/TreeMap/TreeMapProblems.java | 6 - .../Queues/ArrayDeque/ArrayDequeProblems.java | 8 - src/main/java/Queues/Deque/DequeProblems.java | 6 - .../java/Sets/HashSet/HashSetProblems.java | 14 -- 9 files changed, 107 insertions(+), 109 deletions(-) diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..7ae7de1 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,17 +1,65 @@ package CollectionsHackerrank; -import java.util.List; -import java.util.Map; -import java.util.Queue; +import java.util.*; public class CollectionsHackerrankProblems { - public class CollectionsHackerrankPractice { - public static void main(String[] args) { - - // You can test your methods here - - } + public static void main(String[] args) { + + // Problem 1 + System.out.println("Problem 1 - Remove Duplicates"); + System.out.println(removeDuplicates(new ArrayList<>(List.of(1,2,2,3,4,4,5)))); + // Expected: [1,2,3,4,5] + + // Problem 2 + System.out.println("\nProblem 2 - Count Frequency"); + System.out.println(countFrequency(new ArrayList<>(List.of(1,2,2,3,3,3)))); + // Expected: {1=1, 2=2, 3=3} + + // Problem 3 + System.out.println("\nProblem 3 - First Unique"); + System.out.println(firstUnique(new ArrayList<>(List.of(4,5,1,2,0,4)))); + // Expected: 5 + + // Problem 4 + System.out.println("\nProblem 4 - Two Sum"); + System.out.println(twoSum(new ArrayList<>(List.of(2,7,11,15)), 9)); + System.out.println(twoSum(new ArrayList<>(List.of(2,8,11,15)), 9)); + // Expected: true, false + + // Problem 5 + System.out.println("\nProblem 5 - Count Unique Words"); + System.out.println(countUniqueWords(new ArrayList<>(List.of("apple","banana","apple","orange")))); + // Expected: 3 + + // Problem 6 + System.out.println("\nProblem 6 - Reverse Queue"); + Queue queue = new LinkedList<>(List.of(1,2,3,4)); + System.out.println(reverseQueue(queue)); + // Expected: [4,3,2,1] + + // Problem 7 + System.out.println("\nProblem 7 - Balanced Parentheses"); + System.out.println(isBalanced("(())")); + // Expected: true + System.out.println(isBalanced("(()")); + // Expected: false + + // Problem 8 + System.out.println("\nProblem 8 - Most Frequent"); + System.out.println(mostFrequent(new ArrayList<>(List.of(1,3,2,3,4,3)))); + // Expected: 3 + + // Problem 9 + System.out.println("\nProblem 9 - Group By Length"); + System.out.println(groupByLength(new ArrayList<>(List.of("cat","dog","elephant","ant")))); + // Expected: {3=[cat,dog,ant], 8=[elephant]} + + // Problem 10 + System.out.println("\nProblem 10 - Max Sliding Window Sum"); + System.out.println(maxSlidingWindowSum(new ArrayList<>(List.of(2,1,5,1,3,2)), 3)); + // Expected: 9 + } /* Problem 1 @@ -22,10 +70,7 @@ public static void main(String[] args) { Output: [1,2,3,4,5] */ public static List removeDuplicates(List numbers) { - - // TODO: Implement this method - - return null; + return new ArrayList<>(new LinkedHashSet<>(numbers)); } /* @@ -37,10 +82,16 @@ public static List removeDuplicates(List numbers) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - - // TODO: Implement this method - - return null; + Map answer = new HashMap<>(); + for (Integer num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); + } + } + return answer; } /* @@ -52,10 +103,21 @@ public static Map countFrequency(List numbers) { Output: 5 */ public static Integer firstUnique(List numbers) { - - // TODO: Implement this method - - return null; + LinkedHashMap answer = new LinkedHashMap<>(); + for (Integer num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); + } + } + for (Integer key : answer.keySet()) { + if (answer.get(key) == 1) { + return key; + } + } + return 0; } /* @@ -69,9 +131,13 @@ public static Integer firstUnique(List numbers) { Output: true */ public static boolean twoSum(List numbers, int target) { - - // TODO: Implement this method - + for (int i = 0; i < numbers.size() - 1; i++) { + for (int j = i + 1; j < numbers.size(); j++) { + if (numbers.get(i) + numbers.get(j) == target) { + return true; + } + } + } return false; } @@ -84,10 +150,8 @@ public static boolean twoSum(List numbers, int target) { Output: 3 */ public static int countUniqueWords(List words) { - - // TODO: Implement this method - - return 0; + Set unique = new HashSet<>(new HashSet<>(words)); + return unique.size(); } /* @@ -99,10 +163,12 @@ public static int countUniqueWords(List words) { Output: [4,3,2,1] */ public static Queue reverseQueue(Queue queue) { - - // TODO: Implement this method - - return null; + Queue answer = new ArrayDeque<>(); + while (!answer.isEmpty()) { + reverseQueue(queue); + answer.add(queue.remove()); + } + return answer; } /* @@ -169,10 +235,14 @@ public static Map> groupByLength(List words) { Output: 9 */ public static int maxSlidingWindowSum(List numbers, int k) { - - // TODO: Implement this method - + int windowStart = 0; + int windowEnd = k; + int max = Integer.MIN_VALUE; + while (k != numbers.size()) { + for (int i = windowStart; i < k; i++) { + + } + } return 0; - } - } + } } diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index 0194476..8a3def9 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -7,7 +7,6 @@ public class ArrayListProblems { public static void main(String[] args) { - List numbers = new ArrayList<>(); numbers.add(4); @@ -33,12 +32,10 @@ public static void main(String[] args) { Output: 6 */ public static int sum(List nums) { - // TODO: Implement this method int sum = 0; for (int num : nums) { sum += num; } - return sum; } @@ -51,15 +48,12 @@ public static int sum(List nums) { Output: 2 */ public static int countEvens(List nums) { - - // TODO: Implement this method int evens = 0; for (int num : nums) { if (num % 2 == 0) { evens++; } } - return evens; } @@ -75,8 +69,6 @@ public static int countEvens(List nums) { Output: false */ public static boolean hasDuplicate(List nums) { - - // TODO: Implement this method if (nums.size() != Set.copyOf(nums).size()) { return true; } @@ -92,8 +84,6 @@ public static boolean hasDuplicate(List nums) { Output: 7 */ public static int findMax(List nums) { - - // TODO: Implement this method int max = Integer.MIN_VALUE; for (int num : nums) { if (num > max) { @@ -114,8 +104,6 @@ public static int findMax(List nums) { The original list should remain unchanged. */ public static List reverse(List nums) { - - // TODO: Implement this method return nums.reversed(); } } diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 207bd59..3ab19a1 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -4,7 +4,6 @@ public class LinkedListProblems { public static void main(String[] args) { - LinkedList numbers = new LinkedList<>(); numbers.add(10); @@ -36,8 +35,6 @@ public static void main(String[] args) { Output: [5,10,20,30] */ public static void addToFront(LinkedList list, int value) { - - // TODO: Implement this method list.push(value); } @@ -50,8 +47,6 @@ public static void addToFront(LinkedList list, int value) { Output: [10,20,30,40] */ public static void addToEnd(LinkedList list, int value) { - - // TODO: Implement this method list.addLast(value); } @@ -65,8 +60,6 @@ public static void addToEnd(LinkedList list, int value) { Output: [20,30] */ public static void removeFirstElement(LinkedList list) { - - // TODO: Implement this method list.removeFirst(); } @@ -79,10 +72,7 @@ public static void removeFirstElement(LinkedList list) { Output: [10,20] */ public static void removeLastElement(LinkedList list) { - - // TODO: Implement this method list.removeLast(); - } /* @@ -94,8 +84,6 @@ public static void removeLastElement(LinkedList list) { Output: 10 */ public static int getFirstElement(LinkedList list) { - - // TODO: Implement this method return list.getFirst(); } @@ -108,8 +96,6 @@ public static int getFirstElement(LinkedList list) { Output: 30 */ public static int getLastElement(LinkedList list) { - - // TODO: Implement this method return list.getLast(); } } diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index d2bf189..5e59bfb 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -6,7 +6,6 @@ public class HashMapProblems { public static void main(String[] args) { - Map inventory = new HashMap<>(); addItem(inventory, "Apples", 10); @@ -33,7 +32,6 @@ public static void main(String[] args) { Output: {"Apples"=10} */ public static void addItem(Map map, String item, int quantity) { - // TODO: Implement this method map.put(item, quantity); } @@ -46,7 +44,6 @@ public static void addItem(Map map, String item, int quantity) Output: 10 */ public static int getQuantity(Map map, String item) { - // TODO: Implement this method return map.get(item); } @@ -59,7 +56,6 @@ public static int getQuantity(Map map, String item) { Output: {"Bananas"=12} */ public static void updateQuantity(Map map, String item, int newQuantity) { - // TODO: Implement this method map.replace(item, newQuantity); } @@ -72,7 +68,6 @@ public static void updateQuantity(Map map, String item, int new Output: item removed */ public static void removeItem(Map map, String item) { - // TODO: Implement this method map.remove(item); } @@ -85,7 +80,6 @@ public static void removeItem(Map map, String item) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - // TODO: Implement this method Map answer = new HashMap<>(); for (int num : numbers) { if (answer.containsKey(num)) { diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index da183e8..bf031e0 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -7,7 +7,6 @@ public class LinkedHashMapProblems { public static void main(String[] args) { - Map studentGrades = new LinkedHashMap<>(); addStudent(studentGrades, "Jordan", 90); @@ -32,7 +31,6 @@ public static void main(String[] args) { Output: {"Jordan"=90} */ public static void addStudent(Map map, String name, int grade) { - // TODO: Implement this method map.put(name, grade); } @@ -45,7 +43,6 @@ public static void addStudent(Map map, String name, int grade) Output: {"Taylor"=92} */ public static void updateGrade(Map map, String name, int newGrade) { - // TODO: Implement this method map.replace(name, newGrade); } @@ -58,7 +55,6 @@ public static void updateGrade(Map map, String name, int newGra Output: Student removed from map */ public static void removeStudent(Map map, String name) { - // TODO: Implement this method map.remove(name); } @@ -71,7 +67,6 @@ public static void removeStudent(Map map, String name) { Output: "Jordan" */ public static String getFirstInserted(Map map) { - // TODO: Implement this method for (String key : map.keySet()) { return key; } @@ -88,7 +83,6 @@ public static String getFirstInserted(Map map) { Output: {apple=2, banana=1, orange=1} */ public static Map wordFrequency(List words) { - // TODO: Implement this method Map answer = new LinkedHashMap<>(); for (String word : words) { if (answer.containsKey(word)) { diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index 8fd33fc..31f5e10 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -4,7 +4,6 @@ public class TreeMapProblems { public static void main(String[] args) { - TreeMap rankings = new TreeMap<>(); addPlayer(rankings, 3, "Jordan"); @@ -29,7 +28,6 @@ public static void main(String[] args) { Output: {1="Jordan"} */ public static void addPlayer(TreeMap map, int rank, String name) { - // TODO: Implement this method map.put(rank, name); } @@ -42,7 +40,6 @@ Return the player with the highest ranking (smallest key). Output: "Jordan" */ public static String getTopPlayer(TreeMap map) { - // TODO: Implement this method return map.firstEntry().getValue(); } @@ -55,7 +52,6 @@ Return the player with the lowest ranking (largest key). Output: "Taylor" */ public static String getLowestPlayer(TreeMap map) { - // TODO: Implement this method return map.lastEntry().getValue(); } @@ -68,7 +64,6 @@ public static String getLowestPlayer(TreeMap map) { Output: player removed */ public static void removePlayer(TreeMap map, int rank) { - // TODO: Implement this method map.remove(rank); } @@ -81,7 +76,6 @@ public static void removePlayer(TreeMap map, int rank) { Output: 3 */ public static Integer getNextRank(TreeMap map, int rank) { - // TODO: Implement this method return rank + 1; } } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index c817d09..c4d31e3 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -4,7 +4,6 @@ public class ArrayDequeProblems { public static void main(String[] args) { - ArrayDeque numbers = new ArrayDeque<>(); addToFront(numbers, 10); @@ -29,9 +28,7 @@ public static void main(String[] args) { Output: [5] */ public static void addToFront(ArrayDeque deque, int value) { - // TODO: Implement this method deque.addFirst(value); - } /* @@ -43,7 +40,6 @@ public static void addToFront(ArrayDeque deque, int value) { Output: [5,10] */ public static void addToBack(ArrayDeque deque, int value) { - // TODO: Implement this method deque.addLast(value); } @@ -57,7 +53,6 @@ public static void addToBack(ArrayDeque deque, int value) { Output: [10,20] */ public static void removeFront(ArrayDeque deque) { - // TODO: Implement this method' deque.removeFirst(); } @@ -71,7 +66,6 @@ public static void removeFront(ArrayDeque deque) { Output: [5,10] */ public static void removeBack(ArrayDeque deque) { - // TODO: Implement this method deque.removeLast(); } @@ -84,7 +78,6 @@ public static void removeBack(ArrayDeque deque) { Output: 5 */ public static Integer peekFront(ArrayDeque deque) { - // TODO: Implement this method return deque.peekFirst(); } @@ -97,7 +90,6 @@ public static Integer peekFront(ArrayDeque deque) { Output: 20 */ public static Integer peekBack(ArrayDeque deque) { - // TODO: Implement this method return deque.peekLast(); } } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 46d2214..31218c1 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -6,7 +6,6 @@ public class DequeProblems { public static void main(String[] args) { // You can test your methods here - ArrayDeque numbers = new ArrayDeque<>(); addFront(numbers, 10); @@ -34,7 +33,6 @@ public static void main(String[] args) { Output: [5] */ public static void addFront(Deque deque, int value) { - // TODO: Implement this method deque.addFirst(value); } @@ -47,7 +45,6 @@ public static void addFront(Deque deque, int value) { Output: [5,10] */ public static void addBack(Deque deque, int value) { - // TODO: Implement this method deque.addLast(value); } @@ -60,7 +57,6 @@ public static void addBack(Deque deque, int value) { Output: 5 */ public static Integer removeFront(Deque deque) { - // TODO: Implement this method return deque.removeFirst(); } @@ -73,7 +69,6 @@ public static Integer removeFront(Deque deque) { Output: 15 */ public static Integer removeBack(Deque deque) { - // TODO: Implement this method return deque.removeLast(); } @@ -98,7 +93,6 @@ public static Integer peekFront(Deque deque) { Output: 15 */ public static Integer peekBack(Deque deque) { - // TODO: Implement this method return deque.peekLast(); } diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index ac000e8..7c046ec 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -7,7 +7,6 @@ public class HashSetProblems { public static void main(String[] args) { - // You can test your methods here Set test = new HashSet<>(); addElement(test, "apple"); @@ -16,8 +15,6 @@ public static void main(String[] args) { removeValue(test, "apple"); System.out.println(test); System.out.println(getUniqueValues(new ArrayList<>(List.of(1, 2, 2, 3, 3)))); - - } /* @@ -29,10 +26,7 @@ public static void main(String[] args) { Output: {"apple"} */ public static void addElement(Set set, String value) { - - // TODO: Implement this method set.add(value); - } /* @@ -44,8 +38,6 @@ public static void addElement(Set set, String value) { Output: true or false */ public static boolean containsValue(Set set, String value) { - - // TODO: Implement this method return set.contains(value); } @@ -58,7 +50,6 @@ public static boolean containsValue(Set set, String value) { Output: value removed */ public static void removeValue(Set set, String value) { - // TODO: Implement this method set.remove(value); } @@ -71,9 +62,6 @@ public static void removeValue(Set set, String value) { Output: 2 */ public static int getUniqueCount(Set set) { - - // TODO: Implement this method - return set.size(); } @@ -86,8 +74,6 @@ public static int getUniqueCount(Set set) { Output: {1,2,3} */ public static Set getUniqueValues(List numbers) { - - // TODO: Implement this method return new HashSet<>(numbers); } } From d3f7f18a31023fb14ea40e3b7576b9ccd67b43a0 Mon Sep 17 00:00:00 2001 From: Jayden Andrews Date: Fri, 6 Mar 2026 11:09:02 -0500 Subject: [PATCH 9/9] Implemented Hackerrank Problems --- .../CollectionsHackerrankProblems.java | 368 ++++++++++-------- 1 file changed, 204 insertions(+), 164 deletions(-) diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 7ae7de1..7af1a60 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -8,33 +8,33 @@ public static void main(String[] args) { // Problem 1 System.out.println("Problem 1 - Remove Duplicates"); - System.out.println(removeDuplicates(new ArrayList<>(List.of(1,2,2,3,4,4,5)))); + System.out.println(removeDuplicates(new ArrayList<>(List.of(1, 2, 2, 3, 4, 4, 5)))); // Expected: [1,2,3,4,5] // Problem 2 System.out.println("\nProblem 2 - Count Frequency"); - System.out.println(countFrequency(new ArrayList<>(List.of(1,2,2,3,3,3)))); + System.out.println(countFrequency(new ArrayList<>(List.of(1, 2, 2, 3, 3, 3)))); // Expected: {1=1, 2=2, 3=3} // Problem 3 System.out.println("\nProblem 3 - First Unique"); - System.out.println(firstUnique(new ArrayList<>(List.of(4,5,1,2,0,4)))); + System.out.println(firstUnique(new ArrayList<>(List.of(4, 5, 1, 2, 0, 4)))); // Expected: 5 // Problem 4 System.out.println("\nProblem 4 - Two Sum"); - System.out.println(twoSum(new ArrayList<>(List.of(2,7,11,15)), 9)); - System.out.println(twoSum(new ArrayList<>(List.of(2,8,11,15)), 9)); + System.out.println(twoSum(new ArrayList<>(List.of(2, 7, 11, 15)), 9)); + System.out.println(twoSum(new ArrayList<>(List.of(2, 8, 11, 15)), 9)); // Expected: true, false // Problem 5 System.out.println("\nProblem 5 - Count Unique Words"); - System.out.println(countUniqueWords(new ArrayList<>(List.of("apple","banana","apple","orange")))); + System.out.println(countUniqueWords(new ArrayList<>(List.of("apple", "banana", "apple", "orange")))); // Expected: 3 // Problem 6 System.out.println("\nProblem 6 - Reverse Queue"); - Queue queue = new LinkedList<>(List.of(1,2,3,4)); + Queue queue = new LinkedList<>(List.of(1, 2, 3, 4)); System.out.println(reverseQueue(queue)); // Expected: [4,3,2,1] @@ -47,202 +47,242 @@ public static void main(String[] args) { // Problem 8 System.out.println("\nProblem 8 - Most Frequent"); - System.out.println(mostFrequent(new ArrayList<>(List.of(1,3,2,3,4,3)))); + System.out.println(mostFrequent(new ArrayList<>(List.of(1, 3, 2, 3, 4, 3)))); // Expected: 3 // Problem 9 System.out.println("\nProblem 9 - Group By Length"); - System.out.println(groupByLength(new ArrayList<>(List.of("cat","dog","elephant","ant")))); + System.out.println(groupByLength(new ArrayList<>(List.of("cat", "dog", "elephant", "ant")))); // Expected: {3=[cat,dog,ant], 8=[elephant]} // Problem 10 System.out.println("\nProblem 10 - Max Sliding Window Sum"); - System.out.println(maxSlidingWindowSum(new ArrayList<>(List.of(2,1,5,1,3,2)), 3)); + System.out.println(maxSlidingWindowSum(new ArrayList<>(List.of(2, 1, 5, 1, 3, 2)), 3)); // Expected: 9 } - /* - Problem 1 - Remove duplicates from a list of integers. + /* + Problem 1 + Remove duplicates from a list of integers. - Example - Input: [1,2,2,3,4,4,5] - Output: [1,2,3,4,5] - */ - public static List removeDuplicates(List numbers) { - return new ArrayList<>(new LinkedHashSet<>(numbers)); - } + Example + Input: [1,2,2,3,4,4,5] + Output: [1,2,3,4,5] + */ + public static List removeDuplicates(List numbers) { + return new ArrayList<>(new LinkedHashSet<>(numbers)); + } - /* - Problem 2 - Count how many times each number appears. - - Example - Input: [1,2,2,3,3,3] - Output: {1=1, 2=2, 3=3} - */ - public static Map countFrequency(List numbers) { - Map answer = new HashMap<>(); - for (Integer num : numbers) { - if (answer.containsKey(num)) { - int value = answer.get(num); - answer.replace(num, value + 1); - } else { - answer.put(num, 1); - } + /* + Problem 2 + Count how many times each number appears. + + Example + Input: [1,2,2,3,3,3] + Output: {1=1, 2=2, 3=3} + */ + public static Map countFrequency(List numbers) { + Map answer = new HashMap<>(); + for (Integer num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); } - return answer; } + return answer; + } - /* - Problem 3 - Return the first number that appears only once. - - Example - Input: [4,5,1,2,0,4] - Output: 5 - */ - public static Integer firstUnique(List numbers) { - LinkedHashMap answer = new LinkedHashMap<>(); - for (Integer num : numbers) { - if (answer.containsKey(num)) { - int value = answer.get(num); - answer.replace(num, value + 1); - } else { - answer.put(num, 1); - } + /* + Problem 3 + Return the first number that appears only once. + + Example + Input: [4,5,1,2,0,4] + Output: 5 + */ + public static Integer firstUnique(List numbers) { + LinkedHashMap answer = new LinkedHashMap<>(); + for (Integer num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); } - for (Integer key : answer.keySet()) { - if (answer.get(key) == 1) { - return key; - } + } + for (Integer key : answer.keySet()) { + if (answer.get(key) == 1) { + return key; } - return 0; } + return 0; + } - /* - Problem 4 - Return true if any two numbers add up to the target. - - Example - numbers = [2,7,11,15] - target = 9 - - Output: true - */ - public static boolean twoSum(List numbers, int target) { - for (int i = 0; i < numbers.size() - 1; i++) { - for (int j = i + 1; j < numbers.size(); j++) { - if (numbers.get(i) + numbers.get(j) == target) { - return true; - } + /* + Problem 4 + Return true if any two numbers add up to the target. + + Example + numbers = [2,7,11,15] + target = 9 + + Output: true + */ + public static boolean twoSum(List numbers, int target) { + for (int i = 0; i < numbers.size() - 1; i++) { + for (int j = i + 1; j < numbers.size(); j++) { + if (numbers.get(i) + numbers.get(j) == target) { + return true; } } - return false; } + return false; + } - /* - Problem 5 - Count how many unique words exist in a list. - - Example - Input: ["apple","banana","apple","orange"] - Output: 3 - */ - public static int countUniqueWords(List words) { - Set unique = new HashSet<>(new HashSet<>(words)); - return unique.size(); - } + /* + Problem 5 + Count how many unique words exist in a list. + + Example + Input: ["apple","banana","apple","orange"] + Output: 3 + */ + public static int countUniqueWords(List words) { + Set unique = new HashSet<>(new HashSet<>(words)); + return unique.size(); + } - /* - Problem 6 - Reverse a queue. - - Example - Input: [1,2,3,4] - Output: [4,3,2,1] - */ - public static Queue reverseQueue(Queue queue) { - Queue answer = new ArrayDeque<>(); - while (!answer.isEmpty()) { - reverseQueue(queue); - answer.add(queue.remove()); - } - return answer; + /* + Problem 6 + Reverse a queue. + + Example + Input: [1,2,3,4] + Output: [4,3,2,1] + */ + public static Queue reverseQueue(Queue queue) { + Deque answer = new ArrayDeque<>(); + while (!queue.isEmpty()) { + answer.addFirst(queue.remove()); } + return answer; + } - /* - Problem 7 - Determine whether parentheses are balanced. - - Example - Input: "(())" - Output: true - - Input: "(()" - Output: false - */ - public static boolean isBalanced(String expression) { - - // TODO: Implement this method - - return false; - } + /* + Problem 7 + Determine whether parentheses are balanced. - /* - Problem 8 - Return the number that appears most frequently in the list. + Example + Input: "(())" ()) [ { + Output: true - Example - Input: [1,3,2,3,4,3] - Output: 3 - */ - public static Integer mostFrequent(List numbers) { + Input: "(()" + Output: false + */ + public static boolean isBalanced(String expression) { + Stack stack = new Stack<>(); - // TODO: Implement this method + for (int i = 0; i < expression.length(); i++) { + char current = expression.charAt(i); - return null; + if (current == '(') { + stack.push(current); + } else if (current == ')') { + if (stack.isEmpty()) { + return false; + } + stack.pop(); + } } + return stack.isEmpty(); + } - /* - Problem 9 - Group words based on their length. - - Example - Input: ["cat","dog","elephant","ant"] - - Output: - { - 3 = ["cat","dog","ant"], - 8 = ["elephant"] + /* + Problem 8 + Return the number that appears most frequently in the list. + + Example + Input: [1,3,2,3,4,3] + Output: 3 + */ + public static Integer mostFrequent(List numbers) { + HashMap numberCount = new HashMap<>(); + int max = Integer.MIN_VALUE; + int answer = 0; + for (Integer num : numbers) { + if (numberCount.containsKey(num)) { + int value = numberCount.get(num); + numberCount.replace(num, value + 1); + } else { + numberCount.put(num, 1); + } } - */ - public static Map> groupByLength(List words) { - - // TODO: Implement this method - return null; + for (Integer key : numberCount.keySet()) { + int currentValue = numberCount.get(key); + if (numberCount.get(key) > max) { + max = currentValue; + answer = key; + } } + return answer; + } - /* - Problem 10 - Return the maximum sum of any window of size k. + /* + Problem 9 + Group words based on their length. - Example - numbers = [2,1,5,1,3,2] - k = 3 + Example + Input: ["cat","dog","elephant","ant"] - Output: 9 - */ - public static int maxSlidingWindowSum(List numbers, int k) { - int windowStart = 0; - int windowEnd = k; - int max = Integer.MIN_VALUE; - while (k != numbers.size()) { - for (int i = windowStart; i < k; i++) { + Output: + { + 3 = ["cat","dog","ant"], + 8 = ["elephant"] + } + */ + public static Map> groupByLength(List words) { + Map> answer = new HashMap<>(); + for (String word : words) { + int length = word.length(); + if (answer.containsKey(length)) { + List update = answer.get(length); + update.add(word); + answer.replace(length, update); + } else { + answer.put(length, new ArrayList<>(List.of(word))); + } + } + return answer; + } - } + /* + Problem 10 + Return the maximum sum of any window of size k. + + Example + numbers = [2,1,5,1,3,2] + k = 3 + + Output: 9 + */ + public static int maxSlidingWindowSum(List numbers, int k) { + int windowStart = 0; + int windowEnd = k; + int max = Integer.MIN_VALUE; + while (windowEnd != numbers.size()) { + int currentSum = 0; + for (int i = windowStart; i < windowEnd; i++) { + currentSum += numbers.get(i); + } + if (currentSum > max) { + max = currentSum; } - return 0; - } + windowStart++; + windowEnd++; + } + return max; + } }