Inorder Predecessor in Binary Search Tree

In Previous Post We learned about In-Order Successor.In this post ,We’ll learn what is In-Order Predecessor. The predecessor of a node x in a search tree is the node with largest key that belongs to the tree and that is strictly less than x’s key. In above binary search tree, in-order predecessor of 17 is Read More →

Mysql Query – Set 1

We have Employee,Department,Region tables as shown below: Employee(EmpID, EmpName, Salary) Department(DeptID, EmpID, DeptName) Region(RegnID, DeptID, RegnName ) Now get all employee details according to the region (RegnName) they belong? Solution: If you want to have all of the employees in the results, despite missing department and region data then you need to use OUTER joins:

Inorder Successor in Binary Search Tree

Binary Search Tree

Given a node in binary search tree say x,the in-order successor of node x is the node with the smallest value greater than node x’s value. In above binary search tree, in-order successor of 18 is 20, successor of 6 is 7 and successor of 13 is 15. Algorithm : In in-order traversal of binary Read More →

100 Prisoners and a Light bulb

light-bulb-100-prisoner-350x250

Problem: There are 100 prisoners are in solitary cells, unable to see, speak or communicate in any way with each other. There’s a central living room with one light bulb, the bulb is initially off. No prisoner can see the light bulb from his own cell. Everyday, the warden picks a prisoner at random, and Read More →

Sand Timer Puzzle

Sand timer

This Puzzle is one of the most commonly asked interview puzzle. Problem: You have two sand timers with you. One can measure 7 minutes and the other sand timer can measure 11 minutes. This means that it takes 7 minutes for the sand timer to completely empty the sand from one portion to the other. Read More →

Copy a linked list with next and arbit pointer

original list

Problem: Given a doubly linked list with one pointer of each node pointing to the next node just like in a singly linked list. The second pointer(arbit pointer) however can point to any node in the list and not just the previous node. Write a program in to create a copy of this list. Algorithm: Read More →

Find Union and Intersection of 2 sorted arrays

Union refers to the set of all the elements of the 2 arrays. Intersection here refers to the set of elements which are common in both the arrays. Steps to find union of 2 arrays: 1) i and j, initial values i = 0, j = 0 2) If a1[i] < a2[j] then print a1[i] Read More →

17 Horses Puzzle

17 horses and 3 sons puzzle

Problem: A farmer wants to divide his 17 horses among his three sons. According to farmer the oldest son should get half of the horses,the middle son should get one third of the horses and the youngest son should get one ninth of the horses. When their father died they were not able to divide Read More →

Print all ancestors of a node in binary tree

Problem: Given a Binary tree and a node’s value, Write a function to print all the ancestors of the node. Thinking of solution??Think Recursively.This problem can be easily solved using recursion. We start from the root of the tree and keep going down the tree. When we reach the node with given value we return Read More →

Rotate linked list by K nodes

You are given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. i.e. if the given linked list is: 1->2->3->4->5 and k is 3, the list should be modified to: 4->5->1->2->3. Assume that k is smaller than the number of nodes in linked list. Steps: Read More →

Software AG interview – Set 1

I am 2010 passed out from MNITJ. I would like to contribute for CrazyforCode by sharing my experience of Software AG Interview process. This was for a SSE position in Bangalore. There was no written test. I had 3 round of F2F interviews and 1 HR interview. Round 1 Q1. Interface i = 20 Abstract Read More →

Find missing number and duplicate number

Given an array of size N containing number from 1 to n,except one number is missing and one number is duplicated.Find Both numbers in linear time. This problem is an extension of our post of finding missing number in an array. Solution : Let missing number be x and duplicate number be y. We know Read More →