Skip to main content

Posts

Showing posts with the label Challenge

Puzzle - How long does it take for the dogs to catch each other and where?

Question:                   There are four dogs each at the corner of a unit square. Each of the dogs starts chasing the dog in the clockwise direction. They all run at the same speed and continuously change their direction accordingly so that they are always heading straight towards the other dog. How long does it take for the dogs to catch each other and where? Answer & Explanation :                   Let the dogs be A, B, C and D where A is chasing B, B is chasing C, C is chasing D and D is chasing A. All the dogs will eventually meet in the center of the square. Since all the dogs move in symmetry, the only logical answer to the location of their meeting is the center of the square. At any point in time, dog A is perpendicular to dog B and B perpendicular to C and so on. Dog A moves towards dog B...

Challenge – Equal Probability between 1 and 7

Question:               Write a method to generate a random number between 1 and 7, given a method that generates a random number between 1 and 5. The distribution between each of the numbers must be uniform. Answer:               Let’s think of this like a decision tree. Each rand5() will be a decision. After 2 tries, we have 25 possible solutions. We try to get maximum bunches of 7 as we can (1 – 21, 3 sets of 7). If we get any of the other values, we just try again. Since the probability of getting each of 21 values are the same every time, trying again won’t affect their probabilities.       int rand7() { while (1) { int num = 5*(rand5()-1) + rand5(); if (num < 22) return ((num % 7) + 1); } } That was fun, right? Anyone up for another challenge? Watch out for it next tuesday (March 1st)...

Challenge – Find First Common Ancestor

Question:                How would you find the first common ancestor of two nodes in a binary search tree? First as in the lowest in the tree. Another way to ask is to find the lowest common ancestor of two nodes.  Meanwhile, check out the challenges from previous weeks here Answer: TreeNode findFirstCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) { return null; } if (root == p || root == q) { return root; } TreeNode left = findFirstCommonAncestor(root.left, p, q); TreeNode right = findFirstCommonAncestor(root.right, p, q); if ((left == p && right == q) || (left == q && right == q)) { return root; } return (left != null) ? left : right; } Alternate: TreeNode findFirstCommonAncestor(TreeNode root, int p, int q) { if (root == null) { return null; } if (root.value == p || root.va...

Challenge – 50 trucks with payload

Question:                Given a fleet of 50 trucks, each with a full fuel tank and a range of 100 miles, how far can you deliver a payload? You can transfer the payload from truck to truck, and you can transfer fuel from truck to truck. Assume all the payload will fit in one truck.  Meanwhile, check out the challenges from previous weeks here Answer :               We want to use as little fuel as possible so we try minimize the number of trucks we use as we go along. Let’s say we start with all 50 trucks with full fuel (5000 miles range). For each mile, we lose 50 miles in range. After two miles, we lose 100 miles leaving us with 4900 miles. This can be supported by 49 trucks so we drop one truck. As you can see for every 100 miles we lose in range, we drop a truck. 50 trucks: 100/50 49 trucks: 100/49 … Total distance = 100/50 + 100/49 + 100/48 + … + 1...

Answer- to Camel and Bananas Challenge

Thanks for all the responses. Quite a few of you have the right answer. The Question was: The owner of a banana plantation has a camel. He wants to transport his 3000 bananas to the market, which is located after the desert. The distance between his banana plantation and the market is about 1000 kilometer. So he decided to take his camel to carry the bananas. The camel can carry at the maximum of 1000 bananas at a time, and it eats one banana for every kilometer it travels. What is the largest number of bananas that can be delivered to the market? And the here is the Answer: At KM#0, we have 3000 bananas. The maximum bananas the camel can carry is 1000 so the camel must at least make 3 trips from the start point. (Leave #0, Return to #0, Leave #0, Return to #0, Leave #0) . If we move just 1km, we need 1 banana for each step mentioned above thus making a total of 5 bananas for each km . We continue making 3 trips until we reach a banana count of 2000. 3000 – 5*d = 2000 ...

Challenge – Camel and Bananas Comment your Answer

Question: The owner of a banana plantation has a camel. He wants to transport his 3000 bananas to the market, which is located after the desert. The distance between his banana plantation and the market is about 1000 kilometer. So he decided to take his camel to carry the bananas. The camel can carry at the maximum of 1000 bananas at a time, and it eats one banana for every kilometer it travels. What is the largest number of bananas that can be delivered to the market? Challenge: Do you know the answer to this question? Post in the comments. The Answer will be posted on 10th Sept   You Can see the Answer for this Challenge Here After Scheduled Date