From 59a7c51ce3527047a97835a847717bbdd48839d7 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 20:50:56 -0700 Subject: [PATCH 01/11] #1 done --- src/Practice.java | 94 ++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index ca8e22b..8492d7a 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -12,7 +12,18 @@ 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 num : nums) { + if (num % 2 != 0) { + sum += num; + } + } + + return sum; } /** @@ -24,7 +35,7 @@ public static int oddSum(int[] nums) { * @param words a set of words * @return the shortest word in the set with a lexicographic tiebreaker * @throws IllegalArgumentException if words is empty - * @throws NullPointerException if words is null + * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { return null; @@ -55,15 +66,16 @@ public static int biggestNumber(ListNode head) { } /** - * Returns a frequency map counting how frequently items appear in a linked list. + * Returns a frequency map counting how frequently items appear in a linked + * list. * * Example: - * Input: a -> x -> a -> a -> x -> y - * Output: {a:3, x:2, y:1} + * Input: a -> x -> a -> a -> x -> y + * Output: {a:3, x:2, y:1} * * Returns an empty map if head is null * - * @param the type of data held by the list + * @param the type of data held by the list * @param head the head of the list * @return a frequency map of values in the list */ @@ -71,7 +83,6 @@ public static Map frequencies(ListNode head) { return null; } - /** * Returns the number of levels in the tree. * @@ -84,27 +95,26 @@ public static int levelCount(BinaryTreeNode root) { return 0; } - /** * Returns the sum at a specified level in a binary tree. * * For example, if the given level was 3: - * 5 - * / \ - * 8 4 - * / \ / - * 7 9 2 - * / - * 1 + * 5 + * / \ + * 8 4 + * / \ / + * 7 9 2 + * / + * 1 * * Nodes at level 3: 7, 9, and 2 - * Sum of nodes at level 3: 18 + * Sum of nodes at level 3: 18 * * The root is considered to be at level 1. * * Returns 0 if the tree is empty or if the level is not present in the tree. * - * @param root the root of the binary tree + * @param root the root of the binary tree * @param level the level to sum * @return the sum of the nodes at the given level */ @@ -112,10 +122,9 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { return 0; } - /** * Returns true if the sum of the values in a given tree is equal to the sum - * of the values in the given list. + * of the values in the given list. * * An empty tree or list is considered to have a sum of 0. * @@ -140,18 +149,20 @@ public static int nbSum(TreeNode root) { } /** - * Returns the count of nodes in a non-binary tree that are only children, EXCLUDING the root. + * Returns the count of nodes in a non-binary tree that are only children, + * EXCLUDING the root. * - * In other words, how many nodes in the tree do NOT have siblings, NOT INCLUDING THE ROOT. + * In other words, how many nodes in the tree do NOT have siblings, NOT + * INCLUDING THE ROOT. * * Example: - * A - * / | \ - * B C D - * / / \ | - * E F X G - * \ - * H + * A + * / | \ + * B C D + * / / \ | + * E F X G + * \ + * H * * Only children: E, G, and H * - E is an only child because B has exactly one child @@ -176,20 +187,20 @@ public static int onlyChildCount(TreeNode root) { * * Example map: * { - * A=[B, C, D], - * B=[E, F], - * D=[G], - * G=[H] + * A=[B, C, D], + * B=[E, F], + * D=[G], + * G=[H] * } * * Tree represented by the map: - * A - * / | \ - * B C D - * / \ | - * E F G - * \ - * H + * A + * / | \ + * B C D + * / \ | + * E F G + * \ + * H * * The longest path from the root to a leaf is: * A -> D -> G -> H @@ -200,10 +211,11 @@ public static int onlyChildCount(TreeNode root) { * * The tree is represented as a map of parent values to lists of children. * - * @param the type of the data in the tree + * @param the type of the data in the tree * @param tree a map of parent values to lists of children * @param root the root value of the tree - * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree + * @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) { return 0; From b44cfb9cf99da1a1156a051eb52adeceb0f2b842 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 21:02:05 -0700 Subject: [PATCH 02/11] #2 finished --- src/Practice.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8492d7a..8d4d49c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -38,7 +38,29 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if (words == null) { + throw new NullPointerException(); + } + + if (words.isEmpty()) { + throw new IllegalArgumentException(); + } + + String shortest = null; + + for (String word : words) { + if (shortest == null) { + shortest = word; + } else if (word.length() < shortest.length()) { + shortest = word; + } else if (word.length() == shortest.length()) { + if (word.compareTo(shortest) < 0) { + shortest = word; + } + } + } + + return shortest; } /** From cc9e1ca4afe7aa9b269f5be2270a7295900f1cb4 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 21:14:31 -0700 Subject: [PATCH 03/11] #3 finished --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8d4d49c..0bbb0fd 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -73,7 +74,19 @@ 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(); + } + + Set result = new HashSet<>(); + + for (String name : ages.keySet()) { + if (ages.get(name) >= 18) { + result.add(name); + } + } + + return result; } /** From 3057fcb4a2c8e7c4521c081ed3e6096e2ee27819 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 21:21:44 -0700 Subject: [PATCH 04/11] #4 finished --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 0bbb0fd..0000628 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -97,7 +97,21 @@ 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(); + } + + int biggest = head.data; + ListNode current = head; + + while (current != null) { + if (current.data > biggest) { + biggest = current.data; + } + current = current.next; + } + + return biggest; } /** From 04313ffd5bb3899b2145b4171925a7b0999d1daa Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 21:30:01 -0700 Subject: [PATCH 05/11] #5 finished --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 0000628..039abf4 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -129,7 +130,20 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map result = new HashMap<>(); + + ListNode current = head; + + while (current != null) { + if (result.containsKey(current.data)) { + result.put(current.data, result.get(current.data) + 1); + } else { + result.put(current.data, 1); + } + current = current.next; + } + + return result; } /** From e2f65d8e601bf47d2865a4c91b1324e59c0aa453 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 21:40:11 -0700 Subject: [PATCH 06/11] #6 finished --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 039abf4..7e53381 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -155,7 +155,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 leftCount = levelCount(root.left); + int rightCount = levelCount(root.right); + + return Math.max(leftCount, rightCount) + 1; } /** From 8f7551e58f6c010a72b2b10c7fca341b6ea96cf2 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 21:49:31 -0700 Subject: [PATCH 07/11] #7 finished --- src/Practice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7e53381..5852f47 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -189,7 +189,15 @@ 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 || level <= 0) { + return 0; + } + + if (level == 1) { + return root.data; + } + + return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); } /** From 8f021f982f9c2dc848b7a3072731a00473e68670 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 22:06:35 -0700 Subject: [PATCH 08/11] #8 finished --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 5852f47..8bcf13c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -211,7 +211,26 @@ 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; + int listSum = 0; + + ListNode current = head; + + while (current != null) { + listSum += current.data; + current = current.next; + } + + int treeSum = sumTree(root); + + return treeSum == listSum; + } + + private static int sumTree(BinaryTreeNode root) { + if (root == null) { + return 0; + } + + return sumTree(root.left) + sumTree(root.right) + root.data; } /** From cb0c01b90ead71dc2725eae4be8010ec3c702e1f Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 22:18:55 -0700 Subject: [PATCH 09/11] #9 finished --- src/Practice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8bcf13c..82e90c0 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -242,7 +242,17 @@ private static int sumTree(BinaryTreeNode root) { * @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 2d345d8629b033aedc848f73f44f8e04f4e38e5a Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 22:26:39 -0700 Subject: [PATCH 10/11] #10 finished --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 82e90c0..66f0030 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -286,7 +286,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 1f251177b9c70808556cbf21592927cba825de73 Mon Sep 17 00:00:00 2001 From: Elvin Hrytsyuk Date: Tue, 7 Apr 2026 22:55:44 -0700 Subject: [PATCH 11/11] #11 finished --- src/Practice.java | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 66f0030..bf5ead3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -338,6 +338,32 @@ public static int onlyChildCount(TreeNode root) { * present in the tree */ public static int maxDepth(Map> tree, T root) { - return 0; + if (tree == null || root == null || !tree.containsKey(root)) { + return 0; + } + + List children = tree.get(root); + + if (children == null || children.isEmpty()) { + return 1; + } + + int max = 0; + + for (T child : children) { + int depth; + + if (tree.containsKey(child)) { + depth = maxDepth(tree, child); + } else { + depth = 1; + } + + if (depth > max) { + max = depth; + } + } + + return max + 1; } } \ No newline at end of file