Given a binary tree and a number n representing the vertical level. Write code to compute the sum of all nodes which are at vertical level n. For example, if the binary tree given is as below: and n=-1, then the output should be 12. Because Nodes at level =1 are 7 and 5 and […]

Given a Binary Tree and a Node, Write a function that return the vertical distance of that node from root of the tree. The vertical distance of a node is computed as below: Then the vertical distance of root is 0. The vertical distance of right node is distance of root+1. The vertical distance of […]

Given an array of integers that represent values of nodes in a Binary Search Tree. For example, If the given array is {9, 4, 17, 6, 5, 3, 7, 22, 20} Then BST created from above array will be as below Two numbers are given (along with the array), find largest number in the path […]

Given a binary tree and a Number N, write code to check if there exist a subtree of the Binary tree with sum of all the nodes = N. For example, if the Binary tree is as given on the right: And N = 12, then the output should be ‘true’ because there exist a […]

Given a linked list, write the non-recursive function to reverse the list. If Input list is 1 -> 2 -> 3 -> 4 -> 5 Then output should be 5 -> 4 -> 3 -> 2 -> 1 We have seen the recursive and non-recursive functions to reverse the list. In the non-recursive version, we take three […]

The most difficult problems in Coding competitions and interviews of companies like Google, Microsoft etc. are from Dynamic Programming. DP as an approach to problem solving is discussed in almost all algorithm books. But, in most of the books, DP, as a concept is lost behind the difficult problems. For example, in one of the best […]

Given a Matrix of order M*N, where each cell can have zero or one orange. The orange in the cell (if present) can be either fresh or rotten. A rotten orange will rot the fresh oranges in the nearby cell in unit time. If a rotten orange is in cell (i,j), then it will rot […]

Write a recursive function that add 5 to the alternate nodes of the linked list. For example, if the list is 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 then the function should change the list to the below one 1 -> 7 -> […]

Write a function that prints all the elements of a circular linked list. (or traverse a circular linked list)

This problem is to place 8 queens on the chess board so that they do not check each other. It can be asked in two ways: Find one of the possible solutions to place 8 -queens, so that no queen can attack any other. How many different ways are possible to place 8 queens such that […]