Tags

, , , , ,

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:

Solution code:

 

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:

Solution code:

 

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:

Solution code:

 

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:

Solution code:

 

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:

Solution code: