Tutor profile: Harshil N.
How many ways to choose any r numbers from a set of n numbers? How do we derive it if we don't know the popular formula?
Consider the numbers from 1....n. Now consider r blanks. Lets start filling the r blanks from the n numbers. The first blank can be filled using any of the n numbers. The second with n-1 as the first blank is already filled and there are n-1 numbers remaining. The third with n-2 and so on. The last blank can be filled with n-(r-1) numbers. So the total ways could be counted as n*(n-1)*(n-2)....(n-r+1) = n!/(r)! . But we have not considered duplicates. For each combination of those blanks we have also counted their permutations as separate combinations. For instance (1,2,3) and (3,2,1) and (2,1,3) are all counted as separate combinations. So we divide each combination we have got by (n-r)!. As we can permute any combination by (n-r)! ways. So we can then divide the total by (n-r)!. Hence n!/(r!(n-r)!).
Subject: Java Programming
Whats a singleton object? And why do we need one?
A singleton object is one which on invoking the constructor of the corresponding class returns the same object on each invocation. To elaborate that, it means the class (lets name it C1 ) which we want as singleton, we return the same object of C1 across all the other classes which are dependent on C1. We would need one because of various reasons. One of them could be space considerations. Any application which uses plenty of classes and each class depends on other classes, we might end up constructing plenty of objects in the memory. Increasing the frequency of garbage collector operating and slow down the application. Also when we make singletons, we make them thread safe. This is to avoid any state changes to the object which is being used by a thread (t1) by another thread(t2).
Subject: Computer Science (General)
What data structure would you use to organise incoming requests to a web server with each request having a priority being assigned to it?
The answer is a priority queue. We use a priority queue using heap implementation where the top most element of the heap is the request which has the highest priority at a given time T. As each request arrives on the server we store it to the heap, the property of the heap being that each element has two nodes both of which have a priority less than the priority of itself. When we insert to the heap, we need to maintain the original property of the heap, which takes log n time (where n is the size of the heap). And when we want to serve a request we just access the top most element which is constant time operation i.e O(1).
needs and Harshil will reply soon.