A Few Question in Computer Science
A Few Question in Computer Science
You most probably know of its solution because you saw it or read it. But if you have not, it is worth trying it out here. The problem asks you to detect whether a linked list contains a loop. If you are aware of Hare and Tortoise solution, can you try something new (At least it should work for most cases)? (Source : Probably Stanford Final Exam 1970) 2) This problem asks you to determine whether an array contains a majority element. An element qualifies to be a majority element if it occurs more than half the times. Do it by both a recursive and a (possibly) simpler iterative algorithm. (Source : Sara Basse) 3) First consider an easier one. There is an array which contains 2n+1 numbers all of which but for one occur with duplicates. Find the non-duplicated element. Now its time for power-play. Consider an array with 2n+2 elements. Only 2 elements in this array have no duplicates. Others do. Find them both. (Source : Adobe Interview) As a bonus, try this problem when you have an array that contains 2n+k elements. K elements in this array have no duplicates. Others do. Find these k non-duplicated elements. 4) Consider a natural number n. The number is partitioned into m >= 0 parts say a1, a2.am such that a1+a2+a3+..am = n. Can you find the maximum lcm the partitions of this number n can attain? (Source : IIT Kanpur TechKriti) 5) You are being chased by a dog. You run hard, you run fast. You come at the foot of a staircase and without thinking you start climbing the stairs. You jump over 2 stairs in a single stride, at times though you climb 1 and at still others you climb 3. After you reach the door, you knock maniacally, wrench the door open and get inside. With your head turned to the wall you start panting thinking all the while Phew that was close. Suddenly you ask yourself in how many ways could I have climbed these steps if the only strides that I took were to be a maximum of 3 steps at a time. The number of steps you climb are 15. (Source : Original with me) 6) Write a program to generate triple-less sequences of arbitrary lengths over the binary alphabet. A triple-less sequence is one which does not contain any substring made of 3 identical bits. (Source : Ian Stewarts Math Hysteria. Also you can try googling for Thues sequence) 7) Write a function to find the subsequence with the maximum sum in an array containing both positive and negative integers. This problem is called the maximum sum subsequence problem. [Hint: Use dynamic programming techniques].
(Source : Jon Bentley. Programming Pearls). Can you also give an O(n.logn) Solution for this (Though that would be not as good as the DP solution, but still its worth a shot) 8) Write a program to find the square root of a natural number. (Source : I dont have to mention ) 9) Given a tree (binary tree of course) as an input, write a function that calculates its height and write another which counts its number of nodes. (Source : Dont ask which interview! There are too many which ask this) 10) HmmTime for a game theory problem. Write a program which determines the winner of the game depicted below for any natural number n. There are 2 players A and B, each extremely logical. They are playing a game in which they take turns drawing a ball labeled with a particular number from a bag. If they removed a ball labeled X, they have to remove all balls bearing labels Y where Y divides X. In case Y is already removed, they skip it. The player faced with the ignominy of being in a position where he cannot remove anything loses the game. Find all the values of n for which 2nd player has a sure win. [Source : IIIT Allahabads Programming contest 2007] 11) Given a graph showing a map of, lets say, India. The nodes of graph are the cities of the country and its edges represent cities that are connected via roads. The weights the edges carry represent the distance between two cities. How will you go about finding two cities which are the greatest distance apart? (Source : Papadimitriou in What is dynamic Programming lectures.) 12) Consider the following puzzle. It is called the orbs puzzle and if you like to know more about it, do a Google search. The puzzle asks you to determine the height at which an orb that plummets down will start breaking. The orbs (2 in number) have to be used judiciously. You cannot afford to have them both broken without having discovered the threshold storey-number at which they start breaking. Suppose the building is 100-storeyed. As a next step try to do the problem if you have k-orbs and the building is nstoreyed. Again you have to find the most efficient solution (in terms of number of orb-drops). [Source : Solidcore interview, IIT Roorkee 2007]
Hints 1) Try reversing the list. At the end the head nodeShould work for most cases. 2) Lets throw in some definitions. Define a candidate element to be the element which occurs most of the times in your scanned portion of the array. When the function returns, count the occurrence of candidate element in the array. If its more than half we are home. But this leaves an important issue dangling how do we select a candidate element? And that is the question you have to answer 3) (i) XOR all. (ii) You need two passes. 4) Use dynamic Programming. 5) Tribonacci Sequence T(n) = T(n-1) + T(n-2) + T(n-3)..As a base condition you can find T(0), T(1) and T(2) 6) Needs more space. Will write later 7) See Programming Pearls Chapter 7. I have not seen a better explanation. Contains both the solutions. 10) 2nd Player never wins! Consider the games where you believe 1st player loses. Have him pick ball labeled 1. Since you believe he has to lose in this case, this surely means 2nd player has a response ready for this choice of A. Lets say he picks ball labeled N and those that divide N. He cannot remove 1 as it already removed and the turn passes back to A. But anyway all the divisors of N including N are now gone. Thus after B has given A his turn back we see a N-totally-removed situation. Now A comes up with the best move in this situation to delay his loss. B replies to it and wins. Fine! But what if A had picked N? Surely he would have created a N-totally-removed situation right then. It means our assumption was wrong. So, A wins. Note that it may be necessary for A to pick 1 in the first turn itself but it does not affect the games outcome anyhow. 11) Make all edges negative and use all pairs shortest path! 12) Set up a recursion. More on this later.