# Programming interview questions and answers

## Linked list nth last element

Write a program to find in a linked list nth last element The approach:- Maintain 2 pointers to head of the list. Move 1st pointer n – 1 elements. Now move both the pointers until one of them hits the end of list. Return the other pointer which would point in the linked list nth …

## 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 …

## Zig zag traversal of binary search tree

Write a program to do zig zag traversal of a binary search tree The approach:- Need assistance of a queue. Same as level order traversal except that instead of pushing the left sub-tree into the queue first push the right tree sub-tree. C++ program to do zig zag traversal of a binary search tree #include <iostream>#include …

## Print all paths in a binary tree

Write a program to print all paths in a binary tree starting from the root The approach:- Initialize an array with size as maximum depth of the tree. Fill the node details in this array as we traverse the tree. If leaf node is reached a path is traversed and print the path. Recursively scan …

## Print longest path in binary tree

Write a program to print longest path in binary tree. The approach:- Initialize an array with size as maximum depth of the tree. Fill the node details in this array as we traverse the tree. If a path is reached with maximum depth print the path and return. Recursively scan the left and right sub …

## 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 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 …

## Transpose a matrix

Write a program to transpose a matrix e.g.) Input matrix.1  2  34 5 67 8 9 Transposed matrix.1 4 72 5 83 6 9 C++ program to transpose a matrix #include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main() {srand(time(0));int m = 3;int n = 3;int** matrix = (int**) calloc(sizeof(int), m);for ( int i = 0; i …

## Rotate a matrix by 90 degrees

Write a program to rotate a matrix by 90 degrees e.g)Input:-1 2 34 5 67 8 9 Output:-7 4 18 5 29 6 3 Approach to rotate a matrix by 90 degrees First we transpose the matrix and swap the columns to rotate the matrix by 90 degrees. 1) Transpose the matrix.1 4 72 5 …

## 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;} …