Skip to content
152 changes: 144 additions & 8 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

public class Practice {
/**
Expand All @@ -11,7 +18,17 @@ public class Practice {
* @return the sum of the odd numbers in the array
*/
public static int oddSum(int[] nums) {
return 0;
int oddTotal = 0;
if (nums == null || nums.length == 0) {
return 0;
}

for (int i: nums) {
if (i % 2 != 0) {
oddTotal += i;
}
}
return oddTotal;
}

/**
Expand All @@ -26,7 +43,23 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
if (words == null) {
throw new NullPointerException("Words is null");
}
if (words.isEmpty()) {
throw new IllegalArgumentException("Words is empty");
}

String shortest = words.iterator().next();
for (String i : words) {
if (i.length() < shortest.length()) {
shortest = i;
}
else if (i.length() == shortest.length() && i.compareTo(shortest) < 0) {
shortest = i;
}
}
return shortest;
}

/**
Expand All @@ -39,7 +72,18 @@ public static String shortestWord(Set<String> words) {
* @throws NullPointerException if ages is null
*/
public static Set<String> adults(Map<String, Integer> ages) {
return null;
if (ages == null) {
throw new NullPointerException("no age");
}

Set<String> result = new HashSet<>();
for (Map.Entry<String, Integer> entry : ages.entrySet()) {
Integer age = entry.getValue();
if (age != null && age >= 18) {
result.add(entry.getKey());
}
}
return result;
}

/**
Expand All @@ -50,6 +94,19 @@ public static Set<String> adults(Map<String, Integer> ages) {
* @throws IllegalArgumentException if head is null
*/
public static int biggestNumber(ListNode<Integer> head) {
if (head == null) {
throw new IllegalArgumentException("Head is null");
}

int max = head.data;
ListNode<Integer> curr = head.next;

while (curr != null) {
if (curr.data > max) {
max = curr.data;
}
curr = curr.next;
}
return 0;
}

Expand All @@ -67,7 +124,16 @@ public static int biggestNumber(ListNode<Integer> head) {
* @return a frequency map of values in the list
*/
public static <T> Map<T, Integer> frequencies(ListNode<T> head) {
return null;
Map<T, Integer> freq = new HashMap<>();
ListNode<T> curr = head;

while (curr != null) {
T val = curr.data;
freq.put(val, freq.getOrDefault(val, 0) + 1);
curr = curr.next;
}

return freq;
}


Expand All @@ -80,7 +146,11 @@ public static <T> Map<T, Integer> frequencies(ListNode<T> head) {
* @return the number of levels in the tree
*/
public static int levelCount(BinaryTreeNode<?> root) {
if (root == null) {
return 0;
}

return 1 + Math.max(levelCount(root.left), levelCount(root.right));
}


Expand Down Expand Up @@ -108,7 +178,13 @@ public static int levelCount(BinaryTreeNode<?> root) {
* @return the sum of the nodes at the given level
*/
public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) {
return 0;
if (root == null || level < 1) {
return 0;
}
if (level == 1) {
return root.data;
}
return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1);
}


Expand All @@ -123,9 +199,27 @@ public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) {
* @return true if the sums are equal, false otherwise
*/
public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> head) {
return false;
return treeSum(root) == listSum(head);
}

private static int treeSum(BinaryTreeNode<Integer> node) {
if (node == null) return 0;

int v = node.data;

return v + treeSum(node.left) + treeSum(node.right);
}

private static int listSum(ListNode<Integer> node) {
int total = 0;
ListNode<Integer> curr = node;
while (curr != null) {
total += curr.data; // use curr.data if that's your field
curr = curr.next;
}
return total;
}

/**
* Returns the sum of all the vertices in a graph that are reachable from a given
* starting vertex.
Expand All @@ -136,7 +230,27 @@ public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> h
* @return the sum of all the vertices
*/
public static int graphSum(Vertex<Integer> start) {
return 0;
if (start == null) {
return 0;
}

int sum = 0;
Set<Vertex<Integer>> seen = new HashSet<>();
Queue<Vertex<Integer>> queue = new LinkedList<>();
queue.add(start);
seen.add(start);

while (!queue.isEmpty()) {
Vertex<Integer> v = queue.poll();
sum += v.data;

for (Vertex<Integer> n : v.neighbors) {
if (n != null && seen.add(n)) {
queue.add(n);
}
}
}
return sum;
}

/**
Expand All @@ -148,6 +262,28 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
if (start == null) return 0;

int sinks = 0;
Set<Vertex<Integer>> seen = new HashSet<>();
Queue<Vertex<Integer>> q = new LinkedList<>();
q.add(start);
seen.add(start);

while (!q.isEmpty()) {
Vertex<Integer> v = q.poll();
List<Vertex<Integer>> neighbor = v.neighbors;

if (neighbor == null || neighbor.isEmpty()) {
sinks++;
} else {
for (Vertex<Integer> n : neighbor) {
if (n != null && seen.add(n)) {
q.add(n);
}
}
}
}
return sinks;
}
}