Mysql Query – Set 4

Given an Employee table with following structure EmpId EmpName Department ManagerId Part 1: Write a sql to retrieve all Manager’s Name with 5 or more subordinates. Note : ManagerId is also an Employee Id. Part 2 : Write a sql to retrieve ManagerId who have 5 or more subordinates in one column and comma separated Read More →

Diameter of a Tree

tree-diameter

Definition : Diameter of a tree is the longest path between two leaf nodes in a tree. We can see in the picture below that Diameter of the tree would be maximum of these: i. Diameter of left subtree. ii. Diameter of right subtree. iii. Path that goes through the root i.e. height of left Read More →

Gold Bar Puzzle

gold bar puzzle for interview

Puzzle: You’ve got someone working for you for seven days and a gold bar to pay him. The gold bar is segmented into seven connected pieces. You must give them a piece of gold at the end of every day. What and where are the fewest number of cuts to the bar of gold that will Read More →

Delete alternate nodes of a Linked List

You are given a Singly Linked List.  Starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->4->8->10->15 then your function should convert it to 1->8->15 Solution: Steps: Take two pointers p and q Initially p points to head and q points to link of p Read More →

Mysql Query – Set 3

Question: You are given a network table. Table contains 3 col’s – friend1, friend2, weight. +———-+——–+——–+—+ |friend1   | friend2  | weight | +———-+———-+——–+—+ | A         | B         | 5       | | B         | C         | Read More →

Replace node with sum of left and right subtree

Given a Binary tree , Replace each node with sum of values in left and right subtree. leaf nodes value will be changed to 0. Solution : i) Traverse the tree recursively. ii) Store the old value of the current node, recursively call for left and right subtrees and change the value of current node Read More →

Generate integer from 1-8 with equal probability

Problem: Write a method to generate a random number between 1 and 8, given a method that generates a random number between 1 and 3. The distribution between each of the numbers must be uniform. Solution: Let’s think of this like a decision tree. Each rand3() will be a decision. If we somehow generate integers Read More →

Frequency of a given number in a Linked List

You are given a linked list. You have to write a function that will give frequency of a given number in linked list. Here is a solution Algorithm: 1. Initialize count as zero. 2. Loop through each element of linked list: 3. If element data is equal to the given number then increment the count. Read More →

River Crossing Puzzle

River crossing puzzle

Problem: Sailor Cat needs to bring a wolf, a goat, and a cabbage across the river. The boat is tiny and can only carry one passenger at a time. If he leaves the wolf and the goat alone together, the wolf will eat the goat. If he leaves the goat and the cabbage alone together, Read More →

Check for Children Sum Property in a Binary Tree

You are given a binary tree, you have to check if tree satisfies the property of children sum. This property says that for each node sum of its left and right children should be equal to node value. Solution: 1)Traverse the given binary tree (recursively) 2) For each node check if the node and both Read More →

Mysql Query – Set 2

We have 2 tables Country and City as shown below: Country (countryid, countryname ) City (countryid, cityname ) Q1. How do you get the countries that has no cities? Q2. How do you get the countries that has less than 3 cities and also make sure the countries with no cities also show up? Solution: Read More →

8 Balls Puzzle

8 ball puzzle

Problem: You have 8 balls. One of them is defective and weighs less than others. You have a balance to measure balls against each other. In 2 weighing, how do you find the defective one? Solution: Defective ball is light Make three Groups G1 – 3 balls G2 – 3 balls G3 – 2 balls First weight- Read More →