This is a list of coding exercises, working up in difficulty. These exercises are provided to learn and for practice, do what works for you – start with the problem & work to the solution, start with the solution & work backwards, or anything in-between.
Please note, all the problems are done in Java, feel free to convert to any language you wish.
The “challenge code” is the basic skeleton code. It is given to provide the basic structure, as well as any initial information you may need to start the exercise – e.g. so you don’t have to worry about taking in input.
The “solution code” is the final result. Don’t worry if your code doesn’t look exactly like the solution code (although it should be similar). We provide it only as a reference. The whole goal is for you to tackle the problem and fit the solution within the given constraints.
Hint: If you understand the problem but don’t know how to fit it within the constraint, write the problem as you understand it. Once you have a working solution, then try to optimize it.
FizzBuzz:
Loop through numbers, 1 – 100.
If the number is divisible by 3, print “Fizz”.
If the number is divisible by 5, print “Buzz”.
If the number is divisible by 3 & 5, print “FizzBuzz”.
If the number does not match any of the above cases, print the number.
Example: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz
The constraint - There can only be 3 conditional statements.
Challenge code:
|
|
package com.sudounlv; public class FizzBuzz { public static void main(String[] args) { int eger = 100; for(int i = 1; i <= eger; i++) { // Solution here } } } |
Solution code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
package com.sudounlv; package com.sudounlv.code; public class FizzBuzz { public static void main(String[] args) { int eger = 30; for (int i = 1; i <= eger; i++) { if (i % 3 == 0) { System.out.print("Fizz"); } if (i % 5 == 0) { System.out.print("Buzz"); } if (i % 3 != 0 && i % 5 != 0) { //or if(i % 15 != 0) System.out.print(i); } System.out.print(", "); } } } |
Palindrome:
Determine if a given string is a palindrome.
The constraint – Using only one memory reference – do not duplicate the string. Do in O(n) time.
Challenge code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package com.sudounlv; import java.util.Arrays; import java.util.List; public class Palindrome { public static void main(String[] args) { List imAListOfStrings = Arrays.asList("notAPalindrome", "racecar", "101101", "tacocat"); String isPalindrome = " is a Palindrome"; String notPalindrome = " is not a Palindrome"; for(String string : imAListOfStrings) { Boolean isPal = isPalindrome(string); if(isPal) { System.out.println(string + isPalindrome); } else { System.out.println(string + notPalindrome); } } } private static Boolean isPalindrome(String string) { Boolean isPal = null; //Solution here return isPal; } } |
Solution code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
package com.sudounlv.code; import java.util.Arrays; import java.util.List; public class Palindrome { public static void main(String[] args) { List imAListOfStrings = Arrays.asList("notAPalindrome", "racecar", "101101", "tacocat"); String isPalindrome = " is a Palindrome"; String notPalindrome = " is not a Palindrome"; for(String string : imAListOfStrings) { Boolean isPal = isPalindrome(string); if(isPal) { System.out.println(string + isPalindrome); } else { System.out.println(string + notPalindrome); } } } private static Boolean isPalindrome(String string) { Boolean isPal = true; int length = string.length(); int index = 0; while ((index < length / 2) && isPal) { if(string.charAt(index) != string.charAt(length - (index + 1))) { isPal = false; } index++; } return isPal; } } |
Bouncy Numbers:
Loop through the list of given numbers – determine if the number is “Increasing”, “Decreasing, “Bouncy”, or “Flat”.
An increasing number is a number where each individual number, from left to right, is increasing. Such as, 12345.
A decreasing number is a number where each individual number, from left to right, is decreasing. Such as, 54321.
A bouncy number is a number that both decreases and increases. Such as, 51900.
A flat number is a number that remains the same, the entire way through. Such as 11111.
The constraint - Do in O(n) time. (Loop once through a number.)
Challenge code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
package com.sudounlv; import java.util.Arrays; import java.util.List; public class BNumbers { public static void main(String[] args) { List integerList = Arrays.asList(123456789, 987654321, 123454321, 222222222, 234123653); for (Integer number : integerList) { String s = determineIntegerType(number); System.out.println(s); } } private static String determineIntegerType(Integer number) { String rval = ""; // Solution here return rval; } } |
Solution code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
package com.sudounlv.code; import java.util.Arrays; import java.util.List; public class BNumbers { public static void main(String[] args) { List integerList = Arrays.asList(123456789, 987654321, 123454321, 222222222, 234123653); for (Integer number : integerList) { String s = determineIntegerType(number); System.out.println(s); } } private static String determineIntegerType(Integer number) { boolean isIncreasing = true; boolean isDecreasing = true; boolean isFlat = false; String buffer = Integer.toString(number); char previous = buffer.charAt(0); String rval = null; for (char c : buffer.substring(1, buffer.length()).toCharArray()) { if (isDecreasing && c > previous) { isDecreasing = false; } if (isIncreasing && c < previous) { isIncreasing = false; } previous = c; } if (!isDecreasing && !isIncreasing) { rval = "BOUNCY"; } if (isDecreasing && isIncreasing) { rval = "FLAT"; isFlat = true; } if (!isFlat && isDecreasing) { rval = "DECREASING"; } if (!isFlat && isIncreasing) { rval = "INCREASING"; } return rval; } } |
Permutations:
Find every permutation – every possible arrangement of the characters – of a string.
Example: {123} => (123), (132), (213), (231), (312), (321)
The constraint - Using recursion.
Challenge code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
package com.sudounlv; import java.util.Arrays; import java.util.List; public class Permutations { public static void main(String[] args) { List strings = Arrays.asList("123", "asdf", "racer"); for (String string : strings) { permutation(string); } } //Feel free to delete these and use any method signature you want public static void permutation(String str) { permutation("", str); } private static void permutation(String prefix, String str) { //Solution here } } |
Solution code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package com.sudounlv.code; import java.util.Arrays; import java.util.List; public class Permutations { public static void main(String[] args) { List strings = Arrays.asList("123", "asdf", "racer"); for (String string : strings) { permutation(string); } } public static void permutation(String str) { permutation("", str); } private static void permutation(String prefix, String str) { int n = str.length(); if (n == 0) { System.out.println(prefix); } else { for (int i = 0; i < n; i++) { permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n)); } } } } |
Duplicate Numbers:
Loop through a list of numbers, numbered 1-n. One (and only one) of the numbers is duplicated in the list – find and print the duplicate.
Example: {3,23,18,19,11,22,7,9,8,13,5,0,17,23,20,15,16,6,1,2,4,14,12,10} => Duplicate number: 23
The constraint - Do in O(n) time. (Loop through the list once.)
Challenge code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
package com.sudounlv; import java.util.Random; public class DuplicateNumber { private static final int max = 25; private static final int[] list = new int[max + 2]; public static void main(String[] args) { Random random = new Random(System.currentTimeMillis()); //Pre-seed and print the list String listAsString = ""; for (int i = 0; i < list.length; i++) { list[i] = i; } // Set the last integer to something random list[list.length-1] = random.nextInt(max+1); // Let's scramble the list of values! for (int i = 0; i < list.length; i++) { final int pos = random.nextInt(list.length); int temp = list[pos]; list[pos] = list[i]; list[i] = temp; } // Print out scrambled list for (int i = 0; i < list.length-1; i++) { System.out.print(list[i]+","); } System.out.println(list.length-1); int duplicateValue = findDuplicateValueInArray(list); System.out.println("\tDuplicate number: " + duplicateValue); } private static int findDuplicateValueInArray(int[] list) { Integer duplicateNumber = null; // Solution here return duplicateNumber; } } |
Solution code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
package com.sudounlv.code; import java.util.Random; public class DuplicateNumber { private static final int max = 25; private static final int[] list = new int[max + 2]; public static void main(String[] args) { Random random = new Random(System.currentTimeMillis()); //Pre-seed and print the list String listAsString = ""; for (int i = 0; i < list.length; i++) { list[i] = i; } // Set the last integer to something random list[list.length-1] = random.nextInt(max+1); // Let's scramble the list of values! for (int i = 0; i < list.length; i++) { final int pos = random.nextInt(list.length); int temp = list[pos]; list[pos] = list[i]; list[i] = temp; } // Print out scrambled list for (int i = 0; i < list.length-1; i++) { System.out.print(list[i]+","); } System.out.println(list.length-1); int duplicateValue = findDuplicateValueInArray(list); System.out.println("\tDuplicate number: " + duplicateValue); } private static int findDuplicateValueInArray(int[] list) { // We get how many there are when divided in half float halfCount = ((float) list.length - 1) / 2; // We multiply that count by the max because either side of the list added up // equals the max. 0 + 10 = 10, 1 + 9 = 10, etc. float shouldBeTotal = max * halfCount; // Now we grab the sum of the list float total = 0; for (int i = 0; i < list.length; i++) { total += list[i]; } // Here's where we get the actual number that is duplicated int result = Math.round(total - shouldBeTotal); return result; } } |