Monday, January 28, 2013

How to count possible combination for coin problem


How to count possible combination for coin problem



/*
          Please implement this method to
          return the number of different combinations of US coins
          (penny: 1c, nickel: 5c, dime: 10c, quarter: 25c, half-dollar: 50c)
          which may be used to produce a given amount of money.
          For example, 11 cents can be produced with
          one 10-cent coin and one 1-cent coin,
          two 5-cent coins and one 1-cent coin,
          one 5-cent coin and six 1-cent coins,
          or eleven 1-cent coins.
          So there are four unique ways to produce 11 cents.
          Assume that the cents parameter is always positive.
         */

The same problem for coins(1,5,10,25,50) has one of below solutions. The solution should satisfy below equation: 1*a + 5*b + 10*c + 25*d + 50*e == cents
public static void countWaysToProduceGivenAmountOfMoney(int cents) {
    for(int a = 0;a<=cents;a++){
        for(int b = 0;b<=cents/5;b++){
            for(int c = 0;c<=cents/10;c++){
                for(int d = 0;d<=cents/25;d++){
                    for(int e = 0;e<=cents/50;e++){
                        if(1*a + 5*b + 10*c + 25*d + 50*e == cents){
                            System.out.println("1 cents :"+a+", 5 cents:"+b+", 10 cents:"+c);
                        }
                    }
                }
            }
        }
    }
}

Sunday, January 27, 2013

Steps to post JMS message to Queue

Steps to post a message to JMS Queue:-

1.  Create JNDI context
       Hashtable ht = new Hashtable();
  ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  ht.put(Context.PROVIDER_URL,"t3://localhost:7001");

        Context jndiContext = new InitialContext(ht);

2.  Create the QueueConnectionFactory
           QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)      
           jndiContext.lookup(connFactoryJNDIName);

3. Create the Queue
          Queue queue = (Queue) jndiContext.lookup(queueName);

4. Create the QueueConnection
           QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();

5. Create the QueueSession
           QueueSession .queueSession = queueConnection.createQueueSession(false, 
           Session.AUTO_ACKNOWLEDGE); 

6. Create the QueueSender
           QueueSender queueSender = queueSession.createSender(queue);

7. Create the Message(Text,Byte,Object,Stream)
          TextMessage message = queueSession.createTextMessage();

8. Set Message
          message.setText("str_message");

9. Send Message
          queueSender.send(message);

Thursday, January 24, 2013

Algorithm Questions

1. How to reverse a single linked list?
    Solution:- Try a program to reverse the node references (instead of current to next change it to next to    
    current) using a temp variable.

2. How to reverse a String?
    Solution:-
   public String reverse(String str) {
    char[] chars = str.toCharArray();
    int n = chars.length;
    for (int i = 0; i < n/2; i++) {
        char tmp = chars[i];
        chars[i] = chars[n-i-1];
        chars[n-i-1] = tmp;
    }
    return new String(chars);
}
3. Find out number of bits needed to represent a positive integer in binary?
      Solution:-
       Just count how many times you shift right before you're left with just zero:
int value = 11;
int count = 0;
while (value > 0) {
    count++;
    value = value >> 1;
}

4. Java program to get the max number in an array?
Solution:
 max = 0;
            
            for (int i = 0; i < array.length; i++) 
            {
                if(max < array[i])
                {
                   max = array[i]; 
                }
            }
5. If you have access to a function that returns a random integer from one to five, write another function which returns a random integer from one to seven.
  Random r1 = new Random();
  Random r2= new Random();
  int m=0;
  while(m<=7){
   int k = r1.nextInt(5);
   int l = (r2.nextInt(5)/2);
   int n = (r2.nextInt(5)/3);
   m = l+k+n;
   System.out.println(m);
   if(m==7){
    break;
   }
  }
6. 

What order is a hash table lookup? 

--Hashtables are sorted by some hash function, not by their natural sort order, so you'd have to extract all the entries O(N) and sort them O(NlogN) 


7. What order is determining if a number is even or odd? 1

8. What order is finding an item in an unsorted list? n square

9. What order is a binary search? log n (because n/2)

10. 
Given an array of size n, containing every element from 1 to n+1, except one. Find the missing element.


    Solution :-   (Sum of all the elements from 1 to n+1)  - (Sum of all the elements in the array)


11. Sum of elements from 1 to n is n*(n+1)/2


12. Factorial:-

protected static long fact (int n)
{
if (n <= 1)
return 1;
return n * fact (n - 1);
} // method fact

14. Queue using stack:-
2 Stacks:-

Keep 2 stacks, let's call them inbox and outbox.
Queue:
- Push the new element onto inbox
Dequeue:
- If outbox is empty, refill it by popping each element from inbox and pushing it onto outbox
- Pop and return the top element from outbox
Using this method, each element will be in each stack exactly once - meaning each element will be pushed twice and popped twice, giving amortized constant time operations.
Here's an implementation in Java:
public class Queue<E>
{

    private Stack<E> inbox = new Stack<E>();
    private Stack<E> outbox = new Stack<E>();

    public void queue(E item) {
        inbox.push(item);
    }

    public E dequeue() {
        if (outbox.isEmpty()) {
            while (!inbox.isEmpty()) {
               outbox.push(inbox.pop());
            }
        }
        return outbox.pop();
    }

}

1 Stack:-

public class SimulatedQueue<E> {
    private java.util.Stack<E> stack = new java.util.Stack<E>();

    public void insert(E elem) {
        if (!stack.empty()) {
            E topElem = stack.pop();
            insert(elem);
            stack.push(topElem);
        }
        else
            stack.push(elem);
    }

    public E remove() {
        return stack.pop();
    }
}
13. http://www.careercup.com/question?id=15421717