From 1b70a552b6a799db645c8789ebdb8be6e38f851f Mon Sep 17 00:00:00 2001 From: Jonus Date: Tue, 7 Apr 2026 15:03:07 -0700 Subject: [PATCH 1/8] Problem one and two, initial commit --- src/Practice.java | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..d635356 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,7 +12,19 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if(nums == null){ + return 0; + } + + int sum = 0; + + for(int i = 0; i < nums.length; i++){ + if(nums[i] % 2 != 0){ + sum += nums[i]; + } + } + + return sum; } /** @@ -27,7 +39,27 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + + if(words.size() == 0){ + throw new IllegalArgumentException("Cant be empty"); + } + + String shortestWord = ""; + int shortest = Integer.MAX_VALUE; + + for (String word : words) { + if(word.length() < shortest){ + shortestWord = word; + shortest = word.length(); + } else if(word.length() == shortest && shortestWord.charAt(0) == word.charAt(0)){ + if(word.compareTo(shortestWord) < 0){ + shortestWord = word; + shortest = word.length(); + } + } + } + + return shortestWord; } /** From be6a895c30aeee053b235d3e00b50f8b83c15bd2 Mon Sep 17 00:00:00 2001 From: Jonus Date: Tue, 7 Apr 2026 22:10:18 -0700 Subject: [PATCH 2/8] Frequency Map and Level Count complete --- src/Practice.java | 51 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index d635356..cf4a034 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,6 +1,8 @@ +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.HashMap; public class Practice { /** @@ -72,7 +74,17 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if(ages == null){ + throw new NullPointerException("Cant be empty"); + } + Set result = new HashSet<>(); + + for(Map.Entry entry : ages.entrySet()){ + if(entry.getValue() >= 18) { + result.add(entry.getKey()); + } + } + return result; } /** @@ -83,7 +95,20 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + if(head == null){ + throw new IllegalArgumentException("Cant be null"); + } + + int biggest = Integer.MIN_VALUE; + + while(head != null){ + if(head.data > biggest){ + biggest = head.data; + } + head = head.next; + } + + return biggest; } /** @@ -100,7 +125,18 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + if(head == null){ + return Map.of(); + } + + Map frequencyMap = new HashMap<>(); + + while(head != null){ + frequencyMap.put(head.data, frequencyMap.getOrDefault(head.data, 0) + 1); + head = head.next; + } + + return frequencyMap; } @@ -113,7 +149,14 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + if(root == null){ + return 0; + } + + int leftDepth = levelCount(root.left); + int rightDepth = levelCount(root.right); + + return Math.max(leftDepth, rightDepth) + 1; } From e7bbd1d0b31ed12ac7b193edd6854a54617ebaac Mon Sep 17 00:00:00 2001 From: Jonus Date: Tue, 7 Apr 2026 22:15:11 -0700 Subject: [PATCH 3/8] sumAtLevel implemented --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index cf4a034..e033da1 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -184,7 +184,18 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { - return 0; + if(root == null){ + return 0; + } + + if(level == 1){ + return root.data; + } + + int leftSum = sumAtLevel(root.left, level - 1); + int rightSum = sumAtLevel(root.right, level - 1); + + return leftSum + rightSum; } From 481452464599cc02681bc58613a9dc40fa13ee26 Mon Sep 17 00:00:00 2001 From: Jonus Date: Wed, 8 Apr 2026 10:58:05 -0700 Subject: [PATCH 4/8] sumMatch Implemented --- src/Practice.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e033da1..54ad958 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -210,7 +210,30 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + if(root == null && head == null){ + return true; + } + + int treeSum = treeSum(root); + int listSum = 0; + + while(head != null){ + listSum += head.data; + head = head.next; + } + + return treeSum == listSum; + } + + private static int treeSum(BinaryTreeNode node) { + if(node == null){ + return 0; + } + + int leftSum = treeSum(node.left); + int rightSum = treeSum(node.right); + + return node.data + leftSum + rightSum; } /** From d307ea65623a1e526a012d6f4a50e2f745ea4e04 Mon Sep 17 00:00:00 2001 From: Jonus Date: Wed, 8 Apr 2026 12:15:22 -0700 Subject: [PATCH 5/8] nbSum implemented --- src/Practice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 54ad958..9d92a8e 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -245,7 +245,15 @@ private static int treeSum(BinaryTreeNode node) { * @return the sum of all the tree's values */ public static int nbSum(TreeNode root) { - return 0; + if(root == null){ + return 0; + } + + int sum = root.data; + for(TreeNode child : root.children){ + sum += nbSum(child); + } + return sum; } /** From 3a17d24a1d20ab60b39b2f2d046909937e58ef73 Mon Sep 17 00:00:00 2001 From: Jonus Date: Wed, 8 Apr 2026 12:28:58 -0700 Subject: [PATCH 6/8] onlychildcount implemented --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 9d92a8e..bf3e2db 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -285,7 +285,20 @@ public static int nbSum(TreeNode root) { * @return the count of nodes that do not have siblings, EXCLUDING THE ROOT */ public static int onlyChildCount(TreeNode root) { - return 0; + if(root == null){ + return 0; + } + + int count = 0; + + for(TreeNode child : root.children){ + if(root.children.size() == 1){ + count++; + } + count += onlyChildCount(child); + } + + return count; } /** From 578b1e1ae6a31b2eb5a4d1a03bb8f9a5a515e198 Mon Sep 17 00:00:00 2001 From: Jonus Date: Wed, 8 Apr 2026 12:32:16 -0700 Subject: [PATCH 7/8] maxDepth unimplemented --- src/Practice.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index bf3e2db..0fc41fc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -336,6 +336,12 @@ public static int onlyChildCount(TreeNode root) { * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree */ public static int maxDepth(Map> tree, T root) { + if(tree == null){ + return 0; + } + + + return 0; } } \ No newline at end of file From a4bb7282a5aefe15ba18a730b69a9b170a7fc603 Mon Sep 17 00:00:00 2001 From: Jonus Date: Wed, 8 Apr 2026 21:11:44 -0700 Subject: [PATCH 8/8] brute force max depth --- src/Practice.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 0fc41fc..132a419 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -336,12 +336,23 @@ public static int onlyChildCount(TreeNode root) { * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree */ public static int maxDepth(Map> tree, T root) { - if(tree == null){ + if(tree == null || root == null || !tree.containsKey(root)){ return 0; } - + List children = tree.get(root); + + if(children == null || children.size() == 0){ + return 1; + } + int maxDepth = 1; + for(T child : children){ + int depth = maxDepth(tree, child); + if(depth > 0){ + maxDepth = Math.max(maxDepth, depth); + } + } - return 0; + return maxDepth + 1; } } \ No newline at end of file