Kangaroo

There are two kangaroos on a number line ready to jump in the positive direction. Each kangaroo takes the same amount of time to make a jump, regardless of distance. That is, if kangaroo one jumps 3 meters and kangaroo two jumps 5 meters, they each do it in one second, for example. Given the starting locations and jump distances for each kangaroo, determine if and when they will land at the same location at the same time. [Read More]

Apple and Orange

Sam’s house has an apple tree and an orange tree that yield an abundance of fruit. Sam’s two children, Larry and Rob, decide to play a game in which they each climb a tree and throw fruit at their (Sam’s) house. Each fruit that lands on the house scores one point for the one who threw it. Larry climbs the tree on the left (the apple), and Rob climbs the one on the right (the orange). [Read More]

Matrix Diagonal Difference

Input Given a square matrix of size N x N, calculate the absolute difference between the sums of its diagonals. Output Print the absolute difference between the two sums of the matrix’s diagonals as a single integer. Sample Input $$ matrix = \begin{bmatrix} 11 & 2 & 4 \newline 4 & 5 & 6 \newline 10 & 8 &-12 \end{bmatrix} $$ Sample Output 15 Explanation The primary diagonal is: $$ \begin{bmatrix} 11 \newline 5 \newline -12 \end{bmatrix} $$ [Read More]

Get Month Name

Map the given integer to a month. Explanation For month = 1, the output should be getMonthName(month) = "Jan" For month = 0, the output should be getMonthName(month) = "invalid month" Solution import java.util.Locale; import java.text.DateFormatSymbols; public class MonthNamer { private final static INVALID_MONTH_LABEL = "invalid month"; private String getMonthName(Integer month){ if(month < 1 || month > 12){ return INVALID_MONTH_LABEL; } return new DateFormatSymbols(Locale.ENGLISH).getShortMonths()[month-1]; } public static void main(String[] args){ Integer month = 1; String result = new MonthNamer(). [Read More]

Is Pangram

Given a sentence, check whether it is a pangram or not. Explanation For sentence = “The quick brown fox jumps over the lazy dog.”, the output should be isPangram(sentence) = true. For sentence = “Hello World!”, the output should be isPangram(sentence) = false. Solution public class PangramVerifier { private static final int ASCII_ALPHABET_SUM = 2015; private Boolean isPangram(String quote){ Integer sum = quote.toUpperCase().chars().filter(x -> x > 64 & x < 91). [Read More]

Stream Merger

Let’s consider the following class: import java.util.Set; import java.util.HashSet; class InputStream { int getStreamId(){ return 0; } } class OutputStream { void emitValue(int value){} } public class StreamMerger { private Set<InputStream> streams; public StreamMerger(Set<InputStream> streams){ this.streams = streams; } public void mergeInto(OutputStream stream){ } public static void main(String[] args){ new StreamMerger(new HashSet<InputStream>()); } } Our goal is to get a collection of streams order it by stream id and then merge those elements into an output stream. [Read More]

Binary Tree

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child. In this example we will create a rooted binary tree witch has a root node and every node has at most two children, if the node has only one child it is a leaf. interface TreeNode { Boolean isRoot() Boolean isLeaf() List<TreeNode> getChildren() void setParent(TreeNode node) void addChild(TreeNode node) } The inteface helps to define our methods in terms of functionality description [Read More]

Fastman

Write a program that breaks up a string of words with no spaces into a string with appropiate spaces. Consider an example that’s rich enough but no tedious: “fastman” –> “fast man” Consider an empty string or null The words come from custom dictionary Solution class Fastman { def dictionary = ["fast","fat","man","strong"] private String find(String string){ String result = '' if(string?.length() > 0){ for(int i=0;i<string.length()-1;i++){ for(int j=string.length();j!=i;j--){ if(dictionary.contains(string.substring(i,j))){ result += "${string. [Read More]

Algorithms

This section is about solving simple algorithms, coding challenges, puzzles, katas and dojo material in Java. Sum a Collection Biggest Number Most Popular in the array Common Elements in two Collections Number Counter Characters Counter Birthday Cake Candles String Compressor Sock Merchant Smallest Biggest Electronics Shop Square my List Digital Adder Sum a Collection Given an array of integers, find the sum of its elements. import java.util.List; public class CollectionAdder { public int sum(List<Integer> numbers) { return numbers. [Read More]

Algorithms

Simple Algorithms Fastman Problem Binary Tree Stream Merger Is Pangram Get Month Name Matrix Diagonal Difference Grading Students Apple and Orange Kangaroo GitHub Repository Binary Search Is a search algorithm that finds the position of a target value within a sorted array. BinarySearch.java public class BinarySearch{ int logarithmic(Integer[] data, int key) { int startIndex = 0; int endIndex = data.length - 1; while (startIndex < endIndex) { int midIndex = (endIndex - startIndex / 2) + startIndex; int midValue = data[midIndex]; if (key > midValue) { startIndex = midIndex++; } else if (key < midValue) { endIndex = midIndex - 1; } else { return midIndex; } } return -1; } public static void main(String[] args){ BinarySearch binarySearch = new BinarySearch(); Integer[] myData = {1,2,3,4,5,6,7,8,9,10}; System. [Read More]