Generate english phrase that describes a integer

Problem Given an integer between 0 and 999,999, print an English phrase that describes the integer (e.g., “One Thousand, Two Hundred and Thirty Four”). Solution private static final Map<Integer, String> digitsToStrings; static { Map<Integer, String> aMap = new HashMap<Integer, String>(); aMap.put(1, "One"); aMap.put(2, "Two"); aMap.put(3, "Three"); aMap.put(4, "Four"); aMap.put(5, "Five"); aMap.put(6, "Six"); aMap.put(7, "Seven"); aMap.put(8, "Eight"); aMap.put(9, "Nine"); aMap.put(10, "Ten"); aMap.put(11, "Eleven"); aMap.put(12, "Twelve"); aMap.put(13, "Thirteen"); aMap.put(14, "Fourteen"); aMap. [Read More]

Game of Master Mind

Problem The Game of Master Mind is played as follows: The computer has four slots containing balls that are red (R), yellow (Y), green (G) or blue (B). For example, the computer might have RGGB (e.g., Slot #1 is red, Slots #2 and #3 are green, Slot #4 is blue). You, the user, are trying to guess the solution You might, for example, guess YRGB. When you guess the correct color for the correct slot, you get a “hit” If you guess a color that exists but is in the wrong slot, you get a “pseudo-hit”. [Read More]

Find the maximum (or minimum) of two numbers (or morenumbers) without use of comparison operators or if-else

Problem Write a method which finds the maximum of two numbers You should not use if-else or any other comparison operator EXAMPLE Input: 5, 10 Output: 10 Solution Method 1 - Using arithematic operator Let a is greater than b, and M is the mid point, then: then max = (a + b) / 2 + |a - b| / 2 Method 2 - Using most significant bit [Read More]

Number of trailing zeros in a factorial

Problem Write an algorithm which computes the number of trailing zeros in n factorial. Solution The trailing zeros are constructed by multiplying pairs of 2 and 5. So to count the number of trailing zeros, we need to count the number of pairs of 2 and 5. For a factorial, there are way more number of factor 2 than 5 so we just need to count the number of factor 5 in the factorial. [Read More]

Check if someone has won tic-tac-toe

Problem Design an algorithm to figure out if someone has won in a game of tic-tac-toe. Solution I’ve written a game of tic-tac-toe in Java, and my current method of determining the end of the game accounts for the following possible scenarios for the game being over: The board is full, and no winner has yet been declared: Game is a draw. Cross has won. Circle has won. [Read More]

Given the post order array find if tree is BST

Problem An array which is a Post order traversal of a Binary Tree. Write a function to check if the Binary Tree formed from the array is a Binary Search Tree. Eg: 2 1 3 The array given as input would be 1 3 2. Write a function to say if the tree formed is a Binary Search Tree. Example 2: 4 is root. 0 is left child of 1 , 1 is left child of 2 and 2 is left child of 4. [Read More]

Road Race

Problem

In the final stretch of a road race, you pass the 2nd-place runner right before crossing the finish line. What place do you finish in?Pu

Solution

Second place. You would have had to pass the first place racer to have finished in first place.

Sum of Hats

Problem There are 3 people Abel,Bill and Clark.Three of them have numbers written on their hats.One can only see the numbers written on others hats and can not see the number written on his own hat. Sum of numbers on any two 2 hats is equal to the number on the third hat.Now the following event occurs 1. Abel was asked about the number on his hat.He replied “Don’t Know” [Read More]

Coins on the Table

Variant 1 - Suppose we have 10 coins with Heads f… AmITh - May 2, 2014Variant 1 - Suppose we have 10 coins with Heads faced up and 10 more with Tails. Now we randomly divide the total(20) coins into two pipes of 10 coins each. pipe1 pipe2 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 since there are 10 heads(1) and 10 tails(0) , if there are 4 Heads in pipe1, then remaining 6 should be in pipe2. [Read More]