In a binary search tree, a node which contains blank left and right children’s that particular node is called as a leaf node in a tree. Here We are going give a c program for finding the number of leaf nodes in a tree. In a c Program we explained recursively how to find leaf … Read More →
Write a function to get the intersection point of two Linked Lists (Y Shape)
Method 1 (Brute Force) Using 2 for loops. Outer loop will be for each node of the 1st list and inner loop will be for 2nd list. In the inner loop, check if any of nodes of 2nd list is same as the current node of first linked list. Time complexity of this method will … Read More →
2 Eggs 100 Floors Puzzle
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
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 →
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 →
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
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 →
What’s going on ?