From ceb3032dfd66c92ce06c97867fd1e809ce784d43 Mon Sep 17 00:00:00 2001 From: wilfredainsworth Date: Fri, 6 Mar 2026 19:07:02 -0500 Subject: [PATCH 1/5] Problems 1-4 completed. Unsure why LinkedListProblems.java is throwing an IndexOutOfBoundsException --- src/main/java/Hierachy/PRACTICE.md | 22 +++++++--- .../Iterable/Examples/ForEachLoopDemo.java | 12 ++++-- .../java/Iterable/Examples/IteratorDemo.java | 14 ++++++- .../Iterable/Practice/IterableWarmups.java | 32 +++++++++++--- src/main/java/Iterable/README.md | 4 +- .../Lists/ArrayLists/ArrayListProblems.java | 42 ++++++++++++++----- .../Lists/LinkedLists/LinkedListProblems.java | 15 +++---- 7 files changed, 105 insertions(+), 36 deletions(-) diff --git a/src/main/java/Hierachy/PRACTICE.md b/src/main/java/Hierachy/PRACTICE.md index 2b885d9..ec51292 100644 --- a/src/main/java/Hierachy/PRACTICE.md +++ b/src/main/java/Hierachy/PRACTICE.md @@ -16,6 +16,7 @@ B. Iterable C. List D. Queue +Answer: **B Iterable** --- ## Question 2 @@ -27,6 +28,7 @@ B. Collection C. Map D. Deque +Answer: **B Collection** --- ## Question 3 @@ -38,6 +40,8 @@ B. Set C. Queue D. Map +Answer: **D Map** + --- ## Question 4 @@ -49,6 +53,7 @@ B. List C. Queue D. TreeSet +Answer: **B List** --- ## Question 5 @@ -60,17 +65,18 @@ B. Set C. Queue D. Map +Answer: **D Map** --- # True or False Write **True** or **False**. -1. `ArrayList` implements the `List` interface. -2. `Set` allows duplicate elements. -3. `Map` stores elements using keys and values. -4. `Queue` typically follows FIFO behavior. -5. `Iterable` allows collections to be used in enhanced for-loops. +1. `ArrayList` implements the `List` interface. TRUE +2. `Set` allows duplicate elements. FALSE +3. `Map` stores elements using keys and values. TRUE +4. `Queue` typically follows FIFO behavior. TRUE +5. `Iterable` allows collections to be used in enhanced for-loops. TRUE --- @@ -80,12 +86,14 @@ Write **True** or **False**. What is the difference between an **interface** and a **class** in the Java Collections Framework? +An interface defines behaviors(what can be done), and a class implements the behaviors and does the work. --- ## Question 2 Why is it recommended to declare variables using interfaces like `List` instead of concrete classes like `ArrayList`? +It proves better flexibility, code design, and Abstraction. Example: ```java @@ -98,6 +106,7 @@ List list = new ArrayList<>(); Name three interfaces that extend `Collection`. +List, Set, and Queue --- # Code Reading Exercise @@ -118,8 +127,11 @@ for (String fruit : fruits) { Questions: 1. Which interface type is used for the variable? + **List** 2. Which class is used as the implementation? + **ArrayList** 3. Why can the enhanced for-loop be used here? + **List extends Collections which extends iterable, so the enhanced for-loop can be used on all collections.** --- diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..2cf4f5d 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -16,11 +16,15 @@ public static void main(String[] args) { System.out.println("Printing students using a for-each loop:"); // TODO: - // Use a for-each loop to print each student name + for (String x: students){ + System.out.println(x); +} System.out.println("\nPrinting students in uppercase:"); - + for (String x: students){ + System.out.println(x.toUpperCase()); + } // TODO: // Use a for-each loop to print each name in uppercase @@ -28,7 +32,9 @@ public static void main(String[] args) { System.out.println("\nCount the number of students:"); int count = 0; - + for (String x: students){ + count=students.size(); + } // TODO: // Use a for-each loop to count how many students are in the list diff --git a/src/main/java/Iterable/Examples/IteratorDemo.java b/src/main/java/Iterable/Examples/IteratorDemo.java index b09fca7..0a6b937 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -26,6 +26,13 @@ public static void main(String[] args) { // TODO: // Use iterator.hasNext() and iterator.next() // Print each number + while(iterator.hasNext()){ + int x = iterator.next(); + System.out.println(x); + + } + + System.out.println("\nRemoving odd numbers using Iterator"); @@ -35,7 +42,12 @@ public static void main(String[] args) { // TODO: // Use iterator to remove odd numbers // Remember: use iterator.remove() - + while(iterator.hasNext()){ + int x = iterator.next(); + if (x%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..a79c38f 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -14,9 +14,17 @@ public static void main(String[] args) { numbers.add(4); numbers.add(8); + List words = new ArrayList<>(); + words.add("fun"); + words.add("funny"); + words.add("funny money"); + System.out.println("Sum: " + sum(numbers)); System.out.println("Even count: " + countEven(numbers)); System.out.println("Max value: " + findMax(numbers)); + System.out.println("Count Matches: " + countMatches(words,"fun")); + + } @@ -27,10 +35,11 @@ public static void main(String[] args) { public static int sum(Iterable numbers) { int total = 0; - + for (int x: numbers){ + total+=x; + } // TODO: // Use a for-each loop to calculate the sum - return total; } @@ -43,10 +52,14 @@ public static int countEven(Iterable numbers) { int count = 0; + for (int c: numbers){ + if (c%2==0){ + count++; + } + } // TODO: // Loop through numbers // Increment count if number is even - return count; } @@ -58,7 +71,11 @@ public static int countEven(Iterable numbers) { public static int findMax(Iterable numbers) { int max = Integer.MIN_VALUE; - +for (int x: numbers){ + if (x>max){ + max=x; + } +} // TODO: // Loop through numbers // Update max if current number is larger @@ -74,11 +91,14 @@ public static int findMax(Iterable numbers) { public static int countMatches(Iterable words, String target) { int count = 0; - + for (String x: words){ + if (x.equals(target)){ + count++; + } + } // TODO: // Loop through words // Compare each word to target - return count; } } diff --git a/src/main/java/Iterable/README.md b/src/main/java/Iterable/README.md index 858f7e3..d7b9fbd 100644 --- a/src/main/java/Iterable/README.md +++ b/src/main/java/Iterable/README.md @@ -81,8 +81,8 @@ it.remove(); // safely removes current element ## Time Complexity | Operation | Complexity | -| -------------- | ---------- | -| Full traversal | O(n) | +| -------------- |-----------| +| Full traversal | O(n) | Where `n` = number of elements. diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..5aeb801 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -34,8 +34,11 @@ public static void main(String[] args) { public static int sum(List nums) { // TODO: Implement this method - - return 0; + int a = 0; + for (int b : nums) { + a += b; + } + return a; } /* @@ -49,8 +52,14 @@ public static int sum(List nums) { public static int countEvens(List nums) { // TODO: Implement this method - - return 0; + int evenCount = 0; + for (int a : nums) { + evenCount = 0; + if (a % 2 == 0) { + evenCount++; + } + } + return evenCount; } /* @@ -67,7 +76,11 @@ public static int countEvens(List nums) { public static boolean hasDuplicate(List nums) { // TODO: Implement this method - + for (int a : nums) { + if (nums.contains(a)) { + return true; + } + } return false; } @@ -82,8 +95,13 @@ public static boolean hasDuplicate(List nums) { public static int findMax(List nums) { // TODO: Implement this method - - return 0; + int max = 0; + for (int a : nums) { + if (a > max) { + max = a; + } + } + return max; } /* @@ -97,9 +115,13 @@ public static int findMax(List nums) { The original list should remain unchanged. */ public static List reverse(List nums) { - + List rev = nums.reversed(); // TODO: Implement this method - - return null; +// List rev = List.of(); +// while (nums.listIterator().hasPrevious()) { +// rev = nums.reversed(); +// } + return rev; } } + diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..df82639 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -36,7 +36,7 @@ public static void main(String[] args) { Output: [5,10,20,30] */ public static void addToFront(LinkedList list, int value) { - + list.addFirst(value); // TODO: Implement this method } @@ -50,7 +50,7 @@ public static void addToFront(LinkedList list, int value) { Output: [10,20,30,40] */ public static void addToEnd(LinkedList list, int value) { - + list.add(list.getLast(),value); // TODO: Implement this method } @@ -64,7 +64,7 @@ public static void addToEnd(LinkedList list, int value) { Output: [20,30] */ public static void removeFirstElement(LinkedList list) { - + list.removeFirst(); // TODO: Implement this method } @@ -78,7 +78,7 @@ public static void removeFirstElement(LinkedList list) { Output: [10,20] */ public static void removeLastElement(LinkedList list) { - + list.remove(list.getLast()); // TODO: Implement this method } @@ -92,10 +92,8 @@ public static void removeLastElement(LinkedList list) { Output: 10 */ public static int getFirstElement(LinkedList list) { - +return list.getFirst(); // TODO: Implement this method - - return 0; } /* @@ -107,9 +105,8 @@ public static int getFirstElement(LinkedList list) { Output: 30 */ public static int getLastElement(LinkedList list) { - + return list.getLast(); // TODO: Implement this method - return 0; } } From 77d1d7e09175427427b5f574d24df2dcfe36569b Mon Sep 17 00:00:00 2001 From: wilfredainsworth Date: Fri, 6 Mar 2026 20:12:58 -0500 Subject: [PATCH 2/5] HashSetProblems.java completed. --- .../java/Sets/HashSet/HashSetProblems.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..a0bee0d 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,30 @@ public class HashSetProblems { public static void main(String[] args) { // You can test your methods here + HashSet set = new HashSet<>(); + List nums = new ArrayList<>(); + + + addElement(set,"Fred"); + addElement(set,"Fred"); + addElement(set,"Fredd"); + addElement(set,"Freddy"); + + System.out.println(containsValue(set,"Fred")); + + System.out.println(getUniqueCount(set)); + + nums.add(10); + nums.add(20); + nums.add(30); + nums.add(30); + nums.add(40); + + System.out.println(set); + System.out.println((nums)); + System.out.println(getUniqueValues(nums)); + removeValue(set,"Fred"); + System.out.println(set); } @@ -20,6 +46,7 @@ public static void main(String[] args) { */ public static void addElement(Set set, String value) { + set.add(value); // TODO: Implement this method } @@ -33,10 +60,9 @@ public static void addElement(Set set, String value) { Output: true or false */ public static boolean containsValue(Set set, String value) { - + return set.contains(value); // TODO: Implement this method - return false; } /* @@ -49,6 +75,7 @@ public static boolean containsValue(Set set, String value) { */ public static void removeValue(Set set, String value) { + set.remove(value); // TODO: Implement this method } @@ -63,9 +90,9 @@ public static void removeValue(Set set, String value) { */ public static int getUniqueCount(Set set) { + return set.size(); // TODO: Implement this method - return 0; } /* @@ -78,8 +105,10 @@ public static int getUniqueCount(Set set) { */ public static Set getUniqueValues(List numbers) { + Set uniqueValues = new HashSet<>(numbers); + + return uniqueValues; // TODO: Implement this method - return null; } } From 42b0a38634daee99a3c70f626aec1124a5653b16 Mon Sep 17 00:00:00 2001 From: wilfredainsworth Date: Fri, 6 Mar 2026 21:16:12 -0500 Subject: [PATCH 3/5] DequeProblems.java completed. --- .../Queues/ArrayDeque/ArrayDequeProblems.java | 16 +++++------- src/main/java/Queues/Deque/DequeProblems.java | 26 ++++++++++++++----- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..8c51e78 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -29,9 +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,7 +42,7 @@ public static void addToFront(ArrayDeque deque, int value) { Output: [5,10] */ public static void addToBack(ArrayDeque deque, int value) { - + deque.addLast(value); // TODO: Implement this method } @@ -57,7 +56,7 @@ public static void addToBack(ArrayDeque deque, int value) { Output: [10,20] */ public static void removeFront(ArrayDeque deque) { - + deque.removeFirst(); // TODO: Implement this method } @@ -71,7 +70,7 @@ public static void removeFront(ArrayDeque deque) { Output: [5,10] */ public static void removeBack(ArrayDeque deque) { - + deque.removeLast(); // TODO: Implement this method } @@ -85,10 +84,9 @@ public static void removeBack(ArrayDeque deque) { Output: 5 */ public static Integer peekFront(ArrayDeque deque) { - + return deque.peekFirst(); // TODO: Implement this method - return null; } /* @@ -100,9 +98,7 @@ public static Integer peekFront(ArrayDeque deque) { Output: 20 */ public static Integer peekBack(ArrayDeque deque) { - + return deque.peekLast(); // TODO: Implement this method - - return null; } } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..953af94 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -1,11 +1,24 @@ 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 + Deque deque = new ArrayDeque<>(); + deque.addFirst(90); + + addFront(deque,100); + deque.addLast(1000); + + addBack(deque,120); +// removeFront(deque); +// removeFront(deque); +// removeBack(deque); + + System.out.println(peekFront(deque)); + System.out.println(peekBack(deque)); } @@ -18,7 +31,7 @@ public static void main(String[] args) { Output: [5] */ public static void addFront(Deque deque, int value) { - + deque.addFirst(value); // TODO: Implement this method } @@ -33,6 +46,7 @@ public static void addFront(Deque deque, int value) { */ public static void addBack(Deque deque, int value) { + deque.addLast(value); // TODO: Implement this method } @@ -47,9 +61,9 @@ public static void addBack(Deque deque, int value) { */ public static Integer removeFront(Deque deque) { + return deque.removeFirst(); // TODO: Implement this method - return null; } /* @@ -64,7 +78,7 @@ public static Integer removeBack(Deque deque) { // TODO: Implement this method - return null; + return deque.removeLast(); } /* @@ -79,7 +93,7 @@ public static Integer peekFront(Deque deque) { // TODO: Implement this method - return null; + return deque.peekFirst(); } /* @@ -94,7 +108,7 @@ public static Integer peekBack(Deque deque) { // TODO: Implement this method - return null; + return deque.peekFirst(); } } From 8a003c1acab3bcf25fb417a055a0e19bc58d9c46 Mon Sep 17 00:00:00 2001 From: wilfredainsworth Date: Fri, 6 Mar 2026 23:40:06 -0500 Subject: [PATCH 4/5] DequeProblems.java completed. --- .../CollectionsHackerrankProblems.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..d1fe483 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,14 +1,19 @@ 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 class CollectionsHackerrankPractice { public static void main(String[] args) { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(3); + + System.out.println(list); + System.out.println(removeDuplicates(list)); // You can test your methods here } @@ -22,10 +27,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 HashSet<>(numbers)); } /* @@ -37,8 +39,12 @@ public static List removeDuplicates(List numbers) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { + Map map = new HashMap<>(); + + for (int num: numbers){ + + } - // TODO: Implement this method return null; } @@ -175,4 +181,4 @@ public static int maxSlidingWindowSum(List numbers, int k) { return 0; } } -} +} \ No newline at end of file From c7e96d3223aaa5beaa3377a2e93ac269bd89782c Mon Sep 17 00:00:00 2001 From: wilfredainsworth Date: Mon, 9 Mar 2026 13:21:31 -0400 Subject: [PATCH 5/5] Hackerrank completed --- .../CollectionsHackerrankProblems.java | 128 ++++++++++++++---- 1 file changed, 104 insertions(+), 24 deletions(-) diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index d1fe483..4f891fa 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -9,11 +9,18 @@ public static void main(String[] args) { List list = new ArrayList<>(); list.add(1); list.add(2); + list.add(2); + list.add(3); list.add(3); list.add(3); + list.add(33); + list.add(33); + list.add(33); System.out.println(list); System.out.println(removeDuplicates(list)); + System.out.println(countFrequency(list)); + System.out.println(firstUnique(list)); // You can test your methods here } @@ -41,12 +48,10 @@ public static List removeDuplicates(List numbers) { public static Map countFrequency(List numbers) { Map map = new HashMap<>(); - for (int num: numbers){ - + for( int i : numbers){ + map.put(i,map.getOrDefault(i,0)+1); } - - - return null; + return map; } /* @@ -58,9 +63,14 @@ public static Map countFrequency(List numbers) { Output: 5 */ public static Integer firstUnique(List numbers) { - - // TODO: Implement this method - + Map map = new LinkedHashMap<>(); + for(int i: numbers){ + map.put(i,map.getOrDefault(numbers,0)); + } + for (Map.Entryentry: map.entrySet()) + if(entry.getValue()==1){ + return entry.getKey(); + } return null; } @@ -75,8 +85,17 @@ public static Integer firstUnique(List numbers) { Output: true */ public static boolean twoSum(List numbers, int target) { + Set seen = new HashSet<>(); - // TODO: Implement this method + for (Integer num : numbers) { + int complement = target - num; + + if (seen.contains(complement)) { + return true; + } + + seen.add(num); + } return false; } @@ -90,10 +109,9 @@ public static boolean twoSum(List numbers, int target) { Output: 3 */ public static int countUniqueWords(List words) { + Set uniqueWords = new HashSet<>(words); - // TODO: Implement this method - - return 0; + return uniqueWords.size(); } /* @@ -106,9 +124,19 @@ public static int countUniqueWords(List words) { */ public static Queue reverseQueue(Queue queue) { - // TODO: Implement this method + Stack stack = new Stack<>(); - return null; + // Move elements from queue to stack + while (!queue.isEmpty()) { + stack.push(queue.poll()); + } + + // Move elements back to queue (reversed order) + while (!stack.isEmpty()) { + queue.offer(stack.pop()); + } + + return queue; } /* @@ -123,10 +151,27 @@ public static Queue reverseQueue(Queue queue) { Output: false */ public static boolean isBalanced(String expression) { +//use a Stack. +//A stack works well because we must match every opening ( with a closing ) in the correct order. + Stack stack = new Stack<>(); + + for (char ch : expression.toCharArray()) { + + if (ch == '(') { + stack.push(ch); // store opening parenthesis + } + + else if (ch == ')') { + if (stack.isEmpty()) { + return false; // no matching opening + } + stack.pop(); // match found + } + } - // TODO: Implement this method + // if stack is empty, parentheses are balanced + return stack.isEmpty(); - return false; } /* @@ -138,10 +183,27 @@ public static boolean isBalanced(String expression) { Output: 3 */ public static Integer mostFrequent(List numbers) { + int max = numbers.getFirst(); + int maxKey = numbers.getFirst(); + Map map = new HashMap<>(); - // TODO: Implement this method + for (Integer number : numbers) { + if (map.containsKey(number)) { + map.put(number, map.get(number) + 1); + }else{ + map.put(number, 1); + } + } - return null; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() > max) { + max = entry.getValue(); + maxKey = entry.getKey(); + } + + + } + return maxKey; } /* @@ -158,10 +220,18 @@ public static Integer mostFrequent(List numbers) { } */ public static Map> groupByLength(List words) { + Map> result = new HashMap<>(); + for (String word : words) { + int length = word.length(); - // TODO: Implement this method + if (!result.containsKey(length)) { + result.put(length, new ArrayList<>()); + } - return null; + result.get(length).add(word); + } + + return result; } /* @@ -175,10 +245,20 @@ public static Map> groupByLength(List words) { Output: 9 */ public static int maxSlidingWindowSum(List numbers, int k) { - - // TODO: Implement this method - - return 0; + if (numbers.size() < k){ + return numbers.getFirst(); + } + int max = Integer.MIN_VALUE; + for (int i = 0; i < numbers.size()-k+1; i++) { + int maxTest = 0; + for(int j = i; j < i+k; j++){ + maxTest = numbers.get(j) + maxTest; + } + if (maxTest > max) { + max = maxTest; + } + } + return max; } } } \ No newline at end of file