Showing posts with label A question A day. Show all posts

gravatar

Blog # 86 : How to find Avg. Salary? Help them!



Five coworkers want to know what the average of all their salaries is, but refuse to reveal ANY information about their own salaries to their coworkers. How can they calculate the average ?


courtesy: Vibhaj

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 85 : Largest 100 from files


There is a big file containing 10^8 integers, one per line. Devise an algorithm to find the largest 100 integers among them. Remember you cannot read all of them into memory at once.


courtesy: Vibhaj

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 84 : Risky Bridge


A truck stops at a weigh station at the entrance to a bridge and is shown to weigh exactly 2000 lbs. The operator of the weigh station notes that the bridge can hold exactly 2000 lbs, but would crumble if it were subjected to even a fraction of an ounce more. But he says that no other vehicles ever cross over the bridge and so it's fine if the truck crosses. The truck continues on.
Once the truck is halfway across the 10-mile long bridge, a sparrow flies over to the truck and lands on the hood. The truck driver sees the bird lands and his heart drops as he realizes the bridge is about to collapse, but even after the bird lands, the bridge doesn't crumble.
How was the bridge able to hold up despite the extra weight from the bird ?




courtesy: Vibhaj

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 83 : Bitonic Array


Given an array of N distinct integers with the property that there exists an index K (0 <= K <= N-1) such that input[0], ..., input[K] is and increasing sequence and input[K], ..., input[N-1] is a decreasing sequence. Devise and algorithm to find K. Ex [1 2 4 7 10 15 13 11 5 0 -5] Answer: K=5 (input[K]=15) 


courtesy: Vibhaj

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 82 : Birthday Problem


What is the least number of people that need to be in a room such that there is greater than a 50 % chance that at least two of the people have the same birthday ?



Click here to see the solution.

Courtesy: Vibhaj Rajan
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 80 : Simple but Tricky Sum


What would be the output of the following code ?
int main(int argc, char **argv){
printf("%d", 012345 + 123450);
return 0;
}
courtesy: Mahesh Gondi


Click here to see the solution.

p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 79 : Strange Indian Coins


You have two Indian coins that add up to 6 rupees. However, one of them is not a 5 rupee coin.
What are the two coins ?


courtesy: Vibhaj Rajan


Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 77 : The Lonely Element


Given an array of integers with size 2N+1 such that N elements appear twice in arbitrary positions and 1 element appears only once.
Devise an algorithm to find the lonely element.

Ex : {1 2 4 5 1 4 2}
Answer : 5


courtesy: Syed Wali Hamza
Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 76 : Checking the range of an array


Write a function that takes an int array of size M, and returns (true/false) if the array consists of the numbers only within the range [N, N+M-1]. The array is not guaranteed to be sorted.
For instance, {2,3,4} would return true. {1,3,1} would return true, {1,2,4} would return false.


courtesy: Vibhaj
Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 75 : Catch the repetition



Given an array of size N which contains all the numbers from 1 to N-1. Find the number which is repeated in O(N) time.
How do you proceed with the same with floating numbers from 0 to 1 instead of 1 to N-1 ?


courtesy: Vibhaj
Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 74 : Truth is all about the question you ask!


You are walking down a path when you come to two doors. Opening one of the doors will lead you to a life of prosperity and happiness, while opening the other door will lead to a life of misery and sorrow. You don't know which door leads to which life. In front of the doors are two twin brothers who know which door leads where. One of the brothers always lies, and the other always tells the truth. You don't know which brother is the liar and which is the truth-teller. You are allowed to ask one single question to one of the brothers (not both) to figure out which door to open.
What question should you ask?

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 73 : The hourglass


You have two sand hourglasses, one that measures exactly 4 minutes and one that measures exactly 7 minutes. You need to measure out exactly 2 minutes.
Using only these two hourglasses, how can you measure out exactly 2 minutes ?

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 72 : No mathematical operator


Write a function to add two integers without using mathematical operators (+, -, *, /).

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

Courtesy: Vibhaj

gravatar

Blog # 71 : Largest "absolute" sum


Given an array of N integers (both positive and negative), find the sub-sequence with largest absolute sum.
For ex: Let A = {1 2 -5 4 5 -1 2 -11} then largest absolute sum is 11 with sub-sequence {-11}.


Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 70 : Playing Cards in Dark Room


You are standing in a pitch-dark room. A friend walks up and hands you a normal deck of 52 cards. He tells you that 17 of the 52 cards are face-up, the rest are face-down. These face-up cards are distributed randomly throughout the deck. Your task is to split up the deck into two piles, using all the cards, such that each pile has the same number of face-up cards. The room is pitch-dark, so you can't see the deck as you do this.
How can you accomplish this seemingly impossible task ?


Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.

gravatar

Blog # 69 : Repositioning 0 and 1


Given an array of integers containing only 0s and 1s.You have to place all the 0s in even position and 1s in odd position. And if suppose, no. of 0s exceed no. of 1s or vice versa then keep the exceeding integers untouched. Do that in ONE PASS and without taking extra memory (modify the array in-place).

For Example :
Input Array: [0,1,1,0,1,0,1,0,1,1,1,0,0,1,0,1,1]
Output Array: [0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1]


Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 68 : Print in reverse order


Devise an algorithm to print elements of linked list in reverse without reversing the linked list. State the time and space complexities.



Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 67 : Nth element from last


Devise an efficient algorithm for finding the Nth element from last in a linked list.

Eg. List [1, 2, 3, 4, 5, 6, 7, 8]
6th element from last = 3



Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 66 : Recruitment for Titanic


The captain of the ship needs to select the crew for the ship. But everyone seems to be eligible. So to test their intelligence, he plays a game.
The contestants have to stand in a line. They are given the numbers in the order in which they stand, starting from 1. The captain then removes all the contestants that are standing at an odd position. Initially, standing people have numbers - 1,2,3,4,5... After first pass, people left are - 2,4,... After second pass - 4,.... And so on.
You want to board the ship as a crew member. Given the total number of applicants for a position, find the best place to stand in the line so that you are selected.


Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj

gravatar

Blog # 65 : Maximum sum [A different version]


Given a sequence of positive numbers, find the maximum sum that can be formed which has no 3 consecutive elements present.

For example: consider the sequence 3000 2000 1000 3 10
Here, the answer would be 5013, by taking 3000, 2000, 3 & 10. Note that we can't form a sequence that takes 3000, 2000 & 1000 together because they are the consecutive elements of the array.

Sample cases:
1 2 3 ans=5
100 1000 100 1000 1 ans=2101
1 1 1 1 1 ans=4
1 2 3 4 5 6 7 8 ans=27

Try to find an O(N) solution. N is the number of elements in the array.

Click here to see the solution.
p.s. - For Remarks please hit the given link once. For Hint, hit it twice and for the solution, hit it thrice.



Courtesy: Vibhaj