Programming interview questions and answers

Level order traversal of a binary search tree with a new line after each level

Write a program to do level order printing of a binary search tree with a new line after each level The approach:- As in the regular level order traversal we need a queue. Push the root node into the queue and set the current level as 1. When queue not empty dequeue a node and …

Level order traversal of a binary search tree with a new line after each level Read More »

Find the minimum depth of binary search tree and maximum depth of binary search tree

Write a program to find the minimum depth of binary search tree and maximum depth of binary search tree The approach:- Depth is the height of a leaf node starting from the root. Easiest approach is to have recursive function which finds the depth of left and write sub trees. C++ program to find the minimum …

Find the minimum depth of binary search tree and maximum depth of binary search tree Read More »

Find least common ancestor (lca) of 2 nodes in a binary search tree

Write a program to find least common ancestor (lca) of 2 nodes in a binary search tree The approach:- Starting from the root node. Check if the node key is between the two provided keys. If so, the current node is the least common ancestor. If the two provided keys are greater than the current …

Find least common ancestor (lca) of 2 nodes in a binary search tree Read More »

Hexadecimal string to decimal number

Write a program to convert hexadecimal string to decimal number. The solution:- #include <iostream>using namespace std;int main() { char input[] = “64”; int val = 0; char* ptr = input; while ( *ptr != ” ) { val = val * 16 + *ptr – ‘0’; ptr++; } cout << val << endl; return 0;} …

Hexadecimal string to decimal number Read More »