You are on page 1of 5

Kurukshetra '07  On-Site Programming Contest Prelims Round Solution Set

Q: Write the C compare function for qsort which will sort based on 2 elds: A:

int compare(void *a, void *b) { values *x = (values *)a; values *y = (values *)b; if (x->value1 != y->value1) return x->value1 - y->value1; else return x->value2 - y->value2; }
all o initially. First, I go and ip the switch in all rooms. Then I ip the switch in rooms 2,4,6.... Then I ip it in rooms 3,6,9, .... This continues for 10000 times. Which rooms will have the light switch turned on?

Q: There are 10000 rooms with a light bulb (with its switch of course) each. They are

A: All rooms whose number is a perfect square will have the lights turned on. Why? The basic idea is that two ips of the switch will bring it back to the original state. All the square numbers will have an odd number of factors. For instance 4 has only 1 factor (other than 1 and itself )  2. 9  3, 16  2, 4, 8. Get the idea? Q: How many dierent binary trees are possible with 10 nodes?

n A: 1014. There are always 2 - n for any binary tree with n nodes. For instance, let
us take the case of a binary tree with 3 nodes:

Q: Given a triple pointer `int ***a', write the C code to initialize the memory (assume each dimension of 10). A:

#define MAX 10 int ***allocate() { int ***a,i,j;

a=(int ***) malloc(MAX * sizeof(int ***)); for(i=0;i<MAX;i++) { a[i]=(int **)malloc(MAX * sizeof(int *)); for(j=0;j<MAX;j++) a[i][j]=(int *)malloc(MAX * sizeof(int)); } return a;

Q: How many positive integers `n' satisfy the constraint:

n/99 = n/101
A: Lets split the set of positive integers into discrete sets. All numbers from 1-98 follow the above said constraint. That makes 98 numbers. Then from 101-197 also follow the constraint. That makes another 97 numbers. The next set is from 202-296 which makes abother 95 numbers. So the total set of positive integers which follow this constraint is = 98 + 97 + 95 + . . . + 1 = 99 + 97 + 95 + . . . + 1 -1 2 = 50 -1 Q: There is a 100 storey building and you have 2 eggs with you. The egg breaks when you drop it down from any oor greater than or equal to n. It doesn't break when you drop it from oors less than n. Example: You can apply the brute force method by dropping it at 1, 2, 3. . . etc. But the worst case is 100 if the egg breaks only at 100. Make sure you use only two eggs. Find `n' in the minimum number of steps and how you arrived at the answer. A: The worst case is just 14 tries. Break the 1st egg at oor 14. If it breaks do a

brute force with the second egg from 1 to 13. If the 1st egg didnt break, then try at 14+13=27. If it breaks, do a brute force from 15 to 26. The tries for the rst egg are 14, 14+13, 14+13+12 etc making the worst case as 14. How to get the magic number 14? n*(n+1)/2 should be > 100 and 14 is the rst number which satises. Q: Given a string (or a character array) write a function that reverses the words in the string. For e.g. Kurukshetra takes place in Jan becomes Jan in place takes Kurukshetra A: 1. Reverse whole string 2. Reverse Individual words in the string Q: Given an array of integers (positive and negative) write a function which will nd a pair of numbers in the array whose dierence in the least. A: 1. Sort the array of numbers (O(n lg n)) 2. Compare adjacent numbers in the array to nd minimum dierence 2

3. Save the 2 numbers whose dierence is minimum Q: Given a number of points on a top row and a number of points on a bottom row. Find the number of intersections. (Question not complete for the sake of brevity) A: P(a,b) = a(a-1)*b(b-1)/4 Take 1 point at a time from the top row and connect it to all the points on the bottom row. Now when you draw lines from the rst point on the top row to the bottom row points, there will be no intersection. When you draw lines from the second point on the top row to the rst point of the bottom row it will cause (b-1) intersections. Connecting to the second point on the bottom row causes (b-2) intersections and so on until 0. So now total numbver of intersections caused by connecting the second point on the top row to all the points on the bottom row = (b-1) + (b-2) + . . . = b*(b-1)/2 This will continue (a-1) times. Causing arithmetic progression in both. i.e. Total number of intersections would be: = b*(b-1)/2 + 2*b(b-1)/2 + . . . (a-1)*b(b-1)/2 = a(a-1)*b(b-1)/4 Q: I have a circle. Given 1 straight line I can cut it into 2 halves. Now, If I have 2 lines, I can cut the circle into 4 parts. Given `n' straight lines, how many parts can you cut the circle? (maximum). A: The number of parts on adding a cut goes like this: 2 + 2 + 3 + 4 + ...+ n = 1 + 1 + 2 + 3 + 4 + ...+ n = 1 + n(n+1)/2 Q: Two players A and B alternate turns during a game as follows: Player A starts by calling a whole number between 1 and 10. Each turn a player calls a whole number larger than the previous by at most 10. The player who calls 100 wins. For example, a game can start as A calls 3, B calls 12, A calls 22, B calls 24, A calls 25, etc. Give a winning strategy for player A. A: Make sure that A calls numbers in the following order no matter what B calls outs: 1, 12, 23, 34, 45, 56, 67, 78, 89, 100 Q: Number of reections in a reecting sphere in order for the light ray to get out of the sphere (Question not complete for the sake of brevity) A:

function sphere(incident angle i): for n = 1 to 70: if (n*i)%360 == 0: return `Yes' return `No'
use the coin to write an algo that will return 1 with prob 1/2

Q: Given a coin whose probability of showing the face with `1' is P i.e. Prob(1) = P,

A: Assume that X is a function that represents 1 toss of the coin. 3

function newX() { p = X(); q = X(); return p xor q ? p : newX(); }


2 Prob(1) = pq/2 + (1-pq).pq/2+ (1-pq) .pq/2........ ( Binomial distribution : independent events). Q: Count to 10 in base

negative

2!

A: 1 = 1, 2 = 110, 3 = 111, 4 = 100, 5 = 101, 6 = 11010, 7 = 11011, 8 = 11000, 9 = 11001, 10 = 11110

0 1 Digit in units place of a number of base -2 is -2 . Digit in tens place is -2 . Digit 2 in hundreds place is -2 . And so on... 2 1 0 Example: 310 = (1x(-2) + 1x(-2) + 1x(-2) )2
Q: Four people need to cross a rickety rope bridge to get back to their camp at night. Unfortunately, they only have one ashlight and it only has enough light left for seventeen minutes. The bridge is too dangerous to cross without a ashlight, and it's only strong enough to support two people at any given time. Each of the campers walks at a dierent speed. One can cross the bridge in 1 minute, another in 2 minutes, the third in 5 minutes, and the slow poke takes 10 minutes to cross. How do the campers make it across in 17 minutes? A: 1. A & B cross. total time: 2 minutes. 2. B comes back. total time: 4 minutes. 3. C & D cross. total time: 14 minutes. 4. A comes back. total time: 15 minutes. 5. A & B cross. total time: 17 minutes. Another valid solution is to have A bring the ashlight back in step 2. Q: How many squares are there on a chessboard?? (the answer is not 64) Size 1x1 2x2 3x3 A: 4x4 5x5 6x6 7x7 8x8 Horizontal positions 8 7 6 5 4 3 2 1 Vertical positions 8 7 6 5 4 3 2 1 Total Positons 64 49 36 25 16 9 4 1 204

Q: A line of 100 airline passengers is waiting to board a plane. they each hold a ticket to one of the 100 seats on that ight. (for convenience, let's say that the nth passenger in line has a ticket for the seat number n.) 4

Unfortunately, the rst person in line is crazy, and will ignore the seat number on their ticket, picking a random seat to occupy. all of the other passengers are quite normal, and will go to their proper seat unless it is already occupied. if it is occupied, they will then nd a free seat to sit in, at random. What is the probability that the last (100th) person to board the plane will sit in their proper seat (#100)? A: 1/2. Let us take the case of 3 passengers. Now, there are 3! means 6 possibilities (seating arrangements): 123, 132, 213, 231, 312, 321. The rst guy is SO crazy he COULD randomly pick his own seat. So the only ones we can eliminate are 231 and 132, because the second guy would sit in his seat if avaliable. This leaves 123, 213, 312, 321. Passenger 3 gets his own seat 1/2 the time. At each point, the chances of randomly choosing seat 1 is the same as randomly choosing seat n, so the chances will always be 1/2, no matter how many seats/passengers we're talking about. Q: What does this Perl Regex match? (simple isn't it?) qr/[-a-z0-9]+(?:\.[-a-z0-9]+)*\.(?:\w3)/i; A: It matches domain names, possibly with subdomains Q:

/* You are not expected to understand this */


What version of the Unix kernel did this comment originate?

A: It's originally from the 6th Edition, and was quoted in the Lions book Q: After what movie was Bell Labs's successor for UNIX named? A: Plan 9 From Outer Space By Ed Woods

You might also like