2 Eggs 100 Floors Puzzle

Tech interview puzzle, brain teasers, riddles

Problem: You are given 2 eggs. You have access to a 100-storey building. Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100 th floor.Both eggs are identical. You need to figure out the highest floor of a Read More →

Iterative preorder, inorder and postorder tree traversals

tree traversals pre order, post prder, in order

Here is a complete C program which prints a BST using both recursion and iteration. The best way to understand these algorithms is to get a pen and a paper and trace out the traversals (with the stack or the queue) alongside. Dont even try to memorize these algorithms! And here is the output… Creating the root.. Read More →

IIT Students and Hats Puzzle

Puzzle: The riddle is Nine IIT students were sitting in a classroom. Their professor wanted them to test. Next day the professor told all of his 9 students that he has 9 hats, The hats either red or black color. He also added that he has at least one hat with red color and the Read More →

Measure 4 gallon of water from 3 gallon and 5 gallon water jar

measure 4 gallon water using 3 and 4 gallon jar

Problem : How to measure exactly 4 gallon of water from 3 gallon and 5 gallon jars, Given, you have unlimited water supply from a running tap. Solution: Step 1. Fill 3 gallon jar with water. ( 5p – 0, 3p – 3) Step 2. Pour all its water into 5 gallon jar. (5p – Read More →

Shifting zeros to end in an array

Given an array with sorted numbers and zeros in between them, than how could we rearrange the array such that all the zeros are placed at end in O(1) space complexity. ex: I/p: 3 0 0 4 0 5 0 6 7 8 0 0 9 o/p: 3 4 5 6 7 8 9 0 Read More →

Print Level Order Traversal of a tree

levelordertraversal

Level Order Traversal of this tree would be 1 2 3 4 5 6 Approach: Starting from the root, first the node is visited and printed then it’s child nodes are put in a queue. Time Complexity: As we are visiting each node once.So the time complexity would be O(n) where n is the number Read More →

Determine if two binary trees are identical

Two trees are identical when they have same data and there physical layout  is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees. Time Complexity: Complexity of the problem will be according to the tree Read More →

Find the maximum of three integers using the ternary operator

Here is how you do it max = ((a>b)?((a>c)?a:c):((b>c)?b:c)); Here is another way max = ((a>b)?a:b)>c?((a>b)?a:b):c; Here is some code to find the max of 4 numbers… Method 1 Method 2

Find second Highest Salary for each department.

In previous mysql post, we found the second highest salary in employee table. In this problem we would like to find second highest salary for each department. EMPLOYEE TABLE EmpName Salary Department Steve 150$ Finance James 120$ IT Andrew 120$ Finance Mark 100$ Back-Office Adam 110$ IT Lewis 130$ Finance Smith 125$ Back-Office Solution : Read More →

10 identical bottles of pills

This is a very popular brain teaser with many companies. Problem : We have 10 identical bottles of identical pills (each bottle contain hundred of pills). Out of 10 bottles 9 have 1 gram of pills but 1 bottle has pills of weight of 1.1 gram. Given a measurement scale, how would you find the Read More →

Write C code to implement the Binary Search algorithm

Binary search is used to quickly find a value in a sorted sequence.At each step, the algorithm compares the median value in the search space to the target value. Based on the comparison and because the sequence is sorted, it can then eliminate half of the search space. By doing this repeatedly, it will eventually Read More →

what are the differences between final, finally, finalize in java?

final: final is a keyword. The keyword “final” in Java is used in different ways depending on the context. We can have final methods, final classes, final data members and final local variables. A final class implicitly has all the methods as final, but not necessarily the data members. A final class may not be Read More →